c#中怎么把Combobox中选中的在label中显示出来。。求完整代码

给跪了... 给跪了 展开
 我来答
百度网友2951faa
2014-09-29
知道答主
回答量:21
采纳率:0%
帮助的人:19.6万
展开全部

已测试过,OK,望采纳。

        private void Form1_Load(object sender, EventArgs e)
        {
            var aList = ComboBoxItem.GetList();

            comboBox1.DataSource = aList;
            comboBox1.DisplayMember = "Text";
            comboBox1.ValueMember = "Value";

            var aComboBoxItem = comboBox1.SelectedItem as ComboBoxItem;
            if (aComboBoxItem != null)
            {
                var aValue = aComboBoxItem.Value;
                var aText = aComboBoxItem.Text;

                label1.Text = aText;
            }
        }

        private class ComboBoxItem
        {
            public string Value { get; set; }
            public string Text { get; set; }

            public static List<ComboBoxItem> GetList()
            {
                var aList = new List<ComboBoxItem>();
                for (int i = 0; i < 10; i++)
                {
                    var aComboBoxItem = new ComboBoxItem();
                    aComboBoxItem.Text = string.Format("Text_{0}", i);
                    aComboBoxItem.Value = string.Format("Value_{0}", i);

                    aList.Add(aComboBoxItem);
                }
                return aList;
            } 
        }
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式