none
DataGridView How to go next cell automatically when the maxlength of my cell is reached? RRS feed

  • Question

  • I have a DatGridView in which all cells contain only one letter, I want that when the user types a letter, the next cell is selected automatically
    thank you in advance

    Saturday, November 9, 2019 2:31 PM

All replies

  • Hi sesam555,

    I'm afraid you need to provide more detailed requirements.
      1) How many columns are in your DataGridView?
      2) What do you mean by "the next cell"?
          Does "the next cell" mean the right column in the same row?
      3) If "the next cell" is in the last column, what should we do?
      4) If "the next cell" is in the last row, what should we do?

    I would like to know all of your requirements.

    Regards,

    Ashidacchi -- http://hokusosha.com

    Sunday, November 10, 2019 1:44 AM
  • Hi

    Try this stand alone example.

    BTW: you used the words 'How to' in your title - seems you have asked a question. If you just want to discuss the topic then all is OK, otherwise you need to change it to a Question rather than a Discussion.

    ' Form1 with Blank DataGridView1
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim dt As New DataTable("Freddy")
    	Dim tb As TextBox
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		With dt
    			For i As Integer = 65 To 70
    				.Columns.Add(Chr(i))
    			Next
    			For i As Integer = 1 To 10
    				.Rows.Add()
    			Next
    		End With
    		With DataGridView1
    			.DataSource = dt
    			.AllowUserToAddRows = True
    		End With
    	End Sub
    	Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
    		tb = CType(e.Control, TextBox)
    		AddHandler tb.KeyUp, AddressOf DataGridView1_KeyUp
    	End Sub
    	Private Sub DataGridView1_KeyUp(sender As Object, e As KeyEventArgs) Handles DataGridView1.KeyUp
    		With DataGridView1
    			Dim r As Integer = .CurrentCell.RowIndex
    			Dim c As Integer = .CurrentCell.ColumnIndex
    			Select Case c
    				Case < .Columns.Count - 1
    					.CurrentCell = DataGridView1(c + 1, r)
    				Case = .Columns.Count - 1
    					.CurrentCell = DataGridView1(0, r + 1)
    			End Select
    			RemoveHandler tb.KeyUp, AddressOf DataGridView1_KeyUp
    		End With
    	End Sub
    End Class


    Regards Les, Livingston, Scotland


    Sunday, November 10, 2019 3:36 AM
  • Hi Leshey, you're absolutely right. I had to ask a question instead of starting a discussion, sorry!

    Your code works perfectly, whatever I did'nt understand the "trick": how tb_keyUp event  changes the DGV cells selection 

    Anyway thank you very much

    Tuesday, November 12, 2019 2:13 PM
  • Hi,

    I am glad that you have got a solution.If you resolve the issue, please mark the helpful as answer. It will be beneficial to other community.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 13, 2019 1:15 AM
    Moderator