Click here to Skip to main content
15,880,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I created a function to get the data from SQL server which is working with SelectedIndexChange event of a Combobox.

I want to call the same function when i press the Tab key. But the function is not fired when i press the tab key. Please help.

Function
C#
public void geterpdata()
       {
           cmbbox2.Items.Clear();
           con.Open();
           string sqlqrygetdata = "select * from erpdata";
           SqlCommand sqlcmd = new SqlCommand(sqlqrygetdata, con);
           sqlcmd.ExecuteNonQuery();
           DataTable dt = new DataTable();
           SqlDataAdapter da = new SqlDataAdapter(sqlcmd);
           da.Fill(dt);
           foreach(DataRow dr in dt.Rows)
           {
               cmbbox2.Items.Add(dr["empno"].ToString());
           }
           con.Close();
       }

Event
C#
Public Void cmbbox1_TabIndexChanged(object sender, EventArgs e)
{

geterpdata();I want to call this function when tab key is pressing.
cmbbox2.focus();

}


What I have tried:

I created a function to get the data from SQL server which is working with SelectedIndexChange event of a Combobox.

I want to call the same function when i press the Tab key. But the function is not fired when i press the tab key. Please help.
Posted
Updated 14-Jul-22 19:19pm
v2

1 solution

Don't. TAB is a specific function in nearly every operating system that uses a keyboard: it moves from one "user interactable" control to the next in sequence.
For example, from one text box to the next, or from the last input control to the "OK" button perhaps.

Subverting that makes your app non-standard and that confuses users which makes them less likely to want to go near your app.

And that's why it's not trivial: it's handled higher "up the food chain" than text box input for example. It can be done, but it takes work because it's generally a bad idea.

Think about what you are trying to and how teh user will see it before you start too far down this route!
 
Share this answer
 
Comments
arunraveendran 15-Jul-22 5:51am    
is it possible to call that function when pressing 'enter'key?
OriginalGriff 15-Jul-22 7:12am    
ENTER also has a specific function: "I've finished with the form, submit these values"

What are you trying to do?
arunraveendran 15-Jul-22 13:57pm    
I Am creating a reception management software as a part of a project.
as a user it is better to get all the values without using mouse. The receptionist entering the employee number in the combobox. Instead of using the mouse click if the receptionist pressing the tab key to move the next component like text box or button. So the application is more user-friendly if the data is fetching from server to the other components on tab key press event.
OriginalGriff 15-Jul-22 14:03pm    
Right ... so don't touch the TAB or ENTER keys - use the TextBox.Leave event instead:
https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.control.leave?view=windowsdesktop-6.0
That way no matter how the user exits the textbox, the validation gets done and the DB query executed.
arunraveendran 23-Jul-22 8:22am    
Hi.. the database retrieving multile rows for a select query. How to display each rows in a textbox one by one when pressing down arrow key?

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