トップ回答者
dataGridViewのセルでKeyPressイベントを

質問
-
dataGridViewのセルでKeyPressイベントを試みようとしています。
以前にVS2002で下のように書いたコードをVS2005で書き直そうとしていますが,赤色のところが全然うまくいかず,困り果てています。VS2005ではどの様に書けばよいのか教えてくださいますようお願いいたします。
private void Form1_Load(object sender, System. EventArgs e)
{
foreach(DataGridTextBoxColumn kpt in dataGrid1. TableStyles[0]. GridColumnStyles)
{
kpt. TextBox. KeyPress += new KeyPressEventHandler(this. kpt_KeyPress);
}
}private void kpt_KeyPress(object sender, System.Windows.KeyPressEventArgs kpe)
{
・・・・・・・
}
回答
-
以下のような感じで。 +=だけど、どんどん追加されていってしまいますので、最初に-=を実行しています。
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
dataGridView1.EditingControl.KeyPress -= new KeyPressEventHandler(EditingControl_KeyPress);
dataGridView1.EditingControl.KeyPress += new KeyPressEventHandler(EditingControl_KeyPress);
}
すべての返信
-
以下のような感じで。 +=だけど、どんどん追加されていってしまいますので、最初に-=を実行しています。
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
dataGridView1.EditingControl.KeyPress -= new KeyPressEventHandler(EditingControl_KeyPress);
dataGridView1.EditingControl.KeyPress += new KeyPressEventHandler(EditingControl_KeyPress);
} -
trapemiyaさんには,私の最大のピンチのときにいつも助けていただき感謝しています。ありがとうございました。
private void EditingControl_KeyPress(object sender, KeyPressEventArgs e)
{
MessageBox.Show("you press the [ " + e.KeyChar + "]");
}
http://forums.microsoft.com/MSDN-JA/ShowPost.aspx?PostID=463256&SiteID=7
で拝見させていただいた dataGridView1_CellValueChanged をまねてみましたが,うまくいきません。
緑の部分を期待しているのですが,どの様にすると可能なのでしょうか。これについても教えていただきたいのですがよろしくお願いいたします。
private void Form1_Load(object sender, EventArgs e)
{//・・・・・・・・・
dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
}private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
MessageBox.Show(dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString());
} -
こっちの方がきれいかな? ステップ数は多いけど。
DataGridViewTextBoxEditingControl editingControl; private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { editingControl = (DataGridViewTextBoxEditingControl)e.Control; dataGridView1.EditingControl.KeyPress += new KeyPressEventHandler(EditingControl_KeyPress); } private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { editingControl.KeyPress -= new KeyPressEventHandler(EditingControl_KeyPress); }