How to multiselect entries in drop down

  • Thread starter Thread starter Ajith Menon
  • Start date Start date
A

Ajith Menon

I need to select multiple entries in the drop down list.

E.g. Search a string in languages like C#, VB, Java etc. These entries
are in drop down. So i need to multi select to search in multiple
languages

1. One of the option could be to have a check box beside each entry in
drop down. Do any one have an idea how to do this. Solution in C# is
required but even if its unmanaged code is OK.

2. Do any one have another idea for this multi select problem.
 
Ajith, my understanding is that the dropdown is designed for single
selections.

You would be better off using a listview. This allows for multiple
selections, you could then return the selecteditems array to find out
which options were selected.

If it's the dropdown style look and feel your after, you can make sure
that the column headers aren't displayed by ensuring the listview isn't
set to detailed view. You can then play around with the events to hide
and display the listview so that it behaves in a similar way to a
dropdown box.

hth,

Gary.
 
Hi,

You need to implement ownerdrawing of the items, which means setting
DropDownStyle to DropDownList, and DrawMode to OwnerDrawFixed. Then you
need to implement the DrawItem event and code similar to the below

private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();

if ((e.State & DrawItemState.ComboBoxEdit) > 0)
{
e.Graphics.DrawString("One, Two", comboBox1.Font,
SystemBrushes.ControlText, e.Bounds.Left, e.Bounds.Top);
}
else if (e.Index > -1)
{
if (comboBox1.Items[e.Index].ToString() == "Two")
e.Graphics.DrawString("V", comboBox1.Font,
Brushes.Green, e.Bounds.Left, e.Bounds.Top);

e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(),
comboBox1.Font, SystemBrushes.ControlText, e.Bounds.Left + 20,
e.Bounds.Top);
}
}

This will draw "One, Two" in the EditBox if DrawItemEventArgs.State
contains DrawItemState.ComboBoxEdit. Replace "One, Two" with some method
calculating which language is selected.

For the rest of the items it will simply draw the item name, with the
names pushed right 20 pixels, and draw a V in front of "Two" indicating
that "Two" is selected. Instead of using DrawString to draw a V, use
DrawImage with an image of a check mark.

e.DrawBackground() takes care if the selected background color, but to
highlight the text of the selected line, use e.State to determine when
this should occur. Tip, use DrawItemState.Focus and
SystemBrushes.HighlightText
 
Hi i was very interested in this. Could you explain it a bit easier
please. I tried to do this. And when I run my form the combo box
displays one,two as you said.

But if i add items to the collection using the ide, and then run it
again and click on items they dont receive a v in front of them.

Have i misunderstood this?

Please elaborate,

Thanks,

gary.

Hi,

You need to implement ownerdrawing of the items, which means setting
DropDownStyle to DropDownList, and DrawMode to OwnerDrawFixed. Then you
need to implement the DrawItem event and code similar to the below

private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();

if ((e.State & DrawItemState.ComboBoxEdit) > 0)
{
e.Graphics.DrawString("One, Two", comboBox1.Font,
SystemBrushes.ControlText, e.Bounds.Left, e.Bounds.Top);
}
else if (e.Index > -1)
{
if (comboBox1.Items[e.Index].ToString() == "Two")
e.Graphics.DrawString("V", comboBox1.Font,
Brushes.Green, e.Bounds.Left, e.Bounds.Top);

e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(),
comboBox1.Font, SystemBrushes.ControlText, e.Bounds.Left + 20,
e.Bounds.Top);
}
}

This will draw "One, Two" in the EditBox if DrawItemEventArgs.State
contains DrawItemState.ComboBoxEdit. Replace "One, Two" with some method
calculating which language is selected.

For the rest of the items it will simply draw the item name, with the
names pushed right 20 pixels, and draw a V in front of "Two" indicating
that "Two" is selected. Instead of using DrawString to draw a V, use
DrawImage with an image of a check mark.

e.DrawBackground() takes care if the selected background color, but to
highlight the text of the selected line, use e.State to determine when
this should occur. Tip, use DrawItemState.Focus and
SystemBrushes.HighlightText



I need to select multiple entries in the drop down list.

