hello everyone, i am still a newbie in programming so pls excuse my stupid question,
i am making a programm which consists of several text boxes next to eachother and under each other so i need to make the keyboard arrow keys functional so that the user could move the cursor from one another using the keyboard arrow keys to make it easier to input his data. could someone be so gentle and tell me how to do it thanks and regards.
Loading

shafeeq shafeeq139 reputationPosted Apr 17, 2013, 1:50 AM EDT
azzo trigo16 reputationPosted Apr 17, 2013, 2:57 AM EDT
Riddhi ValechaRank #794 · 2.9K reputationPosted Apr 17, 2013, 4:27 AM EDT
One option is - set the tabindex property of the textboxes.
Eg-
Say we have 5 Textbox controls
ID-Textbox1,TabIndex=1
ID-Textbox2,TabIndex=2
ID-Textbox3,TabIndex=3
ID-Textbox4,TabIndex=4
ID-Textbox5,TabIndex=5.
Now, Initially, the focus is on Textbox1.
When the user clicks "Tab" key on the keyboard, the focus will be on Textbox2.
Hope this helps.
azzo trigo16 reputationPosted Apr 17, 2013, 7:01 AM EDT
VulpesRank #55 · 29.2K reputationPosted Apr 17, 2013, 8:06 AM EDT
The first thing to do is to use the same KeyDown handler for all 60 of them. Having added one for the first textbox, just select it from the dropdown in the Properties Box for the other 59.
The code for the handler will now be as follows (off top of my head so may not be quite right):
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
Textbox tb = (TextBox)sender;
int number = int.Parse(tb.Name.Substring(7));
if (e.KeyData == Keys.Right)
{
if (number < 60)
{
tb = (TextBox)this.Controls["textBox" + (number + 1).ToString()];
}
else
{
tb = (TextBox)this.Controls["textBox1"]; // wrap around to number 1
}
tb.Focus();
}
else if (e.KeyData == Keys.Left)
{
if (number == 1)
{
tb = (TextBox)this.Controls["textBox60"]; // wrap around to number 60
}
else
{
tb = (TextBox)this.Controls["textBox" + (number - 1).ToString()];
}
tb.Focus();
}
}
The code for the up and down keys will depend on how the textboxes are arranged on the form.
azzo trigo16 reputationPosted Apr 17, 2013, 12:10 PM EDT
VulpesRank #55 · 29.2K reputationPosted Apr 17, 2013, 2:56 PM EDT
I've just tried the code myself and it seems to be working fine.
Can't really think what exception you could get on tb.Focus(). If tb were null, then it should have shown up in one of the earlier lines.
What is the actual exception you're getting?
Riddhi ValechaRank #794 · 2.9K reputationPosted Apr 17, 2013, 9:43 PM EDT
I have made CheckKeys() Method. It is working fine.
If you are satisfied, do mark the answer as Accepted Answer.
shafeeq shafeeq139 reputationPosted Apr 18, 2013, 6:15 AM EDT
VulpesRank #55 · 29.2K reputationPosted Apr 18, 2013, 6:24 AM EDT
However, if you want to give your textboxes completely different names, then you can put them into an array first and then use something similar to my approach.
azzo trigo16 reputationPosted Apr 23, 2013, 8:26 AM EDT
azzo trigo16 reputationPosted Apr 23, 2013, 9:05 AM EDT
shafeeq shafeeq139 reputationPosted Apr 23, 2013, 8:07 PM EDT
please check you form designer class .
you are missing that textbox change event in form code window.
You can write this code in your Form1.cs class
protected void 'textBox1_TextChanged(object sender,KeyEventArgs e)
{
}
Or you can delete that error line from your designer class..but you cant access the event
Ravikumar G178 reputationPosted Apr 23, 2013, 11:02 PM EDT
It is better to use Jquery for this type for situation.
In Jquery there is a function called Next() and Prev(), with this function you can traverse to the next and previous element . Call these functions based on the arrow keys.
Once go through the below links you will get an idea...
http://api.jquery.com/prev/
http://api.jquery.com/next/
azzo trigo16 reputationPosted May 1, 2013, 10:24 AM EDT
thanks again guys.
Javeed M ShaikhRank #839 · 2.7K reputationPosted May 1, 2013, 10:40 AM EDT
TextBox tb = (TextBox)sender;
with
System.Windows.Forms.TextBox tb = (TextBox)sender;
azzo trigo16 reputationPosted May 4, 2013, 9:52 AM EDT