C#为ComboBox等数组型控件设置自定义数据

ComboBox、ListBox、CheckedListBox等列表型控件,可以单独为每个Item设置显示文本和数据。

为此,我们定义一个类,来实现这个Item的文本显示和数据关联:

    public class ListComponentItem
    {
        private string _text = null;
        private object _value = null;

        public string Text { get => _text; set => _text = value; }
        public object Value { get => _value; set => _value = value; }

        public ListComponentItem(string text, object value = null)
        {
            Text = text;
            Value = value;
        }
        public override string ToString()
        {
            return _text;
        }
    }

在使用时,只需初始化控件的Items即可:

foreach (var pFeatureLayer in lstPolygonFeatureLayer)
{
    comboBoxPolygonLayer.Items.Add(new ListComponentItem(pFeatureLayer.Name, pFeatureLayer.FeatureClass));
}

或:

lstPolygonFeatureLayer.ForEach(e => comboBoxPolygonLayer.Items.Add(new ListComponentItem(e.Name, e.FeatureClass)));

那么,当我们选中某个Item时,通过如下方式取得其中的数据:

var pFeatureClass = (IFeatureClass)((ListComponentItem)comboBoxPolygonLayer.SelectedItem).Value;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 在C#中,可以使用数组和循环来生成一组CheckBox控件,并将已知的一个数组初始化到CheckBox的Text属性中。以下是一个示例代码: ```csharp Boolean[] 数据组 = { true, false, false, true, false, true }; CheckBox[] 控件组 = Array.ConvertAll(数据组, 控件 => new CheckBox()); int 数 = 数据组.Length; foreach (CheckBox 多选 in 控件组) { 多选.Text = (数--).ToString("多选00"); 多选.Name = 数.ToString("多选00"); 多选.Parent = this; 多选.Anchor = (AnchorStyles.Top | AnchorStyles.Left); 多选.Location = new Point(100 * 数, 22); 多选.Checked = 数据组<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [c# 代码生成6个checkbox 并初始化已知的一个数组到checkboxtext内](https://blog.csdn.net/xianfajushi/article/details/41645137)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [C#ComboBox数组控件设置自定义数据](https://blog.csdn.net/a_dev/article/details/83302337)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值