设计一个Windows应用程序,定义一个Teacher类,包含姓名和职称两个字段和一个输出自己信息的方法,并用ArrayList实现与实例6-1相同的功能。
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
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 button2_Click(object sender, EventArgs e)
{
if (a.Count != 0)
{
label4.Text = "";
display();
}
else
{
MessageBox.Show("没有教师信息!");
}
}
private void button3_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);
}
}
}
索引插入:
索引删除:
版权声明:本文为weixin_44927247原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。