第一次C#作业

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ex5_5
{
    class Province
    {
        private string name;


        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private List<string> cities;


        public List<string> Cities
        {
            get { return cities; }
            set { cities = value; }
        }
        public Province(string name)
        {
            this.name = name;
            cities = new List<string>();
        }
    }

}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ex5_5
{
    [Serializable]
    class Student
    {
        
        private string name;


        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private int sex; // 0..男,1..女


        public int Sex
        {
            get { return sex; }
            set { sex = value; }
        }
        private DateTime birthDate;


        public DateTime BirthDate
        {
            get { return birthDate; }
            set { birthDate = value; }
        }
        private int provinceID, cityID; // 只记录省/市在ComboBox列表中的序号。


        public int CityID
        {
            get { return cityID; }
            set { cityID = value; }
        }


        public int ProvinceID
        {
            get { return provinceID; }
            set { provinceID = value; }
        }
// 补充代码编写构造函数、属性
      public Student(string na,int se,DateTime birth,int p,int c)
      {
          name = na;
          sex = se;
          birthDate = birth;
          provinceID = p;
          cityID = c;
      }


    }
}

using System;
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;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace ex5_5
{
    public partial class Form1 : Form
    {
        private const string fileName = "student.txt";
        int num = 0;//人数
        int sex = 0;//性别
        DateTime birth;//生日
        DateTime init;//初始化
        int indexp;//省
        int indexc;//市
        List<Province> prolist = new List<Province>();//省的list表
        List<Student>studentList=new List<Student>();
        int listindex;
        bool ischange = false;
        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            
            Student stu = new Student(textBox1.Text, sex, birth, indexp, indexc);
            studentList.Add(stu);
            listBoxStu.Items.Add(studentList[num].Name);
            num++;
            textBox1.Clear();//返回初始
            radioButton1.Checked = false;
            radioButton2.Checked = false;
            //comboBox2.Text = "  ";
            //comboBox3.Text = "  ";
            comboBox2.SelectedIndex = -1;
            comboBox3.SelectedIndex = -1;
            dateTimePicker1.Value = init;
           
            
        }


        private void Form1_Load(object sender, EventArgs e)
        {


            init = dateTimePicker1.Value;
            FileStream file;
            try
            {
                file = new FileStream("第二题城市数据.txt", FileMode.Open);
            }
            catch (Exception ex)
            {
                MessageBox.Show("打开文件失败" + ex.Message);
                return;
            }
            StreamReader reader = new StreamReader(file);
            int n = int.Parse(reader.ReadLine());//总数
            for (int i = 0; i < n; i++)
            {
                Province p = new Province(reader.ReadLine());//省名
                int m = int.Parse(reader.ReadLine());//有多少市
                for (int j = 0; j < m; j++)
                {
                    p.Cities.Add(reader.ReadLine());//市名
                }
                prolist.Add(p);
            }


                //Province p = new Province("广东");


                //p.Cities.Add("广州");
                //p.Cities.Add("中山");
                //p.Cities.Add("潮州");
                //prolist.Add(p);
                //p = new Province("江苏");


                //p.Cities.Add("南京");
                //p.Cities.Add("苏州");
                //p.Cities.Add("扬州");
                //prolist.Add(p);
                //p = new Province("浙江");


                //p.Cities.Add("杭州");
                //p.Cities.Add("宁波");
                //prolist.Add(p);
                foreach (Province pro in prolist)
                {
                    comboBox2.Items.Add(pro.Name);
                }
            FileInfo fi = new FileInfo(fileName);
            if (fi.Length != 0)
            {


                FileStream stream = null;
                try
                {
                    stream = new FileStream(fileName, FileMode.OpenOrCreate); // 如果文件不存在,此句执行失败,抛出异常
                }
                catch (Exception ex)
                {
                    MessageBox.Show("打开文件失败," + ex.Message); // 显示异常错误信息。
                    return;
                }
                BinaryFormatter bFormatter = new BinaryFormatter(); // 用BinaryFormatter类对象实现二进制反序列化
                studentList = (List<Student>)bFormatter.Deserialize(stream); // 此处请与“读二进制文件”的代码作对比,体会反序列化的优点
                stream.Close();
                // 将stuList的各个对象在listBox1上显示
                //listBoxStu.DataSource = studentList;
                //listBoxStu.DisplayMember = "Name";
                foreach (Student stu in studentList)
                {
                    listBoxStu.Items.Add(stu.Name);
                    num++;
                }


            }


        }


       


        private void listBoxStu_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ischange)
            {
                listBoxStu.SelectedIndex = listindex;
                ischange = false;
            }
            Student stu = studentList[listBoxStu.SelectedIndex];
            textBox1.Text = stu.Name;
            if (stu.Sex == 0)
            {
                radioButton1.Checked = true;
            }
            else if(stu.Sex==1)
            {
                radioButton2.Checked = true;
            }
            dateTimePicker1.Value = stu.BirthDate;
            comboBox2.SelectedIndex = stu.ProvinceID;
            
            if(stu.ProvinceID!=32&&stu.ProvinceID!=33)
            comboBox3.SelectedIndex = stu.CityID;


        }


        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            birth = dateTimePicker1.Value;
         
        }


        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                //studentList[num].Sex = 0;
                sex = 0;


            }
            else if(radioButton2.Checked)
            {
                //studentList[num].Sex = 1;
                sex = 1;
            }
        }


        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            indexp = comboBox2.SelectedIndex;
            comboBox3.Items.Clear();
            //comboBox3.SelectedIndex = -1;
            comboBox3.Text = "";
            if (comboBox2.SelectedIndex != -1)
            {
                foreach (string CityName in prolist[comboBox2.SelectedIndex].Cities)
                {
                    comboBox3.Items.Add(CityName);
                }
            }
        
          
        }


        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            indexc = comboBox3.SelectedIndex;


        }


        private void button2_Click(object sender, EventArgs e)
        {
           //listBoxStu.SelectedIndex
            ischange = true;
            listindex = listBoxStu.SelectedIndex;
            studentList[listBoxStu.SelectedIndex].Name = textBox1.Text;
            //listBoxStu.Items[listBoxStu.SelectedIndex] = textBox1.Text;
           
            if (radioButton1.Checked)
            {
                studentList[listBoxStu.SelectedIndex].Sex = 0;
            }
            else
            {
                studentList[listBoxStu.SelectedIndex].Sex = 1;
            }
            studentList[listBoxStu.SelectedIndex].BirthDate = dateTimePicker1.Value;
            studentList[listBoxStu.SelectedIndex].ProvinceID = comboBox2.SelectedIndex;
            studentList[listBoxStu.SelectedIndex].CityID = comboBox3.SelectedIndex;
            listBoxStu.Items[listBoxStu.SelectedIndex] = textBox1.Text;
        }


        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
          
            FileStream stream = null;
            // FileMode.Create模式:
            // 如果文件不存在,则创建新文件。
            // 如果文件存在,则打开文件,将原文件内容清空。即以“覆盖”的方式写文件。
            stream = new FileStream(fileName, FileMode.OpenOrCreate);
            BinaryFormatter bFormatter = new BinaryFormatter(); // 用BinaryFormatter类对象实现二进制序列化
            bFormatter.Serialize(stream, studentList); // 此处请与“写二进制文件”的代码作对比,体会序列化的优点
            stream.Close();
        }


     
    }
}





