天天看點

C# 實驗5 日期控件、定時器、圖檔框的使用及窗體間傳參

前言:

👏作者簡介:我是笑霸final,一名熱愛技術的在校學生。

目錄

  • ​​一、實驗目的​​
  • ​​二、實驗任務​​
  • ​​1.使用日期控件​​
  • ​​2.使用圖檔框、定時器​​
  • ​​3.設計一個圖書管理系統​​
  • ​​4.建立Windows窗體應用程式,利用2個窗體實作學生成績錄入​​

一、實驗目的

  • 1.掌握日期控件、定時器控件的應用。
  • 2.掌握使用圖檔框控件顯示圖檔的方法。
  • 3.掌握打開檔案對話框的使用方法。
  • 4.掌握多個窗體的使用及窗體間進行資料傳遞的方法

二、實驗任務

1.使用日期控件

使用日期控件,定時器等控件完成“鬧鐘”程式的基本功能,界面如圖5.1所示,運作程式,在日期控件中設定好日期/時間,如圖5.2所示,點選“确定”按鈕,在兩個标簽上分别顯示設定的時間和目前時間(要求目前時間的顯示每隔1秒進行更新),當到了預先設定的時間時,通過消息框提示“定時時間到”。
C# 實驗5 日期控件、定時器、圖檔框的使用及窗體間傳參

源代碼:

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;

namespace 第一題
{
    public partial class Form1 : Form
    {
        string time;//設定的時間
        public Form1()
        {
            InitializeComponent();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            time=  myTime.Value.ToString("yyyy-MM-dd HH:MM:ss");
            timeSet.Text = time;//設定的時間

            timeNow.Text= DateTime.Now.ToString("yyyy-MM-dd HH:MM:ss");//目前時間
            timer1.Enabled=true;//開啟計時器
            timer1.Interval = 1000;//設定1秒

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            myTime.Format = DateTimePickerFormat.Custom;
            myTime.CustomFormat = "yyyy-MM-dd HH:MM:ss";
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timeNow.Text = DateTime.Now.ToString("yyyy-MM-dd HH:MM:ss");//計時器擷取目前時間
            if(time == timeNow.Text)
            {
                //說明時間到了設定的時間
                MessageBox.Show("時間到!");
            }
        }
    }
}      

實驗結果

2.使用圖檔框、定時器

使用圖檔框、定時器、打開檔案對話框設計一個圖檔浏覽程式,界面如圖5.3所示,需要完成以下功能:
  • (1)實作對浏覽圖檔的添加,點選“添加圖檔”按鈕時調用“檔案打開對話框”實作對圖檔的選擇;
  • (2)通過“上一張”和“下一張”按鈕實作對所選擇圖檔的浏覽,要求浏覽到最後一張圖檔時,“下一張”按鈕不可用(灰色),浏覽到第一張圖檔時,“上一張”按鈕不可用。
  • (3)點選“自動播放”按鈕實作對所選擇圖檔的循環自動播放(設每隔2秒自動更換圖檔),點選“暫停”按鈕時自動播放暫停,點選“繼續”按鈕,繼續自動播放圖檔。

源代碼:

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;

namespace 第二題
{
    public partial class Form1 : Form
    {
        OpenFileDialog ofdl = new OpenFileDialog();
        String[] myImags;
        int index = 0;//預設下标
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {//添加按鈕
            try
            {
                ofdl.ShowDialog();//打開檔案對話框
                myImags = ofdl.FileNames;
                pictImage.Image = Image.FromFile(myImags[index]);
            }
            catch(Exception ex)
            {
                //異常處理
                MessageBox.Show("取消添加照片");
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ofdl.Multiselect = true;//允許多選
          
            if (index == 0)
            {
                //是第一張圖檔 (上一張)按鈕不可用
                butOn.Enabled = false;
                //(下一張)按鈕可用  
                butNext.Enabled = true;
            }
           
        }

        private void butOn_Click(object sender, EventArgs e)
        {
            //上一張按鈕
            if ((index % myImags.Length) == myImags.Length - 1)
            {
                //是最後一張圖檔 (上一張)按鈕可用
                butOn.Enabled = true;
                pictImage.Image = Image.FromFile(myImags[(--index) % myImags.Length]);
                butNext.Enabled = false;//開啟下一張按鈕
            }
            else if((index % myImags.Length) != 0)
            {
                butOn.Enabled = true;
                butNext.Enabled = true;
                //切換圖檔
                pictImage.Image = Image.FromFile(myImags[(--index) % myImags.Length]);
            }

            if((index % myImags.Length) == 0)
            {
                //是第一張圖檔 (上一張)按鈕不可用
                    butOn.Enabled = false;
                    //(下一張)按鈕可用  
                    return;//結束
                
            }

          

        }

        private void butNext_Click(object sender, EventArgs e)
        {
            //下一張按鈕 
            if ((index % myImags.Length) == 0)
            {
                //(下一張)按鈕可用  
                butNext.Enabled = true;
                pictImage.Image = Image.FromFile(myImags[(++index) % myImags.Length]);
                butOn.Enabled = true;//開啟添加按鈕
            }
            
            else if((index % myImags.Length) != myImags.Length - 1)
            {
                butOn.Enabled = true;
                butNext.Enabled = true;
                //切換圖檔
                pictImage.Image = Image.FromFile(myImags[(++index) % myImags.Length]);
            }
            if((index % myImags.Length) == myImags.Length - 1)
            {
                //(下一張)按鈕不可用  
                butNext.Enabled = false;
                return;
            }
        }

        private void butAuto_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;//開啟計時器
            timer1.Interval = 2000;//設定1秒
            //循環播放 把數組看成循環隊列
           
        }

