简体   繁体   中英

Focus through TextBoxes with arrows keys in C#

I have many textboxes on this form; how can I use arrow keys to focusing one by one?

Or how can I make the code below more easy and readable?

This code is basic and use for limited textboxes I think but I can't rewrite the same lines into each textbox.

40 is for Down key, 38 is for up key

my form picture, please see it

private void t1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    if (e.KeyValue == 40) { t1a.Focus(); }
    if (e.KeyValue == 38) { t1c.Focus(); }
}

private void t1a_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    if (e.KeyValue == 40) { t1b.Focus(); }
    if (e.KeyValue == 38) { t1.Focus(); }
}

private void t1b_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    if (e.KeyValue == 40) { t1c.Focus(); }
    if (e.KeyValue == 38) { t1a.Focus(); }
}

private void t1c_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    if (e.KeyValue == 40) { t1.Focus(); }
    if (e.KeyValue == 38) { t1b.Focus(); }
}

An idea I have would be:

You have a variable ( int counter = 0; ) and a list of TextBoxes ( List<TextBox> textBoxes = new(); ). In the list, there are all the TextBoxes you use, arranged in the order you want them to be clicked through.

For example:

private void PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    if (e.KeyValue == 40) { counter--; }
    else if (e.KeyValue == 38) { counter++; }
    textBoxes[counter].Focus();
}

The idea is that the integer counter represents the index of the TextBox in the list textBoxes which currently has the focus. When clicking the buttons, it changes the focused TextBox through the variable counter and gets its focus.

Note that this event-method has to be assigned to all the TextBoxes!

Hope it's what you're looking for and it helps you!

Another way is to catch the pressed key directly from the FORM. To do this you must enable the KeyPreview property. More info here enter link description here

You can use :

    void MainFormLoad(object sender, EventArgs e) 
      { 
       this.KeyPreview=true;        
      }

Then :

    void MainFormKeyDown(object sender, KeyEventArgs e)
    {   
        
        // Make a list of textBoxes
        List<string> mylist = new List<string>(new string[] { "t1", "t1a" , "t1b",  "t1c"});
        
        //Get the name of CurrentTextBox
        string CurrentTextBox=ActiveControl.Name;
        
        //Get the index of the CurrentTextBox in textBoxes list
        int index= mylist.IndexOf(CurrentTextBox);
        
        //Check the KeyCode and walk throught the list: {"t1", "t1a" , "t1b",  "t1c"}.
        //if the value of "index" is be negative or over range we will rearrange it.
        
        if (e.KeyCode.ToString()=="Up")   index--;  if (index < 0)  index=mylist.Count-1;
        if (e.KeyCode.ToString()=="Down") index++;  if (index >= mylist.Count) index=0;
        
        //Set the desired textbox to be focused.
        Controls[mylist[index]].Focus(); 

    }

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