comboxのDispalyMemberとValueMember属性のテスト

4751 ワード

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;



namespace DisplayMemberValueMember

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }





        private void Form1_Load(object sender, EventArgs e)

        {

            Module m1=new Module();

            m1.ID=1;

            m1.DISPLAY="v1";



            Module m2=new Module();

            m2.ID=2;

            m2.DISPLAY="v2";

            List<Module> lm = new List<Module>();

            lm.Add(m1);

            lm.Add(m2);



            comboBox1.DataSource = lm;

            comboBox1.DisplayMember = "ID";

            comboBox1.ValueMember = "DISPLAY";



        }



        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

        {

            textBox1.Text = ((Module)comboBox1.SelectedItem).DISPLAY;

            textBox2.Text = ((Module)comboBox1.SelectedItem).ID.ToString();

        }

    }



    public class Module

    {

        public Module()

        {

        }



        int Id;

        string display;



        public int ID

        {

            set { Id = value; }

            get { return Id; }

        }



        public string DISPLAY

        {

            set { display = value; }

            get { return display; }

        }

    }

}