Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
621 views
in Technique[技术] by (71.8m points)

c# - Set hint or watermark or default text for ComboBox without adding it as item

So, I'm trying to put some text showing up on the ComboBox before it has a selection made by the user.

That text should not be an item, selectable by the user. The ComboBox has the DropDownList style on, so the user can't edit it.

This is currently what I have so far:

On the load:

this.comboBox1.Items.AddRange(new object[] {"Caixilharia em PVC", "Caixilharia em Alumínio" });

On the comboBox:

switch (comboBox1.SelectedIndex)
{
    case 0:
        Form2 newForm = new Form2();
        newForm.Show();
        this.Hide();
        break;

    case 1:
        Form3 anothernewForm = new Form3();
        anothernewForm.Show();
        this.Hide();
        break;
} 

Now, I want to show text, as on the following image:

example of where i want the text

I don't want the text to show up here, as an Item:

i don't want the text as an item, like it is here

The only way I managed to do this, the text also appeared as an item.

I'll be waiting for your help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can simply set a String to the Text property of that Combobox. This will give you the desired behaviour. Keep in mind that your code must take into account that this combo may have not any item selected. You can check this by the SelectedIndex property that will be -1.

Image1 Image2


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...