当文本框包含文本时自动设置组合框值 [英] Automatically set combo box value when textbox contains text
问题描述
概述
我有 3 个文本框,txt_Mobile
、txt_Landline
、txt_Other
.如果这 3 个文本框中的 1 个包含值,我需要使用字符串Mobile"、landline"或other"自动设置我的组合框 (cmb_PrefconNumber
),具体取决于文本框充满.这就需要自动将combobox
的值设置为对应的值.如果填充了两个以上的文本框,我需要让用户自己选择它.但是,我仍然需要使用给定的值填充它.
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);
我目前在每个验证事件处理程序
if (!cmb_PrefConNumber.Items.Contains("Alternative"))
{
cmb_PrefConNumber.Items.Add("Alternative");
return;
}
但是,这些不会更新我的组合框,因为我认为它需要两个值,int
和 string
与绑定.当我不使用文本框值本身时,我不确定如何执行此操作.
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.
推荐答案
在所有 3 个文本框上使用 TextChanged
事件并在其中启动类似 PopulateCombo(textboxID)
的函数.对于 textboxID,您为 3 个不同的文本框设置了 1、2 和 3,它看起来像这样:
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));
}
}
这篇关于当文本框包含文本时自动设置组合框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!