Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form with 7 buttons and 7 comboboxes. When I click one of the buttons, I want to put text into the corresponding combobox, so button number 4 puts text into combobox4.

Simple enough, however, I do not want to have to repeat my code, I want it all in one block, so one chunk handles everything.

I am using FindControl, as we posted many times before in here, and elsewhere, and it works, but you can see from my switch statement, that I am still repeating code. I want to crunch that switch down to a single entry.

I created a function called AddText, it requires a string, and an object in order to work. I can then do a for loop to feed it, but not sure how to get the component for the combobox object to send it. In the switch statement, I simply put the name in there and the backend knows what object I want, but how to do that in a loop?

What I have tried:

C#
namespace LotsButtons
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Button aButton = (Button)sender;
            string lastchar = aButton.Text.Substring(aButton.Text.Length - 1);
            ComboBox cbFound;

            string cbName = "comboBox" + lastchar;

            switch (lastchar)
            {
                case "1":
                    cbFound = (ComboBox)FindControl(cbName, comboBox1);
                    if (cbFound != null)
                        cbFound.Text = "Yay";
                    break;
                case "2":
                    cbFound = (ComboBox)FindControl(cbName, comboBox2);
                    if (cbFound != null)
                        cbFound.Text = "Yay";
                    break;
                case "3":
                    cbFound = (ComboBox)FindControl(cbName, comboBox3);
                    if (cbFound != null)
                        cbFound.Text = "Yay";
                    break;
                case "4":
                    cbFound = (ComboBox)FindControl(cbName, comboBox4);
                    if (cbFound != null)
                        cbFound.Text = "Yay";
                    break;
                case "5":
                    cbFound = (ComboBox)FindControl(cbName, comboBox5);
                    if (cbFound != null)
                        cbFound.Text = "Yay";
                    break;
                case "6":
                    cbFound = (ComboBox)FindControl(cbName, comboBox6);
                    if (cbFound != null)
                        cbFound.Text = "Yay";
                    break;
                case "7":
                    cbFound = (ComboBox)FindControl(cbName, comboBox7);
                    if (cbFound != null)
                        cbFound.Text = "Yay";
                    break;
            }

            //Attempt to crunch this down to one function call, how to get component name (combobox1) from string ("combobox1")?
            for (int cnt = 1; cnt < 8; cnt++)
            {
                AddText(lastchar, (ComboBox)cbName);
            }
        }

        private void AddText(string cbName, ComboBox cbToFind)
        {
            ComboBox cbFound = (ComboBox)FindControl(cbName, cbToFind);
            if (cbFound != null)
                cbFound.Text = "Yay";
        }

        public static Control FindControl(string controlId, Control container)
        {
            if (container.Name == controlId)
                return container;

            foreach (Control control in container.Controls)
            {
                Control c = FindControl(controlId, control);
                if (c != null)
                    return c;
            }
            return null;
        }
    }
}
Posted
Updated 14-Oct-19 13:52pm
v2

1 solution

Each control has a Tag property, which is available for you to use for whatever purpose you want.
If your form constructor sets the Tag of each button to the appropriate combobox, you juts have to cast the value to a ComboBox and use it:
C#
button4.Tag = ComboBox4;
...
ComboBox cb = button.Tag as ComboBox;
if (cb != null)
   {
   ...
   }
You can even use a single Click event handler and cast the sender parameter to the button that was clicked.
 
Share this answer
 
v2
Comments
dhaskins 18-Oct-19 20:14pm    
private void button1_Click(object sender, EventArgs e)
{
//The ComboBox is named comboBox1, and button1.Tag is set to comboBox1

Button aButton = (Button)sender;
string lastchar = aButton.Text.Substring(aButton.Text.Length - 1); //This returns 1 correctly

ComboBox cb = aButton.Tag as ComboBox; //No errors reported, but cb is always null

string cbName = "comboBox" + lastchar;

if (cb != null)
AddText(cbName, cb);
}

What am I missing here?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900