关注我 么么哒

【net】Windows程序设计(3-4)

  • 作者:smile
  • 学无止境
  • 时间:Sun May 10 2020 13:48:09 GMT+0800 (China Standard Time)
  • 198人已阅读
简介 .net实验四第3.4题解析

第三题

所需控件:Form3 groupBox1 groupBox2 label1 label2 label3 label4 label5 textBox1 textBox2 textBox3 textBox4 textBox5 comboBox1 button1

  • 题目描述

创建Windows窗体应用程序,用一个学生结构数组存放10名学生的记录,然后根据用户指定的学号显示相应的学生记录,具体界面如下所示

  • 参考图
  • 参考代码
using System;
using System.Data;
using System.Windows.Forms;

namespace test
{
    public partial class Form3 : Form
    {
        DataTable ADt = new DataTable();
        Student[] students = { new Student(1, "小明", "男", "12345", "2020/05/10"),
                               new Student(2, "小红", "女", "12335", "2020/05/06"),
                               new Student(3, "小刚", "男", "10345", "1990/05/10"),
                               new Student(4, "小亮", "男", "12145", "1995/03/10"),
                               new Student(5, "小文", "男", "15845", "1998/08/10"),
                               new Student(6, "小花", "女", "13345", "1995/02/23"),
                               new Student(7, "小才", "男", "12314", "1999/05/20"),
                               new Student(8, "小雯", "女", "12355", "1996/04/11"),
                               new Student(9, "小牛", "男", "12348", "20008/05/15"),
                               new Student(10, "小华", "男", "12845", "1978/05/30")
                             };
        public Form3()
        {
            InitializeComponent();
            DoBindDataSource();
        }

        private void DoBindDataSource()
        {
            DataColumn ADC1 = new DataColumn("stu_ID", typeof(int));
            DataColumn ADC2 = new DataColumn("stu_Name", typeof(string));
            DataColumn ADC3 = new DataColumn("stu_Sex", typeof(string));
            DataColumn ADC4 = new DataColumn("stu_Class", typeof(string));
            DataColumn ADC5 = new DataColumn("stu_Birth", typeof(string));
            ADt.Columns.Add(ADC1);
            ADt.Columns.Add(ADC2);
            ADt.Columns.Add(ADC3);
            ADt.Columns.Add(ADC4);
            ADt.Columns.Add(ADC5);
            for (int i = 0; i < students.Length; i++)
            {
                DataRow ADR = ADt.NewRow();
                ADR[0] = students[i].id;
                ADR[1] = students[i].name;
                ADR[2] = students[i].sex;
                ADR[3] = students[i].classId;
                ADR[4] = students[i].birth;
                ADt.Rows.Add(ADR);
            }
            comboBox1.DisplayMember = "stu_ID";
            comboBox1.ValueMember = "stu_ID";
            comboBox1.DataSource = ADt;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int id = int.Parse(this.comboBox1.Text);
            DataRow[] dr = ADt.Select("stu_ID='" + id +"'");
            foreach (DataRow d in dr)
            {
                textBox1.Text = d["stu_ID"].ToString();
                textBox2.Text = d["stu_Name"].ToString();
                textBox3.Text = d["stu_Sex"].ToString();
                textBox4.Text = d["stu_Class"].ToString();
                textBox5.Text = d["stu_Birth"].ToString();
            }
            
        }
    }
    public class Student
    {
        public int id;
        public string name;
        public string sex;
        public string classId;
        public string birth;
        public Student(int id, string name, string sex, string classId, string birth)
        {
            this.id = id;
            this.name = name;
            this.sex = sex;
            this.classId = classId;
            this.birth = birth;
        }
    }
}

第四题

所需控件:Form4 treeView1 listView1 imageList1

  • 题目描述

创建一个项目,设计一个窗体Form1,其中包含一个TreeView控件treeView1和一个ListView控件listView1,单击treeView1控件中的某结点时,在listView1中显示所有子结点,并通过弹出式菜单选择listView1控件的大图标、小图标、列表和完整图标4种视图显示模式

  • 参考图
  • 参考代码
using System;
using System.Windows.Forms;

