472,972 Members | 2,271 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,972 software developers and data experts.

How to multiselect entries in drop down

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
5 12169
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Danny | last post by:
I like the combo box because you can scroll through items nicely, but the list box just has the navigation where you can go up and down. I am trying to allow the user to multiselect items. With...
4
by: Abbey Krystowiak | last post by:
Does anyone know if I can have a field in a row where I can add two lines of info without adding a whole new line? and then there would be a drop down menu? *** Sent via Developersdex...
3
by: Don Wash | last post by:
Hi There! I have a Server-side Drop-down box in ASP.NET (VB) page. What do I do to widen the Drop down box's Pull-Down list's width? I'm not talking about the Drop-down box's width but the box...
0
by: Robert | last post by:
Hi, I have a program with a ListView (with Multiselect) and am using the SelectedIndexChanged handler. However, when the index is changed, a rather long set of events occurs which takes quite...
2
by: Steph | last post by:
I have created a multiselect list box control (lbx_comorb) that is populated from a datatable (dt_ptAdmission). The list box populates now problem at all. However the issue is when I load the...
1
by: gimme_this_gimme_that | last post by:
What I need is the DHTML for an input box that allows javascript to add drop down entries. .... I'd like to create my own version of google suggest GS. If you haven't seen GS check it out...
2
by: ttime | last post by:
I've got a form that uses a multiselect listbox. When a user is selected from a combo box, values are populated into this listbox associated with that user. The problem is, if one person has say...
1
by: ksex948 | last post by:
Hi, I am working with two listboxes that are initially getting populated from database tables (Access). They basically allow the user to transfer information back and forth from one listbox to...
5
by: atlanteavila | last post by:
Hello all, I've attached a database here that we're using to register people for a series of classes, for a course that we're offering for pre-baptismal certification. I'm wondering if there...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
3
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.