Carmen de Lara
hello to all, I found in Internet the following code that is very usefull to me. The code detects any character that is entered to datagrid, I am interested in the control key F2 but I don't know how to adding an event handler to a column of datagrid that raise when this key is pressed. Please help m
Private Sub AddCustomDataTableStyle( Dim ts1 As New DataGridTableStyle( ts1.MappingName = "Customers ' Add textbox column style so we can catch textbox mouse click Dim TextCol As New DataGridKeyTrapTextBoxColum TextCol.MappingName = "custID TextCol.HeaderText = "CustomerID TextCol.Width = 10 ts1.GridColumnStyles.Add(TextCol
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''add handler (help)'''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' ''
DataGridKeyTrapTextBoxColumn._RowCount = myDataSet.Tables("Customers").Rows.Coun ts1.GridColumnStyles.Add(TextCol dataGrid1.TableStyles.Add(ts1 End Sub 'AddCustomDataTableStyl
Public Class DataGridKeyTrapTextBoxColum Inherits DataGridTextBoxColum Private _keyTrapTextBox As KeyTrapTextBox = Nothin Private _source As System.Windows.Forms.CurrencyManager = Nothin Private _rowNum As Intege Private _isEditing As Boolean = Fals Public Shared _RowCount As Integer =
Public Sub New( _keyTrapTextBox = New KeyTrapTextBo _keyTrapTextBox.BorderStyle = BorderStyle.Non
AddHandler _keyTrapTextBox.Leave, AddressOf LeaveKeyTrapTextBo AddHandler _keyTrapTextBox.KeyPress, AddressOf TextBoxEditStarte End Sub 'Ne
Private Sub TextBoxEditStarted(ByVal sender As Object, ByVal e As KeyPressEventArgs _isEditing = Tru MyBase.ColumnStartedEditing(CType(sender, Control) End Sub 'TextBoxEditStarte
Private Sub LeaveKeyTrapTextBox(ByVal sender As Object, ByVal e As EventArgs If _isEditing The SetColumnValueAtRow(_source, _rowNum, _keyTrapTextBox.Text _isEditing = Fals Invalidate( End I _keyTrapTextBox.Hide( End Sub 'LeaveKeyTrapTextBo
Protected Overloads Overrides Sub Edit(ByVal [source] As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean
_RowCount = [source].Coun
MyBase.Edit([source], rowNum, bounds, [readOnly], instantText, cellIsVisible
_rowNum = rowNu _source = [source
_keyTrapTextBox.Parent = Me.TextBox.Paren _keyTrapTextBox.Location = Me.TextBox.Locatio _keyTrapTextBox.Size = Me.TextBox.Siz _keyTrapTextBox.Text = Me.TextBox.Tex Me.TextBox.Visible = Fals _keyTrapTextBox.Visible = Tru _keyTrapTextBox.BringToFront( _keyTrapTextBox.Focus( End Sub 'Edi
Protected Overrides Function Commit(ByVal dataSource As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer) As Boolea If _isEditing The _isEditing = Fals SetColumnValueAtRow(dataSource, rowNum, _keyTrapTextBox.Text End I Return Tru End Function 'Commi End Class 'DataGridKeyTrapTextBoxColum
Public Class KeyTrapTextBo Inherits TextBo
Public Sub New( End Sub 'Ne
Private Const WM_KEYDOWN As Integer = &H10 Private Const WM_KEYUP As Integer = &H10 Private Const WM_CHAR As Integer = &H10
Public Overrides Function PreProcessMessage(ByRef msg As Message) As Boolea Dim keyCode As Keys = CType(msg.WParam.ToInt32(), Keys) And Keys.KeyCod If msg.Msg = WM_KEYDOWN The Console.WriteLine(("TextBox.WM_KEYDOWN key: " + keyCode.ToString()) End I
' for a datagrid, we need to eat the tab key oe else its done twic If msg.Msg = WM_KEYDOWN AndAlso keyCode = Keys.Tab The 'to ignore a message return true without calling baseclas 'to let the textbox handle message return false; 'don't let textbox handle tab Return True End If Return MyBase.PreProcessMessage(msg)
' //sample handling code. This lets the textbox handle the delete ' //& preventing (for example) a delete shortcut on a menu getting it ' if((msg.Msg == WM_KEYDOWN) ' && keyCode == Keys.Delete) ' { ' //to ignore a message return true without calling baseclass ' //to let the textbox handle message return false; ' ' //let textbox handle Delete ' return false; ' } 'Return MyBase.PreProcessMessage(msg) End Function 'PreProcessMessage End Class 'KeyTrapTextBox
|