Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling item in a ComboBox C#

I have found what seems to be an easy solution to disable certain items in a ComboBox in here. It states:

You can disable an item in a list box or combo box by adding a single backslash to the beginning of the expression.

However if I write

testBox.Items.Add("\Test item");

or

testBox.Items.Add(\"Test item");

it gives a syntax error in VS2010. Maybe this function has been disabled in later than 2005 versions?

If I put an item through a VS2010 designer like this

\Test item

or I write

testBox.Items.Add("\\Test item");

then it appears with a backslash and not is disabled.

Thus my question is: is this method somehow available and I just fail to understand how to use it or I do have to create a custom ComboBox to achieve my goal (in title)?

like image 356
Andrius Naruševičius Avatar asked Jan 16 '23 15:01

Andrius Naruševičius


2 Answers

sadly is it not possible for the combobox control.

I would recommend to just remove the item from the combobox list instead of trying to disable it.

with one of those 3 ways:

// To remove item with index 0:
comboBox1.Items.RemoveAt(0);
// To remove currently selected item:
comboBox1.Items.Remove(comboBox1.SelectedItem);
// To remove "Tokyo" item:
comboBox1.Items.Remove("Tokyo");

If you absolutely need to disable items, you will need to create a custom combobox.

like image 139
TrizZz Avatar answered Jan 24 '23 21:01

TrizZz


UPDATE 1: This does NOT work, but I'm leaving it as is so the comments below make sense.

UPDATE 2: To answer your question... After a bit of googling around I believe your only option to achieve this with WinForms is to create your own control as you suggested.

I suspect the rules for working with items that begin with multiple backslashes would apply to escape sequences too. How about:

testBox.Items.Add("\]Test Item");

I'm not able to test it out, but it looks like it should work.

like image 36
John Laffoon Avatar answered Jan 24 '23 21:01

John Laffoon