简体   繁体   中英

Focus through TextBoxes with arrows keys similar to Excel cells

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]);        
     }
}

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 在此处输入图片说明

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

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. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM