控件显示内容的绑定类 combobox 为每一item 加tag

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace COMM.Type
{
    //组合text tag两种属性 实现tostring 用于自动显示的控件如 combobox
    public class TextTag : Object
    {
        public object _tag = null;
        public string _text = "";

        public object Tag
        {
            get
            {
                return _tag;
            }
            set
            {
                _tag = value;
            }
        }

        public string Text
        {
            get
            {
                return _text;
            }
            set
            {
                _text = value;
            }
        }

        public TextTag()
        {
        }

        public TextTag(string text, object tag)
        {
            this.Tag = tag;
            this.Text = text;
        }
        //显示的数据
        public override string ToString()
        {
            return Text;
        }
        //创建新的实例
        static public TextTag CreateNew(string text, object tag)
        {
            return new TextTag(text, tag);
        }

        //指定的数据的tag
        public T GetTag<T>()
        {
            return (T)_tag;
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace COMM.Type
{
    //给ComboBox的每一行附加tag的操作
    static public class CmbItemTag
    {
        //当前选择的数据的tag
        static public T GetCurrentTag<T>(System.Windows.Forms.ComboBox cmb)
        {
            return (T)((TextTag)(cmb.SelectedItem)).Tag;
        }
        
        //通过tag选择数据
        static public void SetCurrentDataByTag<T>(ComboBox cmb, Func<T, bool> funCmp)
        {
            foreach (var item in cmb.Items)
            {
                if (funCmp(((TextTag)item).GetTag<T>()))
                {
                    cmb.SelectedItem = item;
                    break;
                }
            }
        }
    }
}


    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 10; i++)
            {
                COMM.Type.TextTag item = COMM.Type.TextTag.CreateNew("t" + i.ToString(), i);
                comboBox1.Items.Add(item);
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox1.Text = COMM.Type.CmbItemTag.GetCurrentTag<int>(comboBox1).ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            COMM.Type.CmbItemTag.SetCurrentDataByTag<int>(comboBox1, (t) => t == Convert.ToInt32(textBox2.Text));
        }
    }

你可以使用以下步骤将枚举绑定combobox上,并实现选择combobox内容后重绘PlotPanel控件: 1. 在WinForms应用程序中,打开Form1.cs文件。 2. 在代码顶部添以下引用: ```csharp using System.ComponentModel; ``` 3. 在Form1中定义枚举: ```csharp public enum PlotType { Line, Scatter, Bar } ``` 4. 在Form1中添以下方法,该方法将枚举绑定combobox上: ```csharp private void BindPlotTypeComboBox() { BindingList<PlotType> plotTypes = new BindingList<PlotType>(); foreach (PlotType pt in Enum.GetValues(typeof(PlotType))) { plotTypes.Add(pt); } plotTypeComboBox.DataSource = plotTypes; } ``` 5. 在Form1的构造函数中调用BindPlotTypeComboBox方法: ```csharp public Form1() { InitializeComponent(); BindPlotTypeComboBox(); } ``` 6. 在Form1中添以下事件处理程序,该处理程序将在选择combobox内容时重绘PlotPanel控件: ```csharp private void plotTypeComboBox_SelectedIndexChanged(object sender, EventArgs e) { PlotType plotType = (PlotType)plotTypeComboBox.SelectedItem; switch (plotType) { case PlotType.Line: // 绘制线性图 break; case PlotType.Scatter: // 绘制散点图 break; case PlotType.Bar: // 绘制条形图 break; } } ``` 7. 在Form1设计器中,将combobox和PlotPanel控件到窗体上,并将combobox的SelectedIndexChanged事件绑定到plotTypeComboBox_SelectedIndexChanged方法。 现在,当你选择combobox内容时,将会触发plotTypeComboBox_SelectedIndexChanged方法,你可以在该方法中重绘PlotPanel控件,以显示所选枚举型的图表。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值