.net 實驗四 Windows程序設計

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;
          }   

     }
}

 

實現效果:

   

 

由於近期時間緊張,文章內容控件設計部分寫的不太詳細,後期會加以完善!

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章