By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,647 Members | 1,832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,647 IT Pros & Developers. It's quick & easy.

How to multiselect entries in drop down

P: n/a
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.

Jan 11 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.
Ajith Menon wrote:
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.
Jan 11 '07 #2

P: n/a
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

On Thu, 11 Jan 2007 11:38:46 +0100, Ajith Menon <ko********@gmail.com
wrote:
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.


--
Happy Coding!
Morten Wennevik [C# MVP]
Jan 11 '07 #3

P: n/a
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
Morten Wennevik [C# MVP] wrote:
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
Jan 11 '07 #4

P: n/a
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.
Morten Wennevik [C# MVP] wrote:
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

On Thu, 11 Jan 2007 11:38:46 +0100, Ajith Menon <ko********@gmail.com>
wrote:
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.

--
Happy Coding!
Morten Wennevik [C# MVP]
Jan 11 '07 #5

P: n/a
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 + ", ";
}
On Thu, 11 Jan 2007 15:23:30 +0100, Gary <ga********@myway.comwrote:
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.
Morten Wennevik [C# MVP] wrote:
>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

On Thu, 11 Jan 2007 11:38:46 +0100, Ajith Menon <ko********@gmail.com>
wrote:
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.

--
Happy Coding!
Morten Wennevik [C# MVP]


--
Happy Coding!
Morten Wennevik [C# MVP]
Jan 12 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.