c# Windows窗体应用程序设计(五)

c# Windows窗体应用程序设计(五)

本次来介绍时钟控件、日历控件和MDI窗口。

1.时钟控件和日历控件

1.新建项目

1.新建一个名为时钟和日历控件的项目,再将Name属性改为FormCalendar。
c# Windows窗体应用程序设计(五)_第1张图片

2.添加控件

所要添加的控件及修改属性如下:c# Windows窗体应用程序设计(五)_第2张图片
c# Windows窗体应用程序设计(五)_第3张图片
添加后的效果如下:
c# Windows窗体应用程序设计(五)_第4张图片

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 trackBar1_Scroll(object sender, EventArgs e)
        {
            if (trackBar1.Value > 0)
                timerCalendar.Interval = 10;
            else
                timerCalendar.Interval = 1000;

        }

        private void buttonRun_Click(object sender, EventArgs e)
        {
            buttonStop.ForeColor = buttonRun.ForeColor;
            buttonStop.Enabled = true;
            timerCalendar.Enabled = true;
            buttonRun.ForeColor = Color.Gray;
            buttonRun.Enabled = false;

        }

        private void buttonRest_Click(object sender, EventArgs e)
        {
            trackBar1.Value = 0;
            dateTimePickerCalendar.ResetText();


        }

        private void buttonStop_Click(object sender, EventArgs e)
        {
            buttonRun.ForeColor = buttonStop.ForeColor;
            buttonRun.Enabled = true;
            timerCalendar.Enabled = false;
            buttonStop.ForeColor = Color.Gray;
            buttonStop.Enabled = false;

        }

        private void timerCalendar_Tick(object sender, EventArgs e)
        {
            dateTimePickerCalendar.Value = dateTimePickerCalendar.Value.AddSeconds(trackBar1.Value * 60 + 1);
            monthCalendarCalendar.TodayDate = dateTimePickerCalendar.Value;
        }
        private void FormCalendar_Load(object sender, EventArgs e)
        {
            dateTimePickerCalendar.Value = DateTime.Now;
            monthCalendarCalendar.TodayDate = DateTime.Now;
        }

    }
}

4.运行效果

c# Windows窗体应用程序设计(五)_第5张图片
通过调整trackBar控件(低速-高速)可以调整时间快慢。

MDI窗口

1.新建项目

1.新建一个名为MDI的窗体,Text属性改为“多文档窗口”,Name属性改为FormMain,WindowState属性改为“Maximized”。
c# Windows窗体应用程序设计(五)_第6张图片

2.插入控件

本次只需要插入MeunStrip控件,具体添加内容如下:
Name属性改为“menuStripMainMenu”
c# Windows窗体应用程序设计(五)_第7张图片
快捷键的设置方式在前面介绍过,建立如图的样式。

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 MDI
{
    public partial class FormMain : Form
    {
        private static int FormCount = 0;
        public FormMain()
        {
            InitializeComponent();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {

        }

        private void menuStripMianMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void 新建SToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form temp = new Form();
            temp.MdiParent = this;
            temp.Text = "窗口#" + FormCount.ToString();
            FormCount++;
            temp.Show();

        }

        private void 关闭YToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void 层叠JToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.Cascade);
        }

        private void 水平ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void 水平LToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.TileVertical);
        }
    }
}

4.运行结果

自己做的效果不怎么好,完整的应该是多个窗口并列出现:
c# Windows窗体应用程序设计(五)_第8张图片
前面几期加上这些就是我对于c# Windows窗体应用程序设计分享的全部内容,后面再更新一个综合实例-------在线口算检测,要用到Timer控件的一些知识,如果反馈好的话就可能更新难度更高的关于c#的内容。后面就更新的内容有可能不连续,内容题材都不定的。
欢迎大家在下方讨论。

你可能感兴趣的:(c#)