E.g. Search a string in languages like C#, VB, Java etc. These entries
are in drop down. So i need to multi select to search in multiple
languages

1. One of the option could be to have a check box beside each entry in
drop down. Do any one have an idea how to do this. Solution in C# is
required but even if its unmanaged code is OK.

2. Do any one have another idea for this multi select problem.
 
Thanks Morten ..... thanks a lot :)

I have never got a solution to any problem so fast after posting to the
Google groups or any other forums.
What a superb solution and exactly what I needed.

Now i am just trying to fit in the image exactly at the place required.
I notice that when I move the mouse over the entry it gets refreshed
and the image goes away.
But thats not a big deal ... i know i will find a solution to that.

Morten Rocks!!!


And Gary thanks for your suggestion, but the requirement is such that a
listbox won't do.
And as far as the code given by Morten is concerned, just add an entry
with name "Two" and you will see the "V" besides that. This is just an
example, you can make your own rules to make the check visible and
invisible.

Regards,
Ajith
 
Hi Gary,

As Ajith said, I just added the simplest logic possible and to determine
if a V should be drawn I just checked to see if the name of the item ==
"Two".

To implement a multichecked combobox you will need to store the selected
items somewhere, like a List.

if (comboBox1.Items[e.Index].ToString() == "Two")
e.Graphics.DrawString("V", comboBox1.Font, Brushes.Green, e.Bounds.Left,
e.Bounds.Top);

will then change to something like

if(MyList.Contains(comboBox1.Items[e.Index])
e.Graphics.DrawString("V", comboBox1.Font, Brushes.Green, e.Bounds.Left,
e.Bounds.Top);

You will also have to subscribe to SelectedIndexChanged event to keep
track of which items should be in MyList. Add the selected item, or if
the selected item is already in the list, remove it.

The drawing of "One, Two" should be replaced with something like this

string s = "";
foreach(string item in MyList)
{
s += item + ", ";
}


Hi i was very interested in this. Could you explain it a bit easier
please. I tried to do this. And when I run my form the combo box
displays one,two as you said.

But if i add items to the collection using the ide, and then run it
again and click on items they dont receive a v in front of them.

Have i misunderstood this?

Please elaborate,

Thanks,

gary.

Hi,

You need to implement ownerdrawing of the items, which means setting
DropDownStyle to DropDownList, and DrawMode to OwnerDrawFixed. Then you
need to implement the DrawItem event and code similar to the below

private void comboBox1_DrawItem(object sender,
DrawItemEventArgs e)
{
e.DrawBackground();

if ((e.State & DrawItemState.ComboBoxEdit) > 0)
{
e.Graphics.DrawString("One, Two", comboBox1.Font,
SystemBrushes.ControlText, e.Bounds.Left, e.Bounds.Top);
}
else if (e.Index > -1)
{
if (comboBox1.Items[e.Index].ToString() == "Two")
e.Graphics.DrawString("V", comboBox1.Font,
Brushes.Green, e.Bounds.Left, e.Bounds.Top);

e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(),
comboBox1.Font, SystemBrushes.ControlText, e.Bounds.Left + 20,
e.Bounds.Top);
}
}

This will draw "One, Two" in the EditBox if DrawItemEventArgs.State
contains DrawItemState.ComboBoxEdit. Replace "One, Two" with some
method
calculating which language is selected.

For the rest of the items it will simply draw the item name, with the
names pushed right 20 pixels, and draw a V in front of "Two" indicating
that "Two" is selected. Instead of using DrawString to draw a V, use
DrawImage with an image of a check mark.

e.DrawBackground() takes care if the selected background color, but to
highlight the text of the selected line, use e.State to determine when
this should occur. Tip, use DrawItemState.Focus and
SystemBrushes.HighlightText



I need to select multiple entries in the drop down list.

E.g. Search a string in languages like C#, VB, Java etc. These entries
are in drop down. So i need to multi select to search in multiple
languages

1. One of the option could be to have a check box beside each entryin
drop down. Do any one have an idea how to do this. Solution in C# is
required but even if its unmanaged code is OK.

2. Do any one have another idea for this multi select problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top