[C#] ตัวแปร Struct + ArrayList

โค้ด
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public struct MyStruct
        {
            public string name;
            public string tel;
            public string email;
        }

        private ArrayList myRecord = new ArrayList();

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "")
            {
                MessageBox.Show("You have to fill form.");
            }
            else
            {
                MyStruct x;
                x.name  = textBox1.Text;
                x.tel   = textBox2.Text;
                x.email = textBox3.Text;

                string strRec = x.name + " : " + x.tel + " : " + x.email;

                myRecord.Add(strRec);
                listBox1.Items.Add(strRec);

                textBox1.Text = "";
                textBox2.Text = "";
                textBox3.Text = "";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex == -1)
            {
                MessageBox.Show("You have to select one or nothing to remove.");
            }
            else
            {
                myRecord.RemoveAt(listBox1.SelectedIndex);
                listBox1.Items.RemoveAt(listBox1.SelectedIndex);
            }
        }

    }
}
Previous
Next Post »