Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more: , +
How to create a custom comboBox which can group Items(like HTML dropDownList)??
I searched on google, but I could not find proper example ..
Is it possible with DevExpress control??

Any suggestion would be appreciate....

Regards
Jayanta...
Posted

If you are willing to do the work to create a UserControl, you can combine a ListView, which supports Groups, with a TextBox, for displaying the selected List Item. It's actually pretty easy to do, but, if you are relatively new to C# and Windows Forms, and have no experience with UserControls, I would suggest you carefully analyze your available time, and motivation, before taking this on.

The strategy would be like this:

1. create a new UserControl

2. put a ListView in it docked to the Bottom

3. put a TextBox in it docked to the Top.

4. choose a Font for the TextBox so it covers up the necessary column you must have to display items.

5. get the design looking right.

6. create the necessary logic to:

a. present the UserControl, initially, so its Height is equal to the height of the TextBox.

b. when the TextBox gets a Click, resize the UserControl to the full height it was in design-mode. That will display the ListView and its Groups, and Items.

c. when an Item in the ListView is selected, then put the Text of that Item in the TextBox, and set the UserControl height back to the height of the TextBox.

Of course, you have to expose, for use by consumers of the UserControl, any data from the UserControl necessary.

If you are really interested in this, and have time to work hard, let me know, and I'll post a rough code sketch.

... edit #1 ...

7. raising a custom event from the UserControl when a a ListViewItem is selected: assume the UserControl with ListView and TextBox is named 'LVCombo.

a. define a custom EventArgs class to hold data generated when the Event is raised:
C#
public class LVEventArgs : EventArgs
{
    public int CurrentIndex;
    public string CurrentText;

    public LVEventArgs(int i, string s)
    {
        CurrentIndex = i;
        CurrentText = s;
    }
}
b. define the custom Event:
C#
// the Event declaration
public event EventHandler<LVEventArgs> LVSelectedIndexChanged;

// the Event code
protected virtual void OnLVSelectedIndexChanged(LVEventArgs e)
{
    EventHandler<LVEventArgs> handler = LVSelectedIndexChanged;
    if (handler != null) handler(this, e);
}
c. in the ListView SelectedIndexChanged EventHandler create the custom data to be transmitted, and invoke the Event:
C#
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
    // handle potential no selection error
    if (listView1.SelectedItems.Count == 0) return;

    int ndx = listView1.SelectedIndices[0];
    string txt = listView1.Items[ndx].Text;

    // write the selected value to the TextBox
    textBox1.Text = txt; ;

    // collapse the UserControl
    this.Height = textBox1.Height;

    // invoke the Event
    OnLVSelectedIndexChanged(new LVEventArgs(ndx, txt));
}
8. Example of subscribing to the custom Event from the Form that hosts the UserControl:

... assume the UserControl on the Form is named 'lvCombo1 ...
C#
private void Form1_Load(object sender, EventArgs e)
{
    lvCombo1.LVSelectedIndexChanged += lvCombo1_LVSelectedIndexChanged;
}

// note that we use the custom EventArgs here
private void lvCombo1_LVSelectedIndexChanged(object sender, LVEventArgs e)
{
    // write the information out to a TextBox on the Form, 'textBox1
    textBox1.Text = string.Format("index: {0} value: {1}", e.CurrentIndex, e.CurrentText);
}
Note that in step #7b. we used the generic Event syntax available from .NET 2.0; this syntax lets us define the necessary Event "plumbing" without defining a Delegate.

Good resources for understanding creating, raising, and consuming, Events in C#: on CodeProject: [^], on MSDN: [^], StackOverFlow: [^]

... end edit #1 ...
 
Share this answer
 
v6
Comments
JayantaChatterjee 27-Mar-14 1:50am    
Sir, I have lots of time,
I'm pretty excited to do it..
can you post your rough code sketch.
BillWoodruff 27-Mar-14 2:51am    
Is this a Windows Forms project ?

Are you familiar with the ListView Control and how to use Groups in it ?

Do you understand how to define a custom EventArgs Class, and use it in an Event you raise ?
JayantaChatterjee 27-Mar-14 10:46am    
Yes it is Winform project.
I'm familiar with ListView control and also with groups items, but I'm not familiar with custom EventArgs....
BillWoodruff 27-Mar-14 11:53am    
Okay, I am going to leave to you the work of getting steps 1~6 implemented: making the UserControl, getting its expand/collapse behavior implemented. I will post some code to assist you with the raising of an event when the ListView index changes.
JayantaChatterjee 28-Mar-14 8:20am    
I'm not able to give quick reply for my Internet connection.
Sorry for that... :-(
Thanks for giving custom Events code, and I already made the expand / Collapse behavior for my userControl...
I'm trying to make that control.
Maybe this blogpost can help you: GroupedComboBox[^]
the standard ComboBox control has no provision for groups or categories; it is designed to be a flat list with no implied cohesion between the list items, and no hierarchy.

I don't know if DevExpress can do it.
I wouldn't buy something like that just for one control
 
Share this answer
 
Comments
JayantaChatterjee 27-Mar-14 1:46am    
thanks @ BELGIUMsky !!
I tried GroupedComboBox custom control, but I couldn't add group wise items in that control...
BELGIUMsky 27-Mar-14 3:36am    
Did you follow the example given on that page?
JayantaChatterjee 27-Mar-14 11:16am    
Yes!
That's works...
thanks... :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900