-1

I need to make a combobox with hint text "Pick a Value" and when start typing the value to be cleared and when its null to change the Combobox's text to Pick Value

In Form Load

comboBox1.Text = "Pick a Value";
comboBox1.SelectedIndex = -1;

The problem here is that the cusrsor position doesnt work. It selects all my text

In TextChanged

private void comboBox1_TextChanged(object sender, EventArgs e)
{
   if (comboBox1.Text.Length == 0)
   {
      comboBox1.Text = "Pick a Value";
   }
}

I need this code be working only if user clears the Combobox with backspase not if i will use ComboBox.Text = "" in mouse click;

example

private void comboBox1_MouseClick(object sender, MouseEventArgs e)
{
   //This conflicts the comboBox1_TextChanged event
   comboBox1.Text = "";
}

And in the end i need a code to clearing text if user start typing.

Community
  • 1
  • 1
Dim Chris
  • 19
  • 5

1 Answers1

0

You can you the isNullOrWhiteSpace on the TextChanged event

Something like this

if(string.IsNullOrWhiteSpace(combobox.Text))
{
      combobox.Text = "Pick a value";
}

But if you wanna get the desired efect, you should play with font color and opacity,or something like a watermark.

Link for watermark : https://msdn.microsoft.com/en-us/library/bb613590(v=vs.100).aspx

Also, this could help : Watermark for Textbox

Alexander
  • 405
  • 7
  • 17