Question hide an item in a combo box?

acnice

Member
Joined
Feb 18, 2010
Messages
10
Location
Colombo , Sri Lanka
Programming Experience
Beginner
I want to hide an item in a combo box with out changing indexes of other items..the collection of the combo box is hard coded.

ex: if I logged as a username ABC then combobox shouldn't have a item called ABC and want to remain other items' indexes as previous.

i can't use
cbxOption.Items.RemoveAt(index)
becouse indexes will change..



please help me..
:mad:
 
You'd have to draw the items yourself and set the height of that particular item to zero. Check the documentation for the DrawItem event and any examples you can find.
 
I want to hide an item in a combo box with out changing indexes of other items..the collection of the combo box is hard coded.

ex: if I logged as a username ABC then combobox shouldn't have a item called ABC and want to remain other items' indexes as previous.

i can't use
cbxOption.Items.RemoveAt(index)
becouse indexes will change..



please help me..
:mad:

OK, I hope I am not gonna hit my name with a plank here tonight but here goes.
I created:
1 textbox (TextBox1)
1 button (Button1)
1 combobox (ComboBox1)
My combobox is not bound to a datasource and I added all the items manually by the Edit Items function on a combobox like this:
ABC
DEF
GHI
JKL etc.
In the button's click event I placed the following code:
VB.NET:
        Dim i As Long
        For i = 0 To Me.ComboBox1.Items.Count
            If Me.ComboBox1.Items(i) = Me.TextBox1.Text Then
                Me.ComboBox1.Items(i) = ""
            End If
        Next
But this code has a disadvantage and that is that I leaves a gap where the certain item is. See the attached image. Another poster suggested you set the item height to zero. Maybe he can post some code for us here so we can see how to do it. Since we are all newbies here it won't help to just say how to do it. I searched high and low but could not find a way to set the height.:cool::cool::confused::confused::eek:
 

Attachments

  • combobox.jpg
    combobox.jpg
    9.2 KB · Views: 57
Right, because you set the text to nothing (empty string) the bounds for it are still drawn. You need to see the item's height to 0, not clear the text.
 
I searched high and low but could not find a way to set the height.
I said:
Check the documentation for the DrawItem event
That documentation says:
This event is used by an owner-drawn ComboBox. You can use this event to perform the tasks needed to draw items in the ComboBox. If you have a variable sized item (when the ComboBox.DrawMode property is set to the OwnerDrawVariable value of System.Windows.Forms.DrawMode), before drawing an item, the MeasureItem event is raised. You can create an event handler for the MeasureItem event to specify the size for the item that you are going to draw in your event handler for the DrawItem event.
The information is exactly where I said it would be. I assume that everyone has sufficient reading experience.
 
It looks to me as if you need more flexibility in this control than you can easily get with hard-coded indices. If you have to remove an item, it might be easier to do the following:

You could read the hard-coded indices into a SortedList, for example, when you first start. Then remove the undesired item when you populate the ComboBox. When the user selects the item desired, read its value, then find its hard-coded index in the SortedList.

Pseudo code:
Dim sl as New SortedList()
dim cbi as ComboBoxItem (remember, not real code!)
for each cbi in MyComboBox.Items
sl.add(cbi.text,cbi.index​
next
MyComboBox.items.remove(theBadItem)

on MyComboBox.Whatever
Dim HardCodedIndex as Int16 = Cint(sl(MyComboBox.SelectedItem.ToString))

Extra reminder: pseudo code but you should get the idea
 
OK, sorry for taking so long to post but I have been hectically busy. acnice, sorry for posting something that I knew was not right. I have offically hit my name with a plank so, sorry man, just do not have time right now to check out the whole drawitems documentation but I sincerely hope you came right.
 
Back
Top