c# Windows窗體應用程序設計(五)
本次來介紹時鐘控件、日曆控件和MDI窗口。
1.時鐘控件和日曆控件
1.新建項目
1.新建一個名爲時鐘和日曆控件的項目,再將Name屬性改爲FormCalendar。
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 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.運行效果
通過調整trackBar控件(低速-高速)可以調整時間快慢。
MDI窗口
1.新建項目
1.新建一個名爲MDI的窗體,Text屬性改爲“多文檔窗口”,Name屬性改爲FormMain,WindowState屬性改爲“Maximized”。
2.插入控件
本次只需要插入MeunStrip控件,具體添加內容如下:
Name屬性改爲“menuStripMainMenu”
快捷鍵的設置方式在前面介紹過,建立如圖的樣式。
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窗體應用程序設計分享的全部內容,後面再更新一個綜合實例-------在線口算檢測,要用到Timer控件的一些知識,如果反饋好的話就可能更新難度更高的關於c#的內容。後面就更新的內容有可能不連續,內容題材都不定的。
歡迎大家在下方討論。