| |

VerySource

 Forgot password?
 Register
Search
View: 894|Reply: 1

DataGridView cells are automatically converted to uppercase when typing

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-2-9 03:30:01
| Show all posts |Read mode
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
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-6 21:00:01
| Show all posts
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing

      Dim EditingControl As DataGridViewTextBoxEditingControl
      EditingControl = CType(e.Control, DataGridViewTextBoxEditingControl)
      EditingControl.CharacterCasing = CharacterCasing.Upper

   End Sub
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list