namespace ex5_5
{

   partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;


        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }


        #region Windows 窗体设计器生成的代码


        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.listBoxStu = new System.Windows.Forms.ListBox();
            this.label1 = new System.Windows.Forms.Label();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.radioButton1 = new System.Windows.Forms.RadioButton();
            this.radioButton2 = new System.Windows.Forms.RadioButton();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.comboBox2 = new System.Windows.Forms.ComboBox();
            this.comboBox3 = new System.Windows.Forms.ComboBox();
            this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.label5 = new System.Windows.Forms.Label();
            this.label6 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // listBoxStu
            // 
            this.listBoxStu.FormattingEnabled = true;
            this.listBoxStu.ItemHeight = 12;
            this.listBoxStu.Location = new System.Drawing.Point(24, 51);
            this.listBoxStu.Name = "listBoxStu";
            this.listBoxStu.Size = new System.Drawing.Size(120, 88);
            this.listBoxStu.TabIndex = 0;
            this.listBoxStu.SelectedIndexChanged += new System.EventHandler(this.listBoxStu_SelectedIndexChanged);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(188, 56);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(29, 12);
            this.label1.TabIndex = 1;
            this.label1.Text = "姓名";
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(275, 53);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(196, 21);
            this.textBox1.TabIndex = 2;
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(188, 108);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(29, 12);
            this.label2.TabIndex = 3;
            this.label2.Text = "性别";
            // 
            // radioButton1
            // 
            this.radioButton1.AutoSize = true;
            this.radioButton1.Location = new System.Drawing.Point(275, 106);
            this.radioButton1.Name = "radioButton1";
            this.radioButton1.Size = new System.Drawing.Size(35, 16);
            this.radioButton1.TabIndex = 4;
            this.radioButton1.TabStop = true;
            this.radioButton1.Text = "男";
            this.radioButton1.UseVisualStyleBackColor = true;
            this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
            // 
            // radioButton2
            // 
            this.radioButton2.AutoSize = true;
            this.radioButton2.Location = new System.Drawing.Point(376, 104);
            this.radioButton2.Name = "radioButton2";
            this.radioButton2.Size = new System.Drawing.Size(35, 16);
            this.radioButton2.TabIndex = 5;
            this.radioButton2.TabStop = true;
            this.radioButton2.Text = "女";
            this.radioButton2.UseVisualStyleBackColor = true;
            this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(188, 165);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(53, 12);
            this.label3.TabIndex = 6;
            this.label3.Text = "出生日期";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(197, 241);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(29, 12);
            this.label4.TabIndex = 8;
            this.label4.Text = "籍贯";
            // 
            // comboBox2
            // 
            this.comboBox2.FormattingEnabled = true;
            this.comboBox2.Location = new System.Drawing.Point(275, 233);
            this.comboBox2.Name = "comboBox2";
            this.comboBox2.Size = new System.Drawing.Size(121, 20);
            this.comboBox2.TabIndex = 9;
            this.comboBox2.SelectedIndexChanged += new System.EventHandler(this.comboBox2_SelectedIndexChanged);
            // 
            // comboBox3
            // 
            this.comboBox3.FormattingEnabled = true;
            this.comboBox3.Location = new System.Drawing.Point(275, 321);
            this.comboBox3.Name = "comboBox3";
            this.comboBox3.Size = new System.Drawing.Size(121, 20);
            this.comboBox3.TabIndex = 10;
            this.comboBox3.SelectedIndexChanged += new System.EventHandler(this.comboBox3_SelectedIndexChanged);
            // 
            // dateTimePicker1
            // 
            this.dateTimePicker1.Location = new System.Drawing.Point(275, 165);
            this.dateTimePicker1.Name = "dateTimePicker1";
            this.dateTimePicker1.Size = new System.Drawing.Size(200, 21);
            this.dateTimePicker1.TabIndex = 11;
            this.dateTimePicker1.ValueChanged += new System.EventHandler(this.dateTimePicker1_ValueChanged);
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(189, 366);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 12;
            this.button1.Text = "新增";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(396, 366);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 13;
            this.button2.Text = "修改";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(453, 241);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(17, 12);
            this.label5.TabIndex = 14;
            this.label5.Text = "省";
            // 
            // label6
            // 
            this.label6.AutoSize = true;
            this.label6.Location = new System.Drawing.Point(464, 329);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(17, 12);
            this.label6.TabIndex = 15;
            this.label6.Text = "市";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(627, 410);
            this.Controls.Add(this.label6);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.dateTimePicker1);
            this.Controls.Add(this.comboBox3);
            this.Controls.Add(this.comboBox2);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.radioButton2);
            this.Controls.Add(this.radioButton1);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.listBoxStu);
            this.Name = "Form1";
            this.Text = "Form1";
            this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed);
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();


        }


        #endregion


        private System.Windows.Forms.ListBox listBoxStu;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.RadioButton radioButton1;
        private System.Windows.Forms.RadioButton radioButton2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.ComboBox comboBox2;
        private System.Windows.Forms.ComboBox comboBox3;
        private System.Windows.Forms.DateTimePicker dateTimePicker1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.Label label6;
    }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值