I am making a wordle game and have 30 textboxes arranged in a grid and labeled as "_00" "_01" "_02"... The first digit is the row it's in, while the second is the column it's in. I want to change the text of these textboxes in a row. So After the first player input I change the first row, then the second after the next player input. Is there any way to do this other than switch case like putting it in an enum or array.
_00 is textbox to display.
Player_Input is where the player inputs the information.
This is in WPF.
My current code is
switch (Row)
{
case 0:
_00.Text = Player_Input.Text.Substring(0, 1);
_01.Text = Player_Input.Text.Substring(1, 1);
_02.Text = Player_Input.Text.Substring(2, 1);
_03.Text = Player_Input.Text.Substring(3, 1);
_04.Text = Player_Input.Text.Substring(4, 1);
Row ;
break;
case 1:
_10.Text = Player_Input.Text.Substring(0, 1);
_11.Text = Player_Input.Text.Substring(1, 1);
_12.Text = Player_Input.Text.Substring(2, 1);
_13.Text = Player_Input.Text.Substring(3, 1);
_14.Text = Player_Input.Text.Substring(4, 1);
Row ;
break;
case 2:
_20.Text = Player_Input.Text.Substring(0, 1);
_21.Text = Player_Input.Text.Substring(1, 1);
_22.Text = Player_Input.Text.Substring(2, 1);
_23.Text = Player_Input.Text.Substring(3, 1);
_24.Text = Player_Input.Text.Substring(4, 1);
Row ;
break;
case 3:
_30.Text = Player_Input.Text.Substring(0, 1);
_31.Text = Player_Input.Text.Substring(1, 1);
_32.Text = Player_Input.Text.Substring(2, 1);
_33.Text = Player_Input.Text.Substring(3, 1);
_34.Text = Player_Input.Text.Substring(4, 1);
Row ;
break;
case 4:
_40.Text = Player_Input.Text.Substring(0, 1);
_41.Text = Player_Input.Text.Substring(1, 1);
_42.Text = Player_Input.Text.Substring(2, 1);
_43.Text = Player_Input.Text.Substring(3, 1);
_44.Text = Player_Input.Text.Substring(4, 1);
Row ;
break;
case 5:
_50.Text = Player_Input.Text.Substring(0, 1);
_51.Text = Player_Input.Text.Substring(1, 1);
_52.Text = Player_Input.Text.Substring(2, 1);
_53.Text = Player_Input.Text.Substring(3, 1);
_54.Text = Player_Input.Text.Substring(4, 1);
Row ;
break; }
TLDR: Is there any way to shorten the above code:) Also I am not very good at English, sorry.
CodePudding user response:
How about you put your textboxes into an array, then loop over the array and pull a char from the input?
var tbs = new[] {
_00,_01,_02,_03,_04,
_10,_11,_12,_13,_14,
_20,_21,_22,_23,_24,
_30,_31,_32,_33,_34,
_40,_41,_42,_43,_44,
_50,_51,_52,_53,_54
};
for(int i = 0; i < tbs.Length; i ){
tbs[i].Text = Player_Input.Text[i%5].ToString();
}
The %5
cycles round and round from 0..4 as i
increments to 30, so the first 5 letters of the input will be cyclically put to boxes..
i=0, box=_00, char_from_input=0
i=1, box=_01, char_from_input=1
i=2, box=_02, char_from_input=2
i=3, box=_03, char_from_input=3
i=4, box=_04, char_from_input=4
i=5, box=_10, char_from_input=0 //i%5 wrapped round to 0 again..
...
All you have to do is put all 30 boxes into the array in the right order...