DropDownList 样式的 C# ComboBox,如何设置文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/524780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# ComboBox in DropDownList style, how do I set the text?
提问by
I want to use a ComboBox with the DropDownList style (the one that makes it look like a button so you can't enter a value) to insert a value into a text box. I want the combobox to have a text label called 'Wildcards' and as I select a wildcard from the list the selected value is inserted in to a text box and the combobox text remains 'Wildcard'. My first problem is I can't seem to set a text value when the combobox is in DropDownList style. Using the properties pallet doesn't work the text value is simply cleared when you click off, adding comboBox.Text = "Wildcards"; to form_load doesn't work either. Can anyone help?
我想使用带有 DropDownList 样式的 ComboBox(使其看起来像一个按钮,因此您无法输入值)将一个值插入到文本框中。我希望组合框有一个名为“通配符”的文本标签,当我从列表中选择一个通配符时,所选值被插入到文本框中,组合框文本仍然是“通配符”。我的第一个问题是当组合框处于 DropDownList 样式时,我似乎无法设置文本值。使用属性托盘不起作用,当您单击关闭时,文本值会被清除,添加 comboBox.Text = "Wildcards"; to form_load 也不起作用。任何人都可以帮忙吗?
采纳答案by BlackWasp
The code you specify:
您指定的代码:
comboBox.Text = "Wildcards";
...should work. The only reason it would not is that the text you specify is not an item within the comboBox's item list. When using the DropDownList style, you can only set Text to values that actually appear in the list.
...应该管用。它不会的唯一原因是您指定的文本不是组合框项目列表中的项目。使用 DropDownList 样式时,只能将 Text 设置为实际出现在列表中的值。
If it is the case that you are trying to set the text to Wildcards and that item does not appear in the list, and an alternative solution is not acceptable, you may have to be a bit dirty with the code and add an item temporarily that is removed when the drop-down list is expanded.
如果您尝试将文本设置为通配符并且该项目未出现在列表中,并且不可接受替代解决方案,则您可能不得不对代码有点脏,并临时添加一个项目当下拉列表展开时被移除。
For example, if you have a form containing a combobox named "comboBox1" with some items and a button named "button1" you could do something like this:
例如,如果您的表单包含一个名为“comboBox1”的组合框,其中包含一些项目和一个名为“button1”的按钮,您可以执行以下操作:
private void button1_Click(object sender, EventArgs e)
{
if (!comboBox1.Items.Contains("Wildcards"))
{
comboBox1.Items.Add("Wildcards");
}
comboBox1.Text = "Wildcards";
}
private void comboBox1_DropDown(object sender, EventArgs e)
{
if (comboBox1.Items.Contains("Wildcards"))
comboBox1.Items.Remove("Wildcards");
}
That's pretty quick and dirty but by capturing the DropDownClosed event too you could clean it up a bit, adding the "Wildcards" item back as needed.
这非常快速和肮脏,但通过捕获 DropDownClosed 事件,您也可以稍微清理一下,根据需要添加“通配符”项目。
回答by PUG
Try this
尝试这个
comboBox1.SelectedValue = "Wildcards";
回答by Jahir
This may be a possible solution:
这可能是一个可能的解决方案:
comboBox1.SelectedValue = comboBox1.Items.FindByText("Wildcards").Value;
回答by Roman Bukin
You can select one of items on formload or in form constructor:
您可以在 formload 或表单构造函数中选择一项:
public MyForm()
{
InitializeComponent();
comboBox.SelectedIndex = 0;
}
or
或者
private void MyForm_Load(object sender, EventArgs e)
{
comboBox.SelectedIndex = 0;
}