I'm trying to make a super simple program where you use 10 different buttons to write in a code (kind of like the code to the door of an appartment complex). All the buttons have this click event:
private void number_click(object sender, EventArgs e)
{
var button = (sender as Button);
if (code.Length == 4)
{
code.Remove(0, 1);
}
switch (button.Name)
{
case "button_1":
code += "1";
break;
case "button_2":
code += "2";
break;
case "button_3":
code += "3";
break;
case "button_4":
code += "4";
break;
case "button_5":
code += "5";
break;
case "button_6":
code += "6";
break;
case "button_7":
code += "7";
break;
case "button_8":
code += "8";
break;
case "button_9":
code += "9";
break;
case "button_0":
code += "0";
break;
}
label1.Text = code;
}
I'm simply trying to make so the number the user presses get added to the code string. When there length of the string reaches 4 it is supposed to remove the first character so that there is never more than 4 characters in the string. For some reason this doesn't seem to work. What am I doing wrong?