namespace test
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();

            treeView1.Nodes.Add("哺乳动物");
          
             treeView1.Nodes[0].Nodes.Add("鱼类");
             treeView1.Nodes[0].Nodes[0].Nodes.Add("鲨鱼");
             treeView1.Nodes[0].Nodes[0].Nodes.Add("热带鱼");
             treeView1.Nodes[0].Nodes[0].Nodes.Add("金鱼");
             treeView1.Nodes[0].Nodes.Add("鸟类");
             treeView1.Nodes[0].Nodes[1].Nodes.Add("麻雀");
             treeView1.Nodes[0].Nodes[1].Nodes.Add("鸽子");

             listView1.SmallImageList = imageList1;
             
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            //MessageBox.Show(treeView1.SelectedNode.Text);
            if (treeView1.SelectedNode.Text == "哺乳动物")
            {
                listView1.Clear();
                ListViewItem lv1 = new ListViewItem();
                lv1.ImageIndex = 0;
                lv1.Text = "鲨鱼";
                listView1.Items.Add(lv1);
                ListViewItem lv2 = new ListViewItem();
                lv2.ImageIndex = 1;
                lv2.Text = "热带鱼";
                listView1.Items.Add(lv2);
                ListViewItem lv3 = new ListViewItem();
                lv3.ImageIndex = 2;
                lv3.Text = "金鱼";
                listView1.Items.Add(lv3);
                ListViewItem lv4 = new ListViewItem();
                lv4.ImageIndex = 3;
                lv4.Text = "麻雀";
                listView1.Items.Add(lv4);
                ListViewItem lv5 = new ListViewItem();
                lv5.ImageIndex = 4;
                lv5.Text = "鸽子";
                listView1.Items.Add(lv5);
            }
            else if (treeView1.SelectedNode.Text == "鱼类")
            {
                listView1.Clear();
                ListViewItem lv1 = new ListViewItem();
                lv1.ImageIndex = 0;
                lv1.Text = "鲨鱼";
                listView1.Items.Add(lv1);
                ListViewItem lv2 = new ListViewItem();
                lv2.ImageIndex = 1;
                lv2.Text = "热带鱼";
                listView1.Items.Add(lv2);
                ListViewItem lv3 = new ListViewItem();
                lv3.ImageIndex = 2;
                lv3.Text = "金鱼";
                listView1.Items.Add(lv3);
            }
            else if (treeView1.SelectedNode.Text == "鲨鱼")
            {
                listView1.Clear(); 
                ListViewItem lv = new ListViewItem();
                lv.ImageIndex = 0;
                lv.Text = "鲨鱼";
                listView1.Items.Add(lv);
            }
            else if (treeView1.SelectedNode.Text == "热带鱼")
            {
                listView1.Clear();
                ListViewItem lv = new ListViewItem();
                lv.ImageIndex = 1;
                lv.Text = "热带鱼";
                listView1.Items.Add(lv);
            }
            else if (treeView1.SelectedNode.Text == "金鱼")
            {
                listView1.Clear();
                ListViewItem lv = new ListViewItem();
                lv.ImageIndex = 2;
                lv.Text = "金鱼";
                listView1.Items.Add(lv);
            }
            else if (treeView1.SelectedNode.Text == "鸟类")
            {
                listView1.Clear();
                ListViewItem lv1 = new ListViewItem();
                lv1.ImageIndex = 3;
                lv1.Text = "麻雀";
                listView1.Items.Add(lv1);
                ListViewItem lv2 = new ListViewItem();
                lv2.ImageIndex = 4;
                lv2.Text = "鸽子";
                listView1.Items.Add(lv2);
            }
            else if (treeView1.SelectedNode.Text == "麻雀")
            {
                listView1.Clear();
                ListViewItem lv1 = new ListViewItem();
                lv1.ImageIndex = 3;
                lv1.Text = "麻雀";
                listView1.Items.Add(lv1);
            }
            else if (treeView1.SelectedNode.Text == "鸽子")
            {
                listView1.Clear();
                ListViewItem lv2 = new ListViewItem();
                lv2.ImageIndex = 4;
                lv2.Text = "鸽子";
                listView1.Items.Add(lv2);
            }
        }
    }
}
 

文章评论