0

Basically I have List of TextBoxes and I have added all the TextBoxes I want to focus through. On Form1_Load I'm populating this Arrows List with TextBoxes which are included in another Lists EURTextBox, EurChange and so on ...

So I'm trying to focus through these TextBoxes like they are in 2x6 matrix similar to excel. Can you suggest me function or helpful link

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    List<TextBox> Arrows = new List<TextBox>();
}



private void Form1_Load(object sender, EventArgs e)
{
     for (int i = 0; i < 2; i++)
     {
           Arrows.Add(EURTextBox[i]);
           Arrows.Add(EURchange[i]);
           Arrows.Add(Cvetno1TextBox[i]);
           Arrows.Add(Cvetno1change[i]);
           Arrows.Add(Cvetno2TextBox[i]);
           Arrows.Add(Cvetno2change[i]);        
     }
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Georgi Antonov
  • 1,581
  • 21
  • 40
  • It's not clear what do you whant to do. *So im trying to focus through these textboxes* - do you want to focus on each textbox step by step with tab key? – Ilia Maskov Sep 07 '15 at 13:30

2 Answers2

1

The only problem with this kind of functionality, how to do the difference between "user need to navigate between cells (textboxes)" or "user wants to navigate into text to edit it".

you can use list of textboxes to solve it

foreach (var textBox in Arrows)
        {
            textBox.PreviewKeyDown += new PreviewKeyDownEventHandler(textBox_PreviewKeyDown);
        }

the event implementation

void textBox_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
    {
        int i = _arrows.IndexOf(sender as TextBox);
        if (i <= -1) return;
        switch (e.KeyCode)
        {
            case Keys.Left:
                break;
            case Keys.Right:
                break;
            case Keys.Up:
                break;
            case Keys.Down:
                break;
        }
    }

Based on index of the item in list and arrow key you can found the textbox to move to.

if textbox index in your list is like this enter image description here

Example of Rules :

  • index < 11 and Keys.Down Arrows[index+1].Focus()
  • index > 0 and Keys.Up Arrows[index-1].Focus()
  • index < 6 and Keys.Right Arrows[index+6].Focus()
  • index > 5 and Keys.Left Arrows[index-6].Focus()

if you do not want to pass from 5 to 6 with Down key you change condition to from index < 11 to (index < 11 and index != 5) etc

S. Gmiden
  • 147
  • 5
0

first, read the answers to this question: Up, Down, Left and Right arrow keys do not trigger KeyDown event

They'll give you a pretty good idea regarding how to capture the Arrow key presses.

In order to move you need to know which textbox is currently focused (Active), for this you might need to turn to something like a switch statement to look for the correct name of the ActiveControl. Something like this:

switch(this.ActiveControl.Name.ToString())
{
    case "txtBox1":
       // Do something fancy with this and the captured Arrow key 
       // and set focus to the correct text box.
       break;
    case "txtBox2":
       // Do something fancy with this and the captured Arrow key 
       // and set focus to the correct text box.
       break;
}

That should, in theory do the trick. :)

Community
  • 1
  • 1
JaggenSWE
  • 1,950
  • 2
  • 24
  • 41