patterncsharpMinor
Select the next TextBox in order
Viewed 0 times
theordernexttextboxselect
Problem
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.
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.
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.
Solution
You absolutely can use a
In some initialization code, maybe the form's constructor, write the following code:
And then, your
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 nextTextboxMap = new Dictionary();
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;
}
}Code Snippets
// 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.
}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;
}
}Context
StackExchange Code Review Q#96056, answer score: 8
Revisions (0)
No revisions yet.