2011-12-28 18 views
2

私は人がボックスの数を入力してからイベントを発生させると、number of boxes*someamountが値の列に入るという単純な関数を書く必要があります。私はデータグリッド表示でkeypressイベント?

が、私は私の研究

private void dataGridView1_EditingControlShowing(object sender, 
    DataGridViewEditingControlShowingEventArgs e) { 

} 

に応じてそのコードをここに書くことになると思うが、私はKeyUpイベントとアクセス列numberofboxes and Amountを置く方法を知らないコントロールをドラッグを使用し、ドロップのDataGridViewを追加しました。ありがとう

+1

はCellEndEditイベントを使用します。 –

+0

@ HansPassantありがとう、どのように私は列アクセスを得ることができます –

+0

それはあなたのイベントハンドラ、e.ColumnIndexプロパティに渡されます。 MSDNライブラリを使用して、これらの種類の回答を見つけてください。 –

答えて

7

私はこれを試して、キーダウンイベントを使用しての値をsomeAmount倍にすると、セルに新しい番号を入力するたびに計算が自動的に実行されます。 vb.net

進検証のため

 public Form1() 
    { 
     InitializeComponent(); 
     MyDataGridViewInitializationMethod(); 
    } 


    private void MyDataGridViewInitializationMethod() 
    { 

     dataGridView1.EditingControlShowing += 
    new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing); 
    } 

    private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 
    { 
     e.Control.KeyPress += 
      new KeyPressEventHandler(Control_KeyPress); 
    } 

    private void Control_KeyPress(object sender, KeyPressEventArgs e) 
    { 
     if (char.IsNumber(e.KeyChar)) 
     { 

      string cellValue = Char.ToString(e.KeyChar); 
      //Get the column and row position of the selected cell 
      int column = dataGridView1.CurrentCellAddress.X; 
      int row = dataGridView1.CurrentCellAddress.Y; 

      if (column == 1) 
      { 
      //Gets the value that existings in that cell 
      string test = dataGridView1[column, row].EditedFormattedValue.ToString(); 
      //combines current key press to the contents of the cell 
      test = test + cellValue; 
      int intNumberBoxes = Convert.ToInt32(test); 
      //Some amount to mutiple the numberboxes by 
      int someAmount = 10; 
      dataGridView1[column + 1, row].Value = intNumberBoxes * someAmount; 
      } 
     } 
    } 


} 
+0

おかげさまでありがとうございました。コード内のあなたのコメントも大変助かりました。感謝のおかげで –

+0

あなたの歓迎、心配しないでください。 – Standage

+0

あなたの 'dataGridView1_EditingControlShowing'イベントハンドラは毎回KeyPressイベントに新しいハンドラを登録しませんか?つまり、別のセルを編集するたびに、重複した 'Control_KeyPress'呼び出しが発生します。おそらく、 '+ ='の前に ' - ='をかけるべきでしょう。 –

0

Public Sub New() 
     InitializeComponent() 
     MyDataGridViewInitializationMethod() 
    End Sub 
    Private Sub MyDataGridViewInitializationMethod() 
     AddHandler dataGridView1.EditingControlShowing, AddressOf dataGridView1_EditingControlShowing 
    End Sub 
    Private Sub dataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs) 
     AddHandler e.Control.KeyPress, AddressOf Control_KeyPress 
    End Sub 
    Dim dotOnce As Boolean 
    Private Sub Control_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) 
     If e.KeyChar Like "[']" Then 
      e.Handled = True 
      Exit Sub 
     End If 
     If e.KeyChar = "." Then 
      If dotOnce Then 
       e.Handled = True 
      End If 
      dotOnce = True 
     Else 
      If (Not e.KeyChar Like "[0-9 . ]") Then 
       e.Handled = True 
       Exit Sub 
      End If 
     End If 
      End Sub 
+0

あなたは後で来るかもしれない人々のためにこのコードを説明することを検討しますか? –

+0

okがコードにコメントを追加する... –

関連する問題