        private void butjixu_Click(object sender, EventArgs e)
        {
            timer1.Start();//繼續
        }

        private void butStop_Click(object sender, EventArgs e)
        {
            timer1.Stop();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //(index++) % myImags.Length
            pictImage.Image = Image.FromFile(myImags[(index++) % myImags.Length]);

        }
    }
}      

實驗結果:

C# 實驗5 日期控件、定時器、圖檔框的使用及窗體間傳參
C# 實驗5 日期控件、定時器、圖檔框的使用及窗體間傳參
C# 實驗5 日期控件、定時器、圖檔框的使用及窗體間傳參

3.設計一個圖書管理系統

設計一個圖書管理系統(系統功能無需實作),系統使用者包含2類使用者,分别為一般管理者使用者和超級管理者使用者。2類使用者使用系統時均需要登入進行身份認證,要求輸入合法的使用者名和密碼(若輸入的使用者名或密碼錯誤應給出消息框),登入界面如圖5.4所示,登入成功後(使用者名和密碼均正确)進入圖書管理主界面并顯示歡迎使用者的資訊,如圖5.5所示,若為超級管理者使用者,顯示“一般使用者使用”和“超級使用者使用”兩個按鈕,而對于一般管理者使用者,隻顯示“一般使用者使用”一個按鈕。

要求:以“登入”視窗為啟動窗體和以“圖書管理系統”主視窗為啟動窗體分别實作。

提示:(1)使用者資訊可以存放在結構數組中。此題設使用者有:

  • 張三,密碼:123,級别:0(為超級管理者使用者)
  • 李四,密碼:456,級别:1(為一般管理者使用者)
  • 王五,密碼:789,級别:1(為一般管理者使用者)
(2)聲明結構體存放使用者資訊:
C# 實驗5 日期控件、定時器、圖檔框的使用及窗體間傳參
C# 實驗5 日期控件、定時器、圖檔框的使用及窗體間傳參

源代碼1:

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.Xml.Linq;

namespace 第三題
{
   
    
    public partial class form1 : Form
    {
        struct User
        {
            public string name;
            public string password;
            public string level;
        }
        User[] userinfo = new User[3];
        public form1()
        {
            userinfo[0].name = "張三";
            userinfo[0].password = "123";
            userinfo[0].level = "0";
            userinfo[1].name = "李四";
            userinfo[1].password = "456";
            userinfo[1].level = "1";
            userinfo[2].name = "王五";
            userinfo[2].password = "789";
            userinfo[2].level = "1";

            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
  
        }

        private void button2_Click(object sender, EventArgs e)
        {
            System.Environment.Exit(0);
        }

        private void button1_Click(object sender, EventArgs e)
        {

            String myuse= username.Text;//擷取目前輸入的使用者名
            String myPwd = pwd.Text;//擷取目前輸入的密碼
            if(myuse==""|| myPwd == "")
            {
                MessageBox.Show("使用者名和密碼不能為空");
                return;//結束
            }
            int len=userinfo.Length;//目前的使用者數量
            int index = 0;//下标
            while (index< len)
            {
                if (myuse.Equals(userinfo[index].name))
                {//使用者比對上了
                    if (myPwd.Equals(userinfo[index].password))
                    {
                        Form form2=new Form2();
                        Program.name = userinfo[index].name;//設定目前名字
                        //密碼比對上了
                        if (userinfo[index].level.Equals("0"))//0(為超級管理者使用者)
                        {
                            Program.myStatus=true;//顯示一般使用者使用按鈕
                            form2.Show();
                        }
                        if (userinfo[index].level.Equals("1"))//1(為一般管理者使用者)
                        {
                            Program.myStatus = false;
                            form2.Show();
                        }
                        return;
                    }
                    else
                    {
                        MessageBox.Show("密碼錯誤");
                        //清空密碼
                        pwd.Text = "";
                        return;
                    }
                }
               
                  index++;
            }
            MessageBox.Show("沒有此使用者");
        }
    }
}      

