# Select the next TextBox in order

Could somebody help me out with trying to clean up this big list of if / else if statements please? I've found that switch statements won't work because it won't handle a TextBox as the parameter. I've seen many answers involving the Dictionary, but those won't work either in my case.

    private void SelectNextTextBox()
{
TextBox newTextBox = null;

if (_currentTextBox == keyText1)
{
newTextBox = keyText2;
}
else if (this._currentTextBox == keyText2)
{
newTextBox = keyText3;
}
else if (this._currentTextBox == keyText3)
{
newTextBox = keyText4;
}
else if (this._currentTextBox == keyText4)
{
newTextBox = keyText5;
}
else if (this._currentTextBox == this.keyText5)
{
newTextBox = regText1;
}
else if (this._currentTextBox == regText1)
{
newTextBox = regText2;
}
else if (this._currentTextBox == regText2)
{
newTextBox = regText3;
}
else if (this._currentTextBox == regText3)
{
newTextBox = regText4;
}
else if (this._currentTextBox == regText4)
{
newTextBox = regText5;
}
else
{
return;
}
newTextBox.SelectAll();
newTextBox.Focus();
}


There's 10 total text boxes and when one is full it changes to the next. This is the method that tells it which text box is next in the list.

• You could use a dictionary?
– Jeremy West
Jul 7, 2015 at 1:46
• Don't know what the variable types are. It actually looks like you're comparing textbox instances to strings. I hope that's not the case.
– sstan
Jul 7, 2015 at 1:46
• What is this in the given context?
– Matt Rowland
Jul 7, 2015 at 1:48
• Is this WinForms? Just set the TabOrder of the controls.
– user24822
Jul 7, 2015 at 1:49
• "I've seen many answers involving the Dictionary, but those won't work either in my case." If you've seen many answers that give the SAME answer, it might be the right answer. Why can't you use a dictionary in your case? I don't see anything in your code that would indicate that you can't.
– aquinas
Jul 7, 2015 at 1:50

You absolutely can use a Dictionary solution. Here is what it would look like.

In some initialization code, maybe the form's constructor, write the following code:

// global mapping
private Dictionary<TextBox, TextBox> nextTextboxMap = new Dictionary<TextBox, TextBox>();

private void InitNextTextBoxMap()
{
this.nextTextboxMap[keyText1] = keyText2;
this.nextTextboxMap[keyText2] = keyText3;
this.nextTextboxMap[keyText3] = keyText4;
// add the other mappings here
// where key = current textbox, and value = next textbox.
}


And then, your SelectNextTextBox() method can be simplified to this:

private void SelectNextTextBox()
{
TextBox newTextBox = null;
if (this.nextTextboxMap.TryGetValue(this._currentTextBox, out newTextBox))
{
newTextBox.SelectAll();
newTextBox.Focus();

// Maybe you also want to reset this._currentTextBox at this point?
// this._currentTextBox = newTextBox;
}
}

• Thank you this is just what I was looking for. I definitely misunderstood the Dictionary class. All of the examples I found used a string / int combo and I assumed those were the only valid types. FYI I am resetting the currentTextBox with the TextBox_Enter handler.
– chiiefs
Jul 7, 2015 at 2:35
• @chiiefs This is why you must state in your question why you have those assumptions (Like 'Dictionary won't work in my case), as aquinas pointed out pretty early on. Jul 7, 2015 at 3:29

TextBox[] allTextBoxes = new TextBox[]{
keyText1,
keyText2,
keyText3,
keyText4,
keyText5,
regText1,
regText2,
regText3
};

int currentIndex = Array.IndexOf(allTextBoxes, this._currentTextBox);

if(currentIndex > 0)
{
int nextIndex = (currentIndex + 1) % allTextBoxes.Length;
TextBox nextTextBox = allTextBoxes[nextIndex];
nextTextBox.SelectAll();
nextTextBox.Focus();
}


List: A List<TextBox> would also work. just replace the Array.IndexOf line with allTextBoxes.IndexOf(_currentTextBox) and the .Length with .Count

From what you ask, this would be my prefered way of doing things.

List<TextBox> boxes;
int boxesIndex;


// In class constructor

boxes = new List<TextBox>(); // Then add each text box to list from first to last.
boxesIndex = -1;


// SelectNext Method

if(boxes == null || boxes.Count == 0)
throw new Exception("No textboxes have been set");

Textbox tb;
if(++boxesIndex >= boxes.Count)
{
boxesIndex = 0;
}

tb = boxes[boxesIndex];
tb.SelectAll();
tb.Focus();


Not only will this select the next text box, but will cycle back to the start once reaches the end.