Hi,
I am trying to capture a key down (or key press or key up) as I want to count the number of characters in a cell and let the user know how may the have left. The datagridview is tied to a dataset and the column has a max 200 character limit in the database, thus once they go above this the whole row is deleted by the datagridview!
So far I have worked out that I need to add an event handler to the column/cells in question and I have got so far, but it still does not update the total number of characters after each key down !!!
Any idaes where I am going wrong ?
I am trying to capture a key down (or key press or key up) as I want to count the number of characters in a cell and let the user know how may the have left. The datagridview is tied to a dataset and the column has a max 200 character limit in the database, thus once they go above this the whole row is deleted by the datagridview!
So far I have worked out that I need to add an event handler to the column/cells in question and I have got so far, but it still does not update the total number of characters after each key down !!!
VB.NET:
Private Sub DataGridView1_EditingControlShowing(sender As System.Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
'Add handler for columns 3 and 4
If Me.DataGridView1.CurrentCell.ColumnIndex > 2 And Me.DataGridView1.CurrentCell.ColumnIndex < 5 Then
AddHandler e.Control.KeyDown, AddressOf Cell_KeyDown
End If
End Sub
Private Sub Cell_KeyDown(sender As Object, e As KeyPressEventArgs)
'Recalc number of chars left
Dim CellText As String = ""
CellText = Me.DataGridView1.CurrentCell.Value.ToString
lCharCount.Text = (200 - CellText.Length) & " Chars Left!"
If CellText.Length > 199 Then
MessageBox.Show("Too many characters, 200 max!", "HCS - Alert!")
End If
End Sub
Any idaes where I am going wrong ?