源代碼2:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 第三題
{
    internal static class Program
    {
        /// <summary>
        /// 應用程式的主入口點。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new form1());
            
          
        }
        //多個視窗共享資料
        public static String name = "";
        public static Boolean myStatus = false;
    }
}      

源代碼3:

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;

namespace 第三題
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            //設定第一個視窗傳來的資料
            myname.Text = "歡迎" + Program.name + "使用";
            button2.Visible = Program.myStatus;
        }
    }
}      

實驗結果:

C# 實驗5 日期控件、定時器、圖檔框的使用及窗體間傳參
C# 實驗5 日期控件、定時器、圖檔框的使用及窗體間傳參

4.建立Windows窗體應用程式,利用2個窗體實作學生成績錄入

建立Windows窗體應用程式,利用2個窗體實作學生成績錄入,如圖5.6,5.7所示。在标題為“學生成績錄入系統”的窗體上點選“錄入成績”按鈕後,打開标題為“成績錄入”的窗體,錄入成績。具體要求:
  • (1)在“成績錄入”窗體中分别輸入學号、高數、英語、計算機3科成績後,點選“平均分”按鈕,在其右側的文本框中顯示3科成績的平均值;
  • (2)學生成績及平均分錄入完畢後,點選“錄入”指令按鈕确認,同時關閉“成績錄入”窗體,并且在“學生成績錄入系統”窗體的清單框中顯示剛才的錄入資訊;
  • (3)在“成績錄入”窗體中還有一個“取消”指令按鈕,其功能是:若在成績輸入完畢後沒有點選“錄入”按鈕,而是點選了“取消”按鈕,則關閉“成績錄入”窗體,但剛才的輸入無效,即不在“學生成績錄入系統”窗體的清單框中顯示。

    源代碼一:

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;

namespace 第四題
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

    

        private void button1_Click(object sender, EventArgs e)
        {
            Program.Status = false;//設定狀态 防止第二個視窗 點×也會增加上一次的内容
            Form f2 = new Form2();
            f2.ShowDialog();
            
        }

        private void Form1_Activated(object sender, EventArgs e)
        {
             if (Program.Status)
             {//當視窗活動時 狀态修改過才會添加
                myText.Text += "\n" + Program.myText;
             }
            Program.Status = false;//設定狀态 防止調用時直至錄入
        }

        private void Form1_Load(object sender, EventArgs e)
        {
  
          
                myText.Text = "學号" + "\t" + "高數" + "\t" + "英語" + "\t" + "計算機" + "\t" + "平均分";
         
            Program.Status = false;//設定狀态 防止調用時直至錄入
        }
    }
}      

源代碼二:

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;

namespace 第四題
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void butAge_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "") { MessageBox.Show("id不能為空");return; }
            Program.myId = int.Parse(textBox1.Text);
            if (textBox2.Text == "") { MessageBox.Show("數學成績不能為空"); return; }
            Program.mathematics = int.Parse(textBox2.Text);
            if (textBox3.Text == "") { MessageBox.Show("英語成績不能為空"); return; }
            Program.English = int.Parse(textBox3.Text);
            if (textBox4.Text == "") { MessageBox.Show("計算機成績不能為空"); return; }
            Program.computer = int.Parse(textBox4.Text);
           
            //計算平均分
            Program.avg = (Program.mathematics + Program.English + Program.computer) / 3.0;
            textBox5.Text=Program.avg.ToString();//顯示平均分
        }

        private void butAdd_Click(object sender, EventArgs e)
        {
            //錄入按鈕
            if (textBox5.Text == "") { MessageBox.Show("請計算平均成績"); return; }
            //拼接成績

            Program.myText= Program.myId + "\t" + Program.mathematics + "\t" + Program.English + "\t" + Program.computer + "\t" + Program.avg;

            //設定傳回狀态
            Program.Status = true;
            Form1 f1 = new Form1();
            
            //退出

            this.Close();
        }

        private void butExit_Click(object sender, EventArgs e)
        {
            //設定傳回狀态
            Program.Status = false;
            //退出
            this.Close();
        }
    }
}      

源代碼三:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 第四題
{
    internal static class Program
    {
        /// <summary>
        /// 應用程式的主入口點。
        /// </summary>
        [STAThread]
       
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            
            Application.Run(new Form1());
        }
        //設定共享資料
        public static int myId;//學号
        public static int mathematics;//高數
        public static int English;//英語
        public static int computer;//計算機
        public static double avg;//平均分
        public static String myText ;//資訊
        public static Boolean Status = false;//傳回狀态 

    }
    
}      

實驗結果:

繼續閱讀