ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

CodeGo.net>使ComboBox只接受特定类型

2019-11-21 08:06:58  阅读:274  来源: 互联网

标签:combobox c net winforms


我目前在Windows窗体应用程序中有一个ComboBox.为了指定ComboBox将包含哪些值,我将ComboBox的DataSource属性设置为某个数组,以便ComboBox包含该数组中的值.我还可以使用Items.Add()将新值添加到ComboBox.但是,我想确保ComboBox可以填充某些特定类型的对象.因此,如果我有一个名为X的类,那么我想制作它,以便仅将类型X的数组用作ComboBox的数据源.现在,ComboBox接受类型为System.Object的对象.我该如何实现?是否需要将ComboBox的属性设置为等于数据类型的名称?还是有一个事件将检查添加到我的ComboBox的对象是否为所需类型,否则将引发异常?

我正在考虑创建一个新类作为ComboBox的子类型,并重写Items属性的Add方法,以便Add检查其参数是否为所需的类型(不确定是否以及如何实现).即使我这样做,仍然有其他方法可以将新值添加到ComboBox(AddRange,CopyTo等),因此我认为应该对此问题有一个更优雅的解决方案.

解决方法:

您可以按自己的方式隐藏Items属性
自定义类型的自己的Items属性,将原始ItemsCollection作为参数

测试示例类

public class Order
{
    public Int32 ID { get; set; }
    public string Reference { get; set; }

    public Order() { }
    public Order(Int32 inID, string inReference)
    {
        this.ID = inID;
        this.Reference = (inReference == null) ? string.Empty : inReference;
    }

    //Very important 
    //Because ComboBox using .ToString method for showing Items in the list
    public override string ToString()
    {
        return this.Reference;
    }

}

在下一堂课中,我尝试将ComboBox的items集合包装为自己的类型.
添加项目必须是具体类型的地方
在这里您可以添加所需的其他方法/属性(删除)

public class ComboBoxList<TCustomType>
{
    private System.Windows.Forms.ComboBox.ObjectCollection _baseList;

    public ComboBoxList(System.Windows.Forms.ComboBox.ObjectCollection baseItems)
    {
        _baseList = baseItems;
    }

    public TCustomType this[Int32 index]
    {
        get { return (TCustomType)_baseList[index]; }
        set { _baseList[index] = value; }
    }

    public void Add(TCustomType item)
    {
        _baseList.Add(item);
    }

    public Int32 Count { get { return _baseList.Count; } }

}

这里自定义ComboBox的组合框类
补充:通用类型

public class ComboBoxCustomType<TCustomType> : System.Windows.Forms.ComboBox
{
    //Hide base.Items property by our wrapping class
    public new ComboBoxList<TCustomType> Items; 

    public ComboBoxCustomType() : base()
    {
        this.Items = new ComboBoxList<TCustomType>(base.Items);
    }

    public new TCustomType SelectedItem 
    { 
        get { return (TCustomType)base.SelectedItem; } 
    }
}

表单中使用的下一个代码

private ComboBoxCustomType<Order> _cmbCustom;

//this method used in constructor of the Form
private void ComboBoxCustomType_Initialize()
{
    _cmbCustom = new ComboBoxCustomType<Order>();
    _cmbCustom.Location = new Point(100, 20);
    _cmbCustom.Visible = true;
    _cmbCustom.DropDownStyle = ComboBoxStyle.DropDownList;
    _cmbCustom.Items.Add(new Order(0, " - nothing - "));
    _cmbCustom.Items.Add(new Order(1, "One"));
    _cmbCustom.Items.Add(new Order(2, "Three"));
    _cmbCustom.Items.Add(new Order(3, "Four"));
    _cmbCustom.SelectedIndex = 0;
    this.Controls.Add(_cmbCustom);
}

标签:combobox,c,net,winforms
来源: https://codeday.me/bug/20191121/2050494.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有