Click here to Skip to main content
16,021,125 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

When I select item from combobox and then press enter, I cant go in textbox1, I want to focus on textbox1 when I press enter in combobox.

following is my code..

Private Sub combobox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
       If Asc(e.KeyChar) = 13 Then
           e.Handled = True
           TextBox1.Focus()
           Exit Sub
       End If
   End Sub


[Edited]Code is wrapped in "pre" tags[/Edited]
Posted
Updated 16-Mar-11 22:31pm
v3
Comments
Dalek Dave 17-Mar-11 4:31am    
Edited for Grammar and Readability.

I've used same code and then press Enter after selecting value from Combobox1 to check its works or not. its works for me it successfully focusing TextBox1 after press enter. when I changed TextBox its focus corresponding Textbox. So I didn't get any error in this Coding.
 
Share this answer
 
Comments
[no name] 17-Mar-11 0:39am    
yes you are right.
1. i fill combo in form-load event.
2. when i select data and then press enter it's not work.
RaviRanjanKr 17-Mar-11 1:33am    
For your better understanding I've mentioned step by step coding so please Read my my answer I've again checking and its working well :)
Albin Abel 17-Mar-11 0:57am    
You are right. So it is strange right. I given him another solution to try which fires before keypress. So if that works then he might be get to know what wrong going between these two events. My 5 for finding there is no wrong as such the code as it is.
Use the key down event.

C#
private void comboBox1_KeyDown(object sender, KeyEventArgs e)
 {
     if (e.KeyValue == 13)
     {
         textBox1.Focus();
     }
}


In VB

VB
Private Sub combobox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown
      If e.keyValue=13 Then
         textBox1.Focus();
      End If
End Sub
 
Share this answer
 
v2
Comments
RaviRanjanKr 17-Mar-11 0:28am    
OP is asking question in VB.net and you are using C#. :)
Dalek Dave 17-Mar-11 4:32am    
It is not a difficult exercise in translating it.
Sharma Richa 17-Mar-11 0:31am    
But KeyDown is fired when key is pressed first time.
Sharma Richa 17-Mar-11 2:18am    
You can convert it to VB.
Espen Harlinn 17-Mar-11 3:36am    
Nice and simple answer to the question as it was formulated when you answered it, my 5
Take a look I've again checked your ComboBox1_KeyPress Event Coding as given
as You mentioned in Comment you are adding Some value in ComboBox in Form_Load event. I've also write a function which will able to fill ComboBox in FormLoad event
VB
Private Sub ADDData(ByVal cmb As ComboBox)
      Dim i As Integer
      For i = 0 To 10
          cmb.Items.Add(i) ' Add value from 1 to 10 in corresponding combobox 
      Next
  End Sub


VB
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       ADDData(ComboBox1) 'Calling method in form_load event for adding value im Combobox 
   End Sub


VB
Private Sub ComboBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
         If Asc(e.KeyChar) = 13 Then
           e.Handled = True
           TextBox1.Focus()  'it focus on TextBox1 after selecting value and press enter 
           Exit Sub
       End If
   End Sub
 
Share this answer
 
Comments
Dalek Dave 17-Mar-11 4:33am    
Also useful!
This is not a solution but more of a confirmation of the issue.

For me the focus goes completely out of the app. For me this is manifesting itself with only one app and occurs only once after I start the app and only the first time I use the combo box. Then intermittently but not consistent enough to draw any further deductions.

I have not yet found a solution to this but I can clarify how to duplicate.

The app is a basic DB list on the main screen using a C1 True Grid. A toolbar button is clicked where I spin up an Entry Form instance (The Entry Form is where the issue manifests itself) and do a show dialog. (The Entry Form is a simple. Not much in the form of business logic.)

I select an entry in the combobox with my mouse and press Enter. The Windows focus goes out in space. The only way to regain App focus is to click on the entry Form with my mouse.

I will simply explain it to my users and live with it. BTW...If I tab through the control order the problem does not occur.
 
Share this answer
 
Comments
CHill60 12-Apr-14 8:54am    
You're right, this is not a solution. Nor is it confirmation of any issue related to the OPs question posted 3 years ago. There is clearly something wrong with your application - without seeing the code no-one can help. It is possible that you are falling foul of an "always on top" issue http://blogs.msdn.com/b/oldnewthing/archive/2011/03/10/10138969.aspx[^]

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