Click here to Skip to main content
15,883,803 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello
sir please help me how to go previous control by press left or up arrow in vb.net
when i used this code :(up arrow)

VB
If e.KeyCode = Keys.Up Then
    Me.SelectNextControl(sender, False, True, True, True)
End If

the problem is --i don't change the listed item using up arrow in a combo box. when i press up arrow the focus go to on the previous control.

when i used this code :(left arrow)
VB
If e.KeyCode = Keys.Left Then
    Me.SelectNextControl(sender, False, True, True, True)
End If

the problem is --i don't move the cursor using left arrow in a text box. when i press left arrow the focus go to on the previous control.

please help me...

What I have tried:

VB
If e.KeyCode = Keys.Left Then
    Me.SelectNextControl(sender, False, True, True, True)
End If
Posted
Updated 27-Jul-22 0:38am
v3
Comments
Richard MacCutchan 29-Apr-18 4:40am    
The code is doing what you have written. You need to change it to allow the normal behaviour inside list or grid types.

1 solution

If I understand your question correctly, you want to prevent the selected item in the combobox from changing.

If this is correct, then set the Handled property to true in your code. Consider the following example:
VB
Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown
    If e.KeyCode = Keys.Down Then
        Me.SelectNextControl(CType(sender, Control), False, True, True, True)
        e.Handled = True
    End If
End Sub

And the same goes with textbox
VB
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
    If e.KeyCode = Keys.Left Then
        Me.SelectNextControl(CType(sender, Control), False, True, True, True)
        e.Handled = True
    End If
End Sub


Addition
----------
Wire events in code

For example in Form_Loaded event, wire all the events as follows
VB
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    For Each mycontrol As Control In Me.Controls
        If TypeOf mycontrol Is TextBox Or TypeOf mycontrol Is ComboBox Then
            AddHandler mycontrol.KeyUp, AddressOf GeneralKeyUpHandler
        End If
    Next
End Sub

and the event handler could look like
VB
Private Sub GeneralKeyUpHandler(sender As Object, e As KeyEventArgs)
    If e.KeyCode = Keys.Left Then
        Me.SelectNextControl(CType(sender, Control), False, True, True, True)
        e.Handled = True
    End If
End Sub
 
Share this answer
 
v3
Comments
Jayanta Modak 29-Apr-18 8:22am    
thanks sir for your reply.
but it is not work.
Private Sub ctrl_next(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cmbitemsalepri.KeyUp, cmbitempurpri.KeyUp, cmbitemmrp.KeyUp, cmbitemopnstk.KeyUp, txtitemcgst.KeyUp, cmbitemsl.KeyUp, cmbitemcl.KeyUp, cmbitemsubcat.KeyUp, cmbitemunit.KeyUp, cmbitemdes.KeyUp, cmbitemnm.KeyUp, cmbitemcate.KeyUp, txtitemsgst.KeyUp, txtitemigst.KeyUp, txtitemcod.KeyUp, cmbhsnitm.KeyUp

If e.KeyCode = Keys.Enter Then
Me.SelectNextControl(sender, True, True, True, True)
End If


'If e.KeyCode = Keys.Left Then
' Me.SelectNextControl(sender, False, True, True, True)
'End If
'If e.KeyCode = Keys.Left Then
' Me.SelectNextControl(CType(sender, Control), False, True, True, True)
' e.Handled = True
'End If

End Sub
going to next control i use the key is ENTER it work fine, but now i update my coding back to previous control how it is possible
Wendelius 29-Apr-18 8:42am    
Not sure what you mean. If I test the code, it works as expected.

Can you explain in more detail how the program should work and what part isn't working?
Jayanta Modak 29-Apr-18 8:53am    
sorry sir i don't wont to say that coding is wrong but i have number of combobox and textbox so, it is not possible that write code every control manually.
Wendelius 29-Apr-18 8:58am    
Don't worry, just asking for more details :)

So are you asking how to add the event handlers in code instead of doing them manually?
Wendelius 29-Apr-18 9:06am    
Have a look at the updated answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900