如何在C#中的ComboBox中创建组项? [英] how to create group items in ComboBox in C#?

查看:382
本文介绍了如何在C#中的ComboBox中创建组项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建可以对项目进行分组的自定义组合框(如HTML dropDownList)??

我在谷歌搜索,但我找不到合适的例子..

是否可以使用 DevExpress 控制?



任何建议都会受到赞赏....



问候

Jayanta ...

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

推荐答案

如果你愿意这样做在创建UserControl时,您可以将支持Groups的ListView与TextBox结合使用,以显示所选的List Item。这实际上很容易,但是,如果您对C#和Windows Forms相对较新,并且没有使用UserControls的经验,我建议您在开始使用之前仔细分析您的可用时间和动机。



策略是这样的:



1.创建一个新的UserControl



2.将ListView放在底部停靠在底部



3.将一个TextBox放在底座上。



4.为TextBox选择一个字体,以便它显示必要的列,以显示项目。



5让设计看起来正确。



6.创建必要的逻辑:



a。最初呈现UserControl,因此它的高度等于TextBox的高度。



b。当TextBox获得Click时,将UserControl的大小调整为设计模式下的完整高度。这将显示ListView及其组和项目。



c。选择ListView中的Item时,将该Item的Text放入TextBox中,并将UserControl高度设置回TextBox的高度。



Of当然,你必须公开UserControl的消费者使用UserControl所需的任何数据。



如果你真的对此感兴趣,并且有时间努力工作,让我知道,我将发布一个粗略的代码草图。



...编辑#1 ... <当选择ListViewItem时,从UserControl引发自定义事件:假设带有ListView和TextBox的UserControl被命名为'LVCombo。



a。定义一个自定义EventArgs类来保存引发事件时生成的数据:
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:
public class LVEventArgs : EventArgs
{
    public int CurrentIndex;
    public string CurrentText;

    public LVEventArgs(int i, string s)
    {
        CurrentIndex = i;
        CurrentText = s;
    }
}

b。定义自定义事件:

b. define the custom Event:

// 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。在ListView SelectedIndexChanged EventHandler中创建要传输的自定义数据,并调用事件:

c. in the ListView SelectedIndexChanged EventHandler create the custom data to be transmitted, and invoke the Event:

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。从承载UserControl的表单订阅自定义事件的示例:



...假设表单上的UserControl名为'lvCombo1 ...

8. Example of subscribing to the custom Event from the Form that hosts the UserControl:

... assume the UserControl on the Form is named 'lvCombo1 ...

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

请注意步骤#7b。我们使用.NET 2.0提供的泛型事件语法;这个语法允许我们定义必要的事件管道而不定义委托。



理解创建,提升和消费的好资源C#中的事件:在CodeProject上: [ ^ ,在MSDN上:[ ^ ],StackOverFlow:[ ^ ]



...结束编辑#1 ...

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


也许这篇博文可以帮到你: GroupedComboBox [ ^ ]

标准梳子oBox控件没有针对组或类别的规定;它被设计成一个平面列表,列表项之间没有隐含的内聚,没有层次结构。



我不知道DevExpress是否可以做到这一点。 />
我不会为一个控件购买类似的东西
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


这篇关于如何在C#中的ComboBox中创建组项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