C#组合框在DropDownList的风格,我怎么设置文本?组合、文本、风格、我怎么

由网友(——⑦彩夢)分享简介:我想使用组合框与DropDownList的风格(一个,使得它看起来像一个按钮,这样你就不能输入值)值插入一个文本框。我想组合框有所谓的通配符文本标签,当我选择从选定的值插入到一个文本框和组合框的文本列表中的通配符仍然通配符。我的第一个问题是,我似乎无法设置一个文本值时,组合框在DropDownList的风格。使用性能托...

我想使用组合框与DropDownList的风格(一个,使得它看起来像一个按钮,这样你就不能输入值)值插入一个文本框。我想组合框有所谓的通配符文本标签,当我选择从选定的值插入到一个文本框和组合框的文本列表中的通配符仍然通配符。我的第一个问题是,我似乎无法设置一个文本值时,组合框在DropDownList的风格。使用性能托盘不工作的文本值只是清除当您单击关闭,加入comboBox.Text =通配符;以Form_load中也不起作用。任何人都可以帮忙吗?

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?

推荐答案

在code指定:

comboBox.Text = "Wildcards";

...应该工作。唯一的原因,它不会是,您所指定的文本不是组合框的项目列表中的项目。当使用DropDownList的风格,你可以只设置文本值实际出现在列表中。

...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.

如果这是您要设置文本通配符的情况和该项目没有出现在列表中,另一种解决方案是不能接受的,你可能要有点脏与code并添加条目暂时当下拉列表展开被去除。

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.

例如,如果你有一个包含一个名为ComboBox1的有一些项目和一个名为按钮组合框的窗体按钮1你可以做这样的事情:

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:

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

这是pretty的快速和肮脏的,但通过捕获DropDownClosed事件太多,你可以清理一下,加入了通配符项背的需要。

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.

阅读全文

相关推荐

最新文章