ItemData在。net

下载ItemData - 33,306 KB 。net中的ItemData,在visual studio 6.0中,ItemData可以将附加值绑定到组合框和列表框中,但在。net中没有那么怎么做呢? Visual studio 6.0:为组合框或列表框控件中的每个项返回/设置一个特定的数字。 使用的代码 你在。net中寻找条目数据属性来存储附加信息吗 (值)与值在组合框或列表框?这个属性在。net中是不可用的,那么怎么做呢?在这里。net有它的魔力,在。net组合框或列表框中,每一件东西都被当作对象,添加方法以参数作为对象,这样我们就可以创建自己的自定义类或结构,并绑定到可以容纳任意数量项目的 控件。 还是不清楚,让我们看看代码: 隐藏,收缩,复制Code

#region Itemdata

    public struct ItemData
    {
        public int EmpID;
        public string EmpName;
        public double Salary;

        public ItemData(int _EmpID, string _EmpName, double _Salary)
        {
            EmpID = _EmpID;
            EmpName = _EmpName;
            Salary = _Salary;
        }

        public override string ToString()
        {
            return this.EmpName;
        }
    }
    #endregion


//Load the values into Combo Box and List Box with the help of 
//Custom structure

private void frmItemData_Load(object sender, EventArgs e)
        {
            listBoxEmp.Items.Clear();
            listBoxEmp.Items.Add(new ItemData(10, "Raja", 20000));
            listBoxEmp.Items.Add(new ItemData(20, "Sekar", 40000));
            listBoxEmp.Items.Add(new ItemData(30, "kumar", 60000));
            comboEmp.Items.Clear();
            comboEmp.Items.Add(new ItemData(10,"Raja",20000));
            comboEmp.Items.Add(new ItemData(20, "Sekar", 40000));
            comboEmp.Items.Add(new ItemData(30, "kumar", 60000));
        }

//Now the Value is loaded into List box and Combo box How do you access it //while selecting the item see the below code

//When select the values in Combo box Messagebox will show the Itemdata
private void comboEmp_SelectedIndexChanged(object sender, EventArgs e)
        {
            MessageBox.Show("Emp Name: " + ((ItemData)comboEmp.SelectedItem).EmpName + "  Emp ID: " + ((ItemData)comboEmp.SelectedItem).EmpID.ToString() + "  Salary:" + ((ItemData)comboEmp.SelectedItem).Salary.ToString());
        }

//When select the values in ListBox Messagebox will show the Itemdata


        private void listBoxEmp_SelectedIndexChanged(object sender, EventArgs e)
        {

            MessageBox.Show("Emp Name: " + ((ItemData)listBoxEmp.SelectedItem).EmpName + "  Emp ID: " + ((ItemData)listBoxEmp.SelectedItem).EmpID.ToString() + "  Salary:" + ((ItemData)listBoxEmp.SelectedItem).Salary.ToString());
        }

这是一篇关于。net中itemdata的初始文章,我将继续更新这个线程。 本文转载于:http://www.diyabc.com/frontweb/news385.html

原文地址:https://www.cnblogs.com/Dincat/p/13443969.html