1、创建Windows窗体应用程序,实现用户登录功能,当输入正确与错误时均给出相应的提示信息,规定用户输入错误次数不能超过3次。(源代码+运行界面)
using System;
using System.Windows.Forms;
namespace Test4_1
{
public partial class Form1 : Form
{
public int count = 0;
public string userName = "xxgcxy";
public string password = "zhaoqi";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (count < 3)
{
var userName = textBox1.Text.ToString();
var password = textBox2.Text.ToString();
if (userName == this.userName && password == this.password)
{
count = 0;
MessageBox.Show("登录成功!", "提示");
}
else
{
count++;
MessageBox.Show("账号或密码输入错误,请重新登录!!!", "提示");
}
}
else
{
MessageBox.Show("输入错误已达3次,请点击取消后重试!", "提示");
}
}
private void button2_Click(object sender, EventArgs e)
{
count = 0;
textBox1.Text = null;
textBox2.Text = null;
MessageBox.Show("已重置", "提示");
}
}
}
运行结果:
注意点:错误次数不能超过三次,所以要加一个错误次数的判断。
2、创建Windows窗体应用程序,界面如下所示,当在组合框中输入一个新项时自动添加到组合框中,并给出相应提示;当输入一个已存在项时给出相应提示。(源代码+运行界面)
using System;
using System.Windows.Forms;
namespace Test4_2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("上海");
comboBox1.Items.Add("平顶山");
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show("您选择的城市是:" + comboBox1.Text, "提示");
}
private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData.Equals(System.Windows.Forms.Keys.Enter))
{
if (e.KeyCode == Keys.Enter)
{
if (comboBox1.Items.Contains(comboBox1.Text))
{
label2.Text = "你的输入已在组合框!";
}
else
{
comboBox1.Items.Add(comboBox1.Text);
label2.Text = "你的输入项已添加到组合框中!";
}
}
}
}
}
}
运行结果:
3、创建Windows窗体应用程序,用一个学生结构数组存放10名学生的记录,然后根据用户指定的学号显示相应的学生记录,具体界面如下所示。(源代码+运行界面)
参考答案:
namespace experment9
{
public partial class Form1 : Form
{
struct StudType
{
public int no;
public string name;
public string sex;
public string sclass;
public DateTime rq;
}
StudType[] stud = new StudType[10]; //定义一个结构类型的数组
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//给定义的数组赋初值
stud[0].no = 1; stud[0].name = "王华"; stud[0].sex = "男";
stud[0].rq = new DateTime(1980, 2, 10); stud[0].sclass = "99091";
stud[1].no = 2; stud[1].name = "李强"; stud[1].sex = "男";
stud[1].rq = new DateTime(1981, 10, 4); stud[1].sclass = "99101";
stud[2].no = 3; stud[2].name = "张丽"; stud[2].sex = "女";
stud[2].rq = new DateTime(1980, 3, 2); stud[2].sclass = "99091";
stud[3].no = 4; stud[3].name = "汪洋"; stud[3].sex = "男";
stud[3].rq = new DateTime(1980, 1, 5); stud[3].sclass = "99101";
stud[4].no = 5; stud[4].name = "江华"; stud[4].sex = "男";
stud[4].rq = new DateTime(1980, 3, 10); stud[4].sclass = "99091";
stud[5].no = 6; stud[5].name = "李英"; stud[5].sex = "女";
stud[5].rq = new DateTime(1980, 6, 2); stud[5].sclass = "99101";
stud[6].no = 7; stud[6].name = "胡军"; stud[6].sex = "男";
stud[6].rq = new DateTime(1981, 10, 9); stud[6].sclass = "99091";
stud[7].no = 8; stud[7].name = "刘驰"; stud[7].sex = "女";
stud[7].rq = new DateTime(1982, 5, 2); stud[7].sclass = "99101";
stud[8].no = 9; stud[8].name = "宋仁"; stud[8].sex = "男";
stud[8].rq = new DateTime(1980, 8, 3); stud[8].sclass = "99101";
stud[9].no = 10; stud[9].name = "许兵"; stud[9].sex = "男";
stud[9].rq = new DateTime(1980, 11, 8); stud[9].sclass = "99091";
//将学号都添加到组合框中
for (int i = 0; i < 10; i++)
comboBox1.Items.Add(stud[i].no);
}
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.Text != "")
{
int i = 0; //查找指定学号的学生记录
while (Convert.ToInt32(comboBox1.Text) != stud[i].no)
i = i + 1;
if (i >= 10)
MessageBox.Show("没有该学号的记录");
else
{
textBox1.Text = stud[i].no.ToString(); //显示找到的学生记录
textBox2.Text = stud[i].name;
textBox3.Text = stud[i].sex;
textBox4.Text = stud[i].sclass;
textBox5.Text = stud[i].rq.ToString();
}
}
}
}
}
在这里,我认为老师最终提供的答案简单一点,放上来和大家一起分享
以下是我的设计过程:
using System;
using System.Windows.Forms;
namespace Test4_3
{
public partial class Form1 : Form
{
struct Students
{
public int stu_ID;
public string stu_name;
public string stu_sex;
public int class_ID;
public string stu_birthday;
}
Students[] student = new Students[10];
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Students student1, student2, student3, student4, student5, student6, student7, student8, student9, student10;
student1.stu_ID = 1; student1.stu_name = "琦琦"; student1.stu_sex = "女"; student1.class_ID = 1702; student1.stu_birthday = "1998/09/15";
student2.stu_ID = 2; student2.stu_name = "森森"; student2.stu_sex = "男"; student2.class_ID = 1702; student2.stu_birthday = "1998/06/23";
student3.stu_ID = 3; student3.stu_name = "张丽"; student3.stu_sex = "女"; student3.class_ID = 1702; student3.stu_birthday = "1997/05/20";
student4.stu_ID = 4; student4.stu_name = "美美"; student4.stu_sex = "女"; student4.class_ID = 1702; student4.stu_birthday = "1999/02/10";
student5.stu_ID = 5; student5.stu_name = "康康"; student5.stu_sex = "男"; student5.class_ID = 1702; student5.stu_birthday = "1997/05/17";
student6.stu_ID = 6; student6.stu_name = "Mary"; student6.stu_sex = "女"; student6.class_ID = 1702; student6.stu_birthday = "1998/05/12";
student7.stu_ID = 7; student7.stu_name = "小雪"; student7.stu_sex = "女"; student7.class_ID = 1702; student7.stu_birthday = "1997/10/25";
student8.stu_ID = 8; student8.stu_name = "小涛"; student8.stu_sex = "男"; student8.class_ID = 1702; student8.stu_birthday = "1997/06/20";
student9.stu_ID = 9; student9.stu_name = "小明"; student9.stu_sex = "男"; student9.class_ID = 1702; student9.stu_birthday = "1998/12/01";
student10.stu_ID = 10; student10.stu_name = "小红"; student10.stu_sex = "女"; student10.class_ID = 1702; student10.stu_birthday = "1999/07/23";
int i = int.Parse(comboBox1.SelectedItem.ToString());
if (i == 1)
{
textBox1.Text = student1.stu_ID.ToString();
textBox2.Text = student1.stu_name;
textBox3.Text = student1.stu_sex;
textBox4.Text = student1.class_ID.ToString();
textBox5.Text = student1.stu_birthday;
}
else if (i == 2)
{
textBox1.Text = student2.stu_ID.ToString();
textBox2.Text = student2.stu_name;
textBox3.Text = student2.stu_sex;
textBox4.Text = student2.class_ID.ToString();
textBox5.Text = student2.stu_birthday;
}
else if (i == 3)
{
textBox1.Text = student3.stu_ID.ToString();
textBox2.Text = student3.stu_name;
textBox3.Text = student3.stu_sex;
textBox4.Text = student3.class_ID.ToString();
textBox5.Text = student3.stu_birthday;
}
else if (i == 4)
{
textBox1.Text = student4.stu_ID.ToString();
textBox2.Text = student4.stu_name;
textBox3.Text = student4.stu_sex;
textBox4.Text = student4.class_ID.ToString();
textBox5.Text = student4.stu_birthday;
}
else if (i == 5)
{
textBox1.Text = student5.stu_ID.ToString();
textBox2.Text = student5.stu_name;
textBox3.Text = student5.stu_sex;
textBox4.Text = student5.class_ID.ToString();
textBox5.Text = student5.stu_birthday;
}
else if (i == 6)
{
textBox1.Text = student6.stu_ID.ToString();
textBox2.Text = student6.stu_name;
textBox3.Text = student6.stu_sex;
textBox4.Text = student6.class_ID.ToString();
textBox5.Text = student6.stu_birthday;
}
else if (i == 7)
{
textBox1.Text = student7.stu_ID.ToString();
textBox2.Text = student7.stu_name;
textBox3.Text = student7.stu_sex;
textBox4.Text = student7.class_ID.ToString();
textBox5.Text = student7.stu_birthday;
}
else if (i == 8)
{
textBox1.Text = student8.stu_ID.ToString();
textBox2.Text = student8.stu_name;
textBox3.Text = student8.stu_sex;
textBox4.Text = student8.class_ID.ToString();
textBox5.Text = student8.stu_birthday;
}
else if (i == 9)
{
textBox1.Text = student9.stu_ID.ToString();
textBox2.Text = student9.stu_name;
textBox3.Text = student9.stu_sex;
textBox4.Text = student9.class_ID.ToString();
textBox5.Text = student9.stu_birthday;
}
else if (i == 10)
{
textBox1.Text = student10.stu_ID.ToString();
textBox2.Text = student10.stu_name;
textBox3.Text = student10.stu_sex;
textBox4.Text = student10.class_ID.ToString();
textBox5.Text = student10.stu_birthday;
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
运行结果:
4、创建一个项目,设计一个窗体Form1,其中包含一个TreeView控件treeView1和一个ListView控件listView1,单击treeView1控件中的某结点时,在listView1中显示所有子结点,并通过弹出式菜单选择listView1控件的大图标、小图标、列表和完整图标4种视图显示模式。如下图所示:(源代码+运行界面)
待实现效果图:
using System;
using System.Windows.Forms;
namespace Test4_4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
treeView1.Nodes.Add("哺乳动物");
treeView1.Nodes.Add("鱼类");
treeView1.Nodes.Add("鸟类");
treeView1.Nodes[0].Nodes.Add("小狗");
treeView1.Nodes[0].Nodes.Add("小熊");
treeView1.Nodes[0].Nodes.Add("绵羊");
treeView1.Nodes[0].Nodes.Add("大象");
treeView1.Nodes[1].Nodes.Add("带鱼");
treeView1.Nodes[1].Nodes.Add("鲨鱼");
treeView1.Nodes[2].Nodes.Add("喜鹊");
treeView1.Nodes[2].Nodes.Add("鹦鹉");
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
listView1.Items.Clear();
listView1.LargeImageList = imageList1; //大图标
listView1.SmallImageList = imageList2; //小图标
listView1.Items.Clear();
string s = e.Node.Text;
if (s == "哺乳动物")
{
listView1.Clear();
listView1.View = View.LargeIcon;
listView1.Items.Add("小狗",0);
listView1.Items.Add("小熊",1);
listView1.Items.Add("绵羊",2);
listView1.Items.Add("大象",3);
}
else if (s == "鱼类")
{
listView1.Clear();
listView1.View = View.LargeIcon;
listView1.Items.Add("带鱼");
listView1.Items.Add("鲨鱼");
}
else if (s == "鸟类")
{
listView1.Clear();
listView1.View = View.LargeIcon;
listView1.Items.Add("喜鹊");
listView1.Items.Add("鹦鹉");
}
else if (s == "小狗")
{
listView1.Clear();
listView1.View = View.SmallIcon;
listView1.Items.Add("小狗",1);
}
else if (s == "小熊")
{
listView1.Clear();
listView1.View = View.SmallIcon;
listView1.Items.Add("小熊",0);
}
else if (s == "绵羊")
{
listView1.Clear();
listView1.View = View.SmallIcon;
listView1.Items.Add("绵羊",2);
}
else if (s == "大象")
{
listView1.Clear();
listView1.View = View.SmallIcon;
listView1.Items.Add("大象",3);
}
}
private void menu1_click(object sender, EventArgs e)
{
listView1.View = View.LargeIcon;
}
private void menu2_click(object sender, EventArgs e)
{
listView1.View = View.SmallIcon ;
}
private void menu3_click(object sender, EventArgs e)
{
listView1.View = View.List;
}
private void menu4_click(object sender, EventArgs e)
{
listView1.View = View.Tile;
}
}
}
实现效果:
由于近期时间紧张,文章内容控件设计部分写的不太详细,后期会加以完善!