【问题标题】:Automatically set combo box value when textbox contains text当文本框包含文本时自动设置组合框值
【发布时间】:2017-07-26 12:33:18
【问题描述】:

概述

我有 3 个文本框,txt_Mobile、txt_Landline、txt_Other。如果这 3 个文本框中的 1 个包含值,我需要根据填充的文本框自动使用字符串“Mobile”、“landline”或“other”设置组合框 (cmb_PrefconNumber)。这需要自动将combobox 的值设置为相应的值。 如果填写了两个以上的文本框,我需要让用户自己选择。但是,我仍然需要使用给定的值填充它。

我不确定是否必须绑定文本框,因为我实际上并没有使用该值,只是使用了相应的字符串。我尝试在构造函数中绑定文本框,如下所示:

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

我目前在每个validating event handler 上都有这个

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

但是,这些不会更新我的组合框,因为我认为它需要两个值,int 和 string 绑定。当我不使用文本框值本身时,我不确定如何执行此操作。

【问题讨论】:

    标签: c# winforms combobox textbox


    【解决方案1】:

    在所有 3 个文本框上使用 TextChanged 事件并在其中启动像 PopulateCombo(textboxID) 这样的函数。对于 textboxID,您为 3 个不同的文本框设置了 1、2 和 3,它看起来像这样:

    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));
            }
        }
    

    【讨论】:

    • 干杯,但我如何填充我的组合框?我试过`案例1://使用移动值填充组合框 cmb_PrefConNumber.Items.Add("Mobile");休息; case 2: //用座机值填充组合框 cmb_PrefConNumber.Items.Add("Landline");休息; `
    • filledTextboxes var 没有正确递增,textboxId 填充正常,但它总是跳到 else
    • 您在任何String.IsNullOrEmpty 中使用了textBox1.Text。你可能想解决这个问题。
    • 是的,它需要是 3 个不同的文本框
    • 我应该把我改成什么
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-21
    • 1970-01-01
    相关资源
    最近更新 更多