none
dataGridViewのセルでKeyPressイベントを RRS feed

  • 質問

  • 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)
    {
          ・・・・・・・
    }

     

    2006年8月26日 1:41

回答

  • 以下のような感じで。 +=だけど、どんどん追加されていってしまいますので、最初に-=を実行しています。

    private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
         dataGridView1.EditingControl.KeyPress -=
    new KeyPressEventHandler(EditingControl_KeyPress);
         dataGridView1.EditingControl.KeyPress +=
    new KeyPressEventHandler(EditingControl_KeyPress);
    }

    2006年8月26日 5:56
    モデレータ
  • おかしいですね。うまくいかないとは、どううまくいかないんでしょうか?
    こちらで試すと、ちゃんとイベントが発生してイベントプロシージャが実行されて、cellの値も表示されています。
    2006年8月26日 9:08
    モデレータ

すべての返信

  • 以下のような感じで。 +=だけど、どんどん追加されていってしまいますので、最初に-=を実行しています。

    private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
         dataGridView1.EditingControl.KeyPress -=
    new KeyPressEventHandler(EditingControl_KeyPress);
         dataGridView1.EditingControl.KeyPress +=
    new KeyPressEventHandler(EditingControl_KeyPress);
    }

    2006年8月26日 5:56
    モデレータ
  • 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());
            }

    2006年8月26日 8:38
  • おかしいですね。うまくいかないとは、どううまくいかないんでしょうか?
    こちらで試すと、ちゃんとイベントが発生してイベントプロシージャが実行されて、cellの値も表示されています。
    2006年8月26日 9:08
    モデレータ
  • すみません。MessageBoxが表示されなかったのですが,trapemiyaさんのご指摘を受けてプログラムを改めてみてみると,dataGridView1_CellValueChangedを書くために色々と試していたときの残骸
    private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
    が残っていたことが原因でした。ありがとうございました。
    2006年8月26日 9:38
  • こっちの方がきれいかな? ステップ数は多いけど。
    
    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);
    }
    2006年8月29日 6:02
    モデレータ
  • ありがとうございます。オーバーライドの勉強に意欲がわいてきました。
    自分でもかけるようになりたいです!!。

     

    2006年8月29日 7:15