tags:

views:

330

answers:

4

I'm loading Strings to a Combobox -

cboIndexLanguage.Items.Add("ThisLanguage")

i would like to make several of these items invisible.

A: 

Try sending a CB_SETMINVISIBLE message for each item.

OR

simply don't set the Text value!

Later edit:

int invItems = 5;
Message msg = new Message();
msg.Msg = CB_SETMINVISIBLE;
msg.LParam = 0;
msg.WParam = invItems;
msg.HWnd = combo.Handle;
MessageWindow.SendMessage(ref msg);

OR invoke this:

[DllImport("user32.dll")]
public static extern int SendMessage(int hWnd, uint Msg, long wParam, long lParam);
thelost
how do i set the CB_SETMINVISIBLE in C#?
Lai
Please see my edit. I would go with the second approach, though (i.e. set empty string for the Text property)
thelost
Hi, thanks for the answer.it looks to me that MessageWindow.SendMessage only available in windows CE...is there any other simplier solution to it?
Lai
Yes, sorry! Updated the post
thelost
While CB_SETMINVISIBLE contains "invisible", it doesn't make anything invisible. It sets the minimum number of items visible in the dropdown. ComboBox doesn't have a way to hide items, just don't add them.
Hans Passant
A: 

you can use an other list to keep strings and show wanted items to user by combo box.To do that you can define a Refresh_Combo_Method that clears combo box items and copies item from list of string to combo , then indexes was in main list.

List<string> colorList = new List<string>();
colorList.Add ("Red");
colorList.Add ("Green");
colorList.Add ("Yellow");
colorList.Add ("Purple");
colorList.Add ("Orange");
...
colorList.Remove("Red");
colorList.Insert(2, "White");
colorList[colorList.IndexOf("Yellow")] = "Black";
colorList.Clear();


...

combobox.Items.Clear();
masood
A: 

In thread-safe way you can do it pretty simple (if you only want to play with colors):

private delegate void SetBackColorDelegate(int index, Color color);
private void SetBackColor(int index, Color color)
    {
        if (cboIndexLanguage.InvokeRequired)
        {
            cboIndexLanguage.Invoke(new SetBackColorDelegate(SetBackColor), new object[] { index, color });
        }
        else
        {
            cboIndexLanguage.Items[index].BackColor = color;
        }
    }

In other way, you can just remove items from combo box but save them in some data structure f.ex:

class ComboElement
{
     public String ComboElementText { get; set; }
     public int Index { get; set; }

    public ComboElement(String elementText, int index)
    {
       ComboElementText = elementText;
       Index = index;
    }
}

//* List of hidden elements.
List<ComboElement> hiddenElements = new List<ComboElement>();

then you want to show it again, you can take it from here, and insert into the right place (you know index).

Lukas Šalkauskas
A: 

add these to your combobox

class MyCboItem
{
    public bool Visible { get; set; }
    public string Value { get; set; }
    public override string ToString()
    {
        if (Visible) return Value;
        return string.Empty;
    }
}
gbogumil