Reducing Code size from ridiculous length

This is a discussion on Reducing Code size from ridiculous length within the C# Programming forums, part of the General Programming Boards category; I have several almost exact repeated sections of code. On my windows form, one of the tab pages has a ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    129

    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.
    Last edited by DanFraser; 01-16-2005 at 05:33 PM.

  2. #2
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    Code:
    		...
    
    		private System.Windows.Forms.ComboBox comboBox1;
    		private System.Windows.Forms.ComboBox comboBox2;
    		private System.Windows.Forms.ComboBox comboBox3;
    		private System.Windows.Forms.ComboBox comboBox4;
    		private System.Windows.Forms.ComboBox comboBox5;
    		private System.Windows.Forms.ComboBox comboBox6;
    		private System.Windows.Forms.ComboBox comboBox7;
    		private System.Windows.Forms.ComboBox comboBox8;
    		private System.Windows.Forms.ComboBox comboBox9;
    		private System.Windows.Forms.ComboBox comboBox10;
    		private System.Windows.Forms.ComboBox comboBox11;
    		private System.Windows.Forms.ComboBox comboBox12;
    
    		...
    
    		this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox_SelectedIndexChanged);
    		this.comboBox2.SelectedIndexChanged += new System.EventHandler(this.comboBox_SelectedIndexChanged);
    		this.comboBox3.SelectedIndexChanged += new System.EventHandler(this.comboBox_SelectedIndexChanged);
    		this.comboBox4.SelectedIndexChanged += new System.EventHandler(this.comboBox_SelectedIndexChanged);
    		this.comboBox5.SelectedIndexChanged += new System.EventHandler(this.comboBox_SelectedIndexChanged);
    		this.comboBox6.SelectedIndexChanged += new System.EventHandler(this.comboBox_SelectedIndexChanged);
    		this.comboBox7.SelectedIndexChanged += new System.EventHandler(this.comboBox_SelectedIndexChanged);
    		this.comboBox8.SelectedIndexChanged += new System.EventHandler(this.comboBox_SelectedIndexChanged);
    		this.comboBox9.SelectedIndexChanged += new System.EventHandler(this.comboBox_SelectedIndexChanged);
    		this.comboBox10.SelectedIndexChanged += new System.EventHandler(this.comboBox_SelectedIndexChanged);
    		this.comboBox11.SelectedIndexChanged += new System.EventHandler(this.comboBox_SelectedIndexChanged);
    		this.comboBox12.SelectedIndexChanged += new System.EventHandler(this.comboBox_SelectedIndexChanged);
    
    		...
    
    		private void comboBox_SelectedIndexChanged(object sender, System.EventArgs e)
    		{
    			MessageBox.Show(this, "Sender: " + ((System.Windows.Forms.ComboBox)sender).Name);
    		}
    
    		...
    Last edited by BMJ; 01-16-2005 at 07:04 PM.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    After trying out that code, I can see what you are showing with that, but with me still being new to programming OOP overall, I am struggling to see how to link the changing of one combo box will affect the selection of another (on the same line).

  4. #4
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    The event of any of the comboboxes results in going to the same function, instead of many for their own.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    but i have 16 lines of [combobox] [combobox] [textbox] [textbox] [textbox], the first combo box in each line only affects the other controls on the same line. These of course have numbers in their name. I can see what you have both said, its the controlling the relevant controls from whatever control is changed is the problem i am having.

    example, selecting option 5 from the first combo box tells the other controls on the same line to enable, and what items to put in the second combobox of the same line.

    Its that number that is stumping me.

  6. #6
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Code:
    if(((System.Windows.Forms.ComboBox)sender).Name == "ComboBox2") {
     int nOptionNumber = ((System.Windows.Forms.ComboBox)sender).selectedIndex;
    }
    I'm really sorry if I'm not understanding

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    I'll shorten the code and rework the example a little.

    Code:
    public void combobox1line1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    switch(combobox1line1.SelectedItem)
    {
    case "1":
    combobox2line1.Enabled = true;
    textbox1line1.Enabled = true;
    textbox2line1.Enabled = true;
    textbox3line1.Enabled = true;
    combobox2line1.Items.AddRange(new object[] { "1",  "2", "3" } );
    break;
    case "2";
    combobox2line1.Enabled = true;
    textbox1line1.Enabled = true;
    textbox2line1.Enabled = false;
    textbox3line1.Enabled = false;
    combobox2line1.Items.AddRange(new object[] { "4",  "5", "6" } );
    break;
    basically, when i choose an item in combobox1line1, i want it to set the properties of other controls on the same line. So in the function that the event (that I understand) is called when you change an item in combobox 1, it must find the number at the end of the control that activated the event, and then apply that number to the end of the control name (ie. replacing the last number of the control inside the switch function in the example code).

    I can see the 'find out what box commited the crime'. But I am having trouble with the 'shortened' version of, 'ok, box 2 did it, so only the boxes on the second line are affected'.

  8. #8
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Now when I finally understand, I don't know how to help you lol, but I hope this gives you some ideas:
    Code:
    public void combobox1line1_SelectedIndexChanged(object sender, System.EventArgs e) {
     ArrayList allComboBoxes = new ArrayList();
     allComboBoxes.add(combobox1line1);
     allComboBoxes.add(combobox1line2);
     allComboBoxes.add(combobox1line3);
     allComboBoxes.add(combobox1line4);
     allComboBoxes.add(combobox1line5);
     
     ArrayList allTextBoxes = new ArrayList();
     allComboBoxes.add(textbox1line1);
     allComboBoxes.add(textbox1line2);
     allComboBoxes.add(textbox1line3);
     allComboBoxes.add(textbox1line4);
     allComboBoxes.add(textbox1line5);
     
     for(int i=0; i<allComboBoxes.size(); i++) {
      switch( ((ComboBox)allComboBoxes[i]).SelectedItem ) {
       case "1":
        ((TextBox)allTextBoxes[i]).Enabled = true;
       case "2":
        ((TextBox)allTextBoxes[i]).Enabled = false;
      }
     }
    }
    I don't have C# with me, I'm doing this from past works.

  9. #9
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    Thanks for the help, i'll have a play!

  10. #10
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    That works excellently. Thanks again for the help. It was difficult explaining what I want to do as I am still learning C#, so the terms etc are still a mystery to me.

    Once again, thanks!

  11. #11
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    I'm really glad I helped you at least a little bit, all of those questions I asked you, I'm surprised you didn't kill me! lol

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reducing rational numbers - code not working properly
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 07:42 AM
  2. Reducing my code
    By limitmaster in forum C++ Programming
    Replies: 7
    Last Post: 01-09-2008, 06:05 AM
  3. Trouble with DMA Segmentation Faults
    By firestorm717 in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 09:20 PM
  4. Reducing the size of compiled file
    By Diablo84 in forum C++ Programming
    Replies: 11
    Last Post: 04-07-2005, 04:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21