C# & .Net 2.0 question (WinForms)
I have set of items in ComboBox
and non of them selected. I would like to show a string on combo \"Pl
Why not do it XAML?
<ComboBox x:Name="myComboBoxMenu" PlaceholderText="Hello"/>
Use the insert method of the combobox to insert the "Please select item" in to 0 index,
comboBox1.Items.Insert(0, "Please select any value");
and add all the items to the combobox after the first index. In the form load set
comboBox1.SelectedIndex = 0;
EDIT:
In form load write the text in to the comboBox1.Text
by hardcoding
comboBox1.Text = "Please, select any value";
and in the TextChanged event of the comboBox1 write the following code
private void comboBox1_TextChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex < 0)
{
comboBox1.Text = "Please, select any value";
}
else
{
comboBox1.Text = comboBox1.SelectedText;
}
}
I used a quick work around so I could keep the DropDownList style.
class DummyComboBoxItem
{
public string DisplayName
{
get
{
return "Make a selection ...";
}
}
}
public partial class mainForm : Form
{
private DummyComboBoxItem placeholder = new DummyComboBoxItem();
public mainForm()
{
InitializeComponent();
myComboBox.DisplayMember = "DisplayName";
myComboBox.Items.Add(placeholder);
foreach(object o in Objects)
{
myComboBox.Items.Add(o);
}
myComboBox.SelectedItem = placeholder;
}
private void myComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (myComboBox.SelectedItem == null) return;
if (myComboBox.SelectedItem == placeholder) return;
/*
do your stuff
*/
myComboBox.Items.Add(placeholder);
myComboBox.SelectedItem = placeholder;
}
private void myComboBox_DropDown(object sender, EventArgs e)
{
myComboBox.Items.Remove(placeholder);
}
private void myComboBox_Leave(object sender, EventArgs e)
{
//this covers user aborting the selection (by clicking away or choosing the system null drop down option)
//The control may not immedietly change, but if the user clicks anywhere else it will reset
if(myComboBox.SelectedItem != placeholder)
{
if(!myComboBox.Items.Contains(placeholder)) myComboBox.Items.Add(placeholder);
myComboBox.SelectedItem = placeholder;
}
}
}
If you use databinding you'll have to create a dummy version of the type you're bound to - just make sure you remove it before any persistence logic.
One line after form InitializeComponent();
cbo_MyDropBox.Text = "Select a server...";
You only need it once right? All you need to do if a pick is mandatory is check if the box index != -1
. Could anyone elaborate why the other answers jump through hoops to get this going?
The only thing I'm missing here is having just this initial text grayed out. If you really want that just use a label in front and turn it off once the index is changed.
if ComboBoxStyle
is set to DropDownList
then the easiest way to make sure the user selects an item is to set SelectedIndex = -1
, which will be empty
Make the Dropdownstyle property of combo box to Dropdown and set the combo box text to "Select" as below
combobox.DataSource = dsIn.Tables[0];
combobox.DisplayMember = "Name";
combobox.ValueMember = "Value";
combobox.Text = "--Select--";