dave.2 dave.2 - 1 year ago 117
C# Question

Automatically set combo box value when textbox contains text

Overview

I have 3 text boxes,

txt_Mobile
,
txt_Landline
,
txt_Other
. If 1 of these 3 text boxes contains a value, I need to automatically set my combo box (
cmb_PrefconNumber
) with the string "Mobile", "landline" or "other", depending on the text box that is filled. This then needs to automatically set the value of the
combobox
to the corresponding value.
If more than two text boxes are filled, I need to let the user select it themselves. However, I still need to populate it with the given values.

I'm not sure whether I have to bind the textbox or not, as I'm not actually using the value, just the corresponding string. I've tried binding the text box in my constructor like so:

binding = new Binding("Text", cmb_PrefConNumber, "Text");
cmb_PrefConNumber.DataBindings.Add(binding);


I currently have this on each
validating event handler


if (!cmb_PrefConNumber.Items.Contains("Alternative"))
{
cmb_PrefConNumber.Items.Add("Alternative");
return;
}


However, these don't update my combo box, as I think it needs the two values,
int
and
string
with binding. I'm unsure of how to do this when I'm not using the textbox value itself.

Answer Source

Use the TextChanged event on all 3 textboxes and start a function like PopulateCombo(textboxID) inside them. For textboxID you set 1, 2 and 3 for 3 different textboxes and it would look like this:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    if(!String.IsNullOrEmpty(textBox1.Text))
    {
        PopulateCombo(1);
    }
}

private void textBox2_TextChanged(object sender, EventArgs e)
{
    if(!String.IsNullOrEmpty(textBox2.Text))
    {
        PopulateCombo(2);
    }
}

private void textBox3_TextChanged(object sender, EventArgs e)
{
    if(!String.IsNullOrEmpty(textBox3.Text))
    {
        PopulateCombo(3);
    }
}

private void PopulateCombo(int textBoxID)
    {
        //With this you will get how many textBoxes have value
        int filledTextboxes = 0;
        if(!String.IsNullOrEmpty(textBox1.Text))
        {
            filledTextboxes++;
        }
        if (!String.IsNullOrEmpty(textBox2.Text))
        {
            filledTextboxes++;
        }
        if (!String.IsNullOrEmpty(textBox3.Text))
        {
            filledTextboxes++;
        }

        //With this you will run one code if only one textbox has value and other if more than one has value
        if(filledTextboxes == 1)
        {
            switch(textBoxID)
            {
                case 1:
                    comboBox1.Items.Clear();
                    comboBox1.Items.Add("TextBox1");
                    break;
                case 2:
                    comboBox1.Items.Clear();
                    comboBox1.Items.Add("TextBox2");
                    break;
                case 3:
                    comboBox1.Items.Clear();
                    comboBox1.Items.Add("TextBox3");
                    break;
            }
        }
        else
        {
            comboBox1.Items.Clear();
            MessageBox.Show(String.Format("All items cleared because there are {0} boxes with value", filledTextboxes));
        }
    }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download