Reducing Code size from ridiculous length
I have several almost exact repeated sections of code. On my windows form, one of the tab pages has a large amount of combo boxes and text boxes to enter information. Its a basic game to be exact.
I have 16 lines of [combobox] [combobox] [textbox] [textbox] [textbox]
as you can imagine, any amount of code is gonna long and drawn out. You havent seen my code yet. It gets quite complex, to prevent the user making mistakes, guide them to the correct way of filling in these text boxes. It would still be huge overall anyway without the fancy bits.
Code:
public void Combobox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
if((string)Combobox1.SelectedItem == "")
{
CheckBoxesClear();
}
else
{
CheckBoxesClearElse();
}
Combobox2.Items.Remove (Combobox2.SelectedItem);
Combobox2.Items.Clear();
Combobox2.Enabled = false;
textBox1.Enabled = false;
textBox2.Enabled = false;
textBox3.Enabled = false;
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
switch ((string)Combobox1.SelectedItem)
{
case "AA":
textBox1.Enabled = true;
textBox2.Enabled = true;
break;
case "AB":
Combobox2.Enabled = true;
textBox1.Enabled = true;
textBox2.Enabled = true;
textBox3.Enabled = true;
Combobox2.Items.AddRange(new object[]{ "1", "2", "3", "4", "5", "6",
"7", "8"});
break;
case "AC":
Combobox2.Enabled = true;
textBox1.Enabled = true;
textBox3.Enabled = true;
Combobox2.Items.AddRange(new object[]{ "1", "2", "3", "4", "5", "6",
"7", "8"});
break;
case "AD":
Combobox2.Enabled = true;
textBox3.Enabled = true;
Combobox2.Items.AddRange(new object[]{ "1", "2", "3", "4", "5", "6",
"7", "8"});
break;
case "AE":
Combobox2.Items.AddRange(new object[]{ "1", "2", "3", "4", "5", "6",
"7", "8"});
Combobox2.Enabled = true;
textBox1.Enabled = true;
textBox2.Enabled = true;
break;
case "AF":
textBox1.Enabled = true;
textBox2.Enabled = true;
break;
case "AG":
Combobox2.Enabled = true;
textBox1.Enabled = true;
textBox3.Enabled = true;
Combobox2.Items.AddRange(new object[]{ "1", "2", "3", "4", "5", "6",
"7", "8"});
break;
case "AH":
Combobox2.Enabled = true;
textBox3.Enabled = true;
Combobox2.Items.AddRange(new object[]{ "1", "2", "3", "4", "5", "6",
"7", "8"});
break;
case "AI":
textBox1.Enabled = true;
break;
case "AJ":
textBox1.Enabled = true;
break;
case "AK":
Combobox2.Enabled = true;
textBox1.Enabled = true;
textBox3.Enabled = true;
Combobox2.Items.AddRange(new object[]{ "1", "2", "3", "4", "5", "6",
"7", "8"});
break;
case "AL":
Combobox2.Enabled = true;
textBox1.Enabled = true;
textBox2.Enabled = true;
textBox3.Enabled = true;
Combobox2.Items.AddRange(new object[]{ "1", "2", "3", "4", "5", "6",
"7", "8"});
break;
case "AM":
textBox1.Enabled = true;
textBox2.Enabled = true;
break;
case "AN":
Combobox2.Enabled = true;
textBox1.Enabled = true;
textBox3.Enabled = true;
Combobox2.Items.AddRange(new object[]{ "1", "2", "3", "4", "5", "6",
"7", "8"});
break;
case "AO":
Combobox2.Enabled = true;
Combobox2.Items.AddRange(new object[]{ "1", "2", "3", "4", "5", "6",
"7", "8"});
break;
case "AP":
Combobox2.Enabled = true;
textBox1.Enabled = true;
textBox2.Enabled = true;
textBox3.Enabled = true;
Combobox2.Items.AddRange(new object[]{ "1", "2", "3", "4", "5", "6",
"7", "8"});
break;
case "AQ":
textBox1.Enabled = true;
textBox2.Enabled = true;
break;
case "AR":
textBox1.Enabled = true;
break;
case "AS":
textBox2.Enabled = true;
textBox3.Enabled = true;
break;
case "AT":
textBox1.Enabled = true;
textBox2.Enabled = true;
break;
case "AU":
textBox1.Enabled = true;
break;
case "AV":
textBox1.Enabled = true;
textBox2.Enabled = true;
break;
case "AW":
Combobox2.Enabled = true;
textBox1.Enabled = true;
textBox3.Enabled = true;
Combobox2.Items.AddRange(new object[]{ "1", "2", "3", "4", "5", "6",
"7", "8"});
break;
case "AX":
textBox1.Enabled = true;
textBox2.Enabled = true;
break;
case "AY":
Combobox2.Enabled = true;
textBox1.Enabled = true;
textBox3.Enabled = true;
Combobox2.Items.AddRange(new object[]{ "1", "2", "3", "4", "5", "6",
"7", "8"});
break;
case "AZ":
Combobox2.Enabled = true;
textBox1.Enabled = true;
textBox2.Enabled = true;
textBox3.Enabled = true;
Combobox2.Items.AddRange(new object[]{ "1", "2", "3", "4", "5", "6",
"7", "8"});
break;
case "BA":
textBox1.Enabled = true;
break;
case "BB":
textBox3.Enabled = true;
break;
case "BC":
Combobox2.Enabled = true;
textBox1.Enabled = true;
textBox3.Enabled = true;
Combobox2.Items.AddRange(new object[]{ "1", "2", "3", "4", "5", "6",
"7", "8"});
break;
case "BD":
textBox1.Enabled = true;
textBox2.Enabled = true;
break;
case "BE":
Combobox2.Enabled = true;
textBox1.Enabled = true;
textBox2.Enabled = true;
textBox3.Enabled = true;
Combobox2.Items.AddRange(new object[]{ "1", "2", "3", "4", "5", "6",
"7", "8"});
break;
case "BF":
textBox3.Enabled = true;
break;
case "BG":
textBox1.Enabled = true;
textBox2.Enabled = true;
break;
case "BH":
Combobox2.Enabled = true;
textBox1.Enabled = true;
textBox3.Enabled = true;
Combobox2.Items.AddRange(new object[]{ "1", "2", "3", "4", "5", "6",
"7", "8"});
break;
case "BI":
textBox3.Enabled = true;
break;
case "BJ":
break;
case "BK":
Combobox2.Enabled = true;
textBox3.Enabled = true;
Combobox2.Items.AddRange(new object[]{ "1", "2", "3", "4", "5", "6",
"7", "8"});
break;
case "BL":
Combobox2.Enabled = true;
textBox1.Enabled = true;
Combobox2.Items.AddRange(new object[] { "1", "2", "3", "4", "5", "6",
"7", "8"});
break;
}
}
there it is. Told you it was big...
I know how to repeat standard code, like if statements for example -
Code:
private void Command1_Click()
{
thefunc ("Hello there") ;
}
private void thefunc(string Msg4U)
{
MessageBox.Show(Msg4U);
}
Its the control names that are getting me on this one. How can I just have one instance of the above code, but linking it to a selectedIndexChanged event from 16 different comboboxes, each one affecting 16 different groups of comboboxes and textboxes?
Oh, and how can I overall speed up the rendering of controls on my windows form? This isnt slow at loading, but you can definitely see it load, a good description might be "a bit clunky".
Thanks for anything you can help with.