Tutorial :.NET invisible items in combobox


I'm loading Strings to a Combobox -


i would like to make several of these items invisible.


I've found a solution. It can be done by binding DataTable object:

            comboBox.DisplayMember = "VALUE";              comboBox.ValueMember = "ID";                DataTable dt = new DataTable();              dt.Columns.Add("ID", typeof(int));              dt.Columns.Add("VALUE");                for (int i = 0; i < 5; i++)              {                  DataRow row = dt.NewRow();                  row[0] = i;                  row[1] = "val"+i;                  dt.Rows.Add(row);              }              dt.AcceptChanges();                comboBox.DataSource = dt;  

Then simply found an item you want to hide and hide it (by setting as deleted):

dt.Select("ID = 2").First().Delete();  

Or unhide all:



Try sending a CB_SETMINVISIBLE message for each item.


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);  


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();  


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).


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;      }  }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »