|
I set CharacterCasing on the cell or column properties of the DataGridView to implement automatic case conversion and call its TextChange method. Can't find it at all. Fortunately, two additional events, KeyPress and TextChange, were added to the DataGridView's EditControl. And in the KeyPress event, the input characters are converted to uppercase and lowercase.
I want to be able to directly modify the CharacterCasing property and call the KeyPress and TextChange events when designing controls like TextBox. What should I do?
'The following is the program code, a DataGridView control and a TextBox control are added to the form
Private Sub AddEditControlKeypressHandler (ByVal GrdObject As DataGridView)
GrdObject.ReadOnly = False
If GrdObject.GetCellCount (DataGridViewElementStates.None) = 0 Then
GrdObject.Rows.Add ()
End If
GrdObject.CurrentCell = GrdObject.Rows (0) .Cells (0)
If GrdObject.Columns (GrdObject.CurrentCell.ColumnIndex) .CellTemplate.GetType.Name <> "DataGridViewTextBoxCell" Then
MsgBox (GrdObject.Columns (GrdObject.CurrentCell.ColumnIndex) .CellTemplate.GetType.Name)
Exit Sub
End If
InEditMode (GrdObject)
Call AddEvent (GrdObject.EditingControl)
End Sub
Sub AddEvent (ByVal obj As Control)
If obj Is Nothing Then
Exit Sub
End If
AddHandler obj.KeyPress, New KeyPressEventHandler (AddressOf CellEditKeyPress)
AddHandler obj.TextChanged, AddressOf CellTextChanged
End Sub
'Convert to uppercase
Sub CellEditKeyPress (ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
'Convert characters to uppercase
e.KeyChar = UCase (e.KeyChar)
e.Handled = False
End Sub
'When inputting data in the editing state, the input content is displayed in TextBox1 in real time
Sub CellTextChanged (ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.TextBox1.Text = "You entered:"&sender.Text
End Sub
'Add event for first cell of DataGridView when form is loaded
'There is a question, why do you only need to add an event to the first cell and each cell can execute this event? Please help to solve the problem.
Private Sub Form2_Load (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call AddEditControlKeypressHandler (Me.DataGridView1)
End Sub
'Enter the editing state directly when clicking on a cell
Private Sub DataGridView1_CellEnter (ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
Call InEditMode (Me.DataGridView1)
End Sub
'' '<summary>
'' 'Specifies that the cell that has focus and has been selected as active enters edit mode
'' '</ summary>
'' '<remarks> </ remarks>
Sub InEditMode (ByVal GrdObj As DataGridView)
Dim oldMode As DataGridViewEditMode = GrdObj.EditMode
GrdObj.EditMode = DataGridViewEditMode.EditProgrammatically
GrdObj.BeginEdit (True)
GrdObj.EditMode = oldMode
End Sub |
|