天天看點

C#——設計一個窗體程式,定義一個Teacher類,包含姓名和職稱兩個字段和一個輸出自己資訊的方法,并用ArrayList實作與對集合的增、删、插入和周遊功能。

1.設計一個窗體程式,定義一個Teacher類,包含姓名和職稱兩個字段和一個輸出自己資訊的方法,并用ArrayList實作與對集合的增、删、插入和周遊功能。

設計如下界面:

C#——設計一個窗體程式,定義一個Teacher類,包含姓名和職稱兩個字段和一個輸出自己資訊的方法,并用ArrayList實作與對集合的增、删、插入和周遊功能。

編寫代碼:

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;

using System.Collections; //引入命名空間

namespace 教師

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)

        {

            label4.Text = "";

        }

        ArrayList a = new ArrayList();

        public void display()

        { 

            foreach(object t in a)

            {

                Teacher x = (Teacher)t;

                label4.Text += "\n" + x.ShowMsg();

            }

        }

        //添加到末尾

        private void button1_Click(object sender, EventArgs e)

        {

            if (textBox1.Text != "" && textBox2.Text != "")

            {

                Teacher x = new Teacher(textBox1.Text, textBox2.Text);

                a.Add(x);

                label4.Text = "";

                display();

                textBox1.Text = "";

                textBox2.Text = "";

                textBox3.Text = "";

            }

            else

            {

                MessageBox.Show("請輸入完整的資訊進行添加!!");

            }

        }

        //周遊

        private void button3_Click(object sender, EventArgs e)

        {

            if (a.Count != 0)

            {

                label4.Text = "";

                display();

            }

            else

            {

                MessageBox.Show("沒有教師資訊!");

            }

        }

        //插入到

        private void button2_Click(object sender, EventArgs e)

        {

            try

            {

                Teacher t = new Teacher(textBox1.Text, textBox2.Text);

                a.Insert(Convert.ToInt32(textBox3.Text), t);

                label4.Text = "";

                display();

                textBox1.Text = "";

                textBox2.Text = "";

                textBox3.Text = "";

            }

            catch 

            {

                MessageBox.Show("請輸入完整資訊");

            }

        }

        //删除

        private void button4_Click(object sender, EventArgs e)

        {

            try 

            {

                if (a.Count != 0)

                {

                    a.RemoveAt(Convert.ToInt32(textBox3.Text));

                    label4.Text = "";

                    display();

                }

                else

                {

                    MessageBox.Show("沒有教師資訊!");

                }

            }

            catch 

            {

                MessageBox.Show("請正确輸入資訊");

            }

        }

    }

    public class Teacher

    {

        string name;

        string title;

        public Teacher(string name,string title)

        {

            this.name =name;

            this.title =title;

        }

        public string ShowMsg()

        {

            return string.Format ("姓名:{0},職稱:{1}",name,title);

        }

    }

}

運作結果:

C#——設計一個窗體程式,定義一個Teacher類,包含姓名和職稱兩個字段和一個輸出自己資訊的方法,并用ArrayList實作與對集合的增、删、插入和周遊功能。