【实验目的】
1.掌握Windows窗体的基本属性、事件和方法的使用。
2.掌握控件(Control)的基本属性、事件和方法的使用。需要掌握控件有:窗体)、标签、超链接标签、文本框、按钮、单选按钮和复选框、列表框、组合框和复选列表框、微调按钮、滚动条和进度条、Timer、DateTimePicker与MonthCalendar、图片框、ToolTip。
【实验内容】
- 设计一个如图5-1所示窗体。该窗体自动位于屏幕中央;大小不可调;最小化、最大化按钮不可用;窗体标题为“烟台大学”。在该窗体上,放置一个按钮、一个标签。单击按钮时,在标签上显示当前系统时间。
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; //窗体自动位于屏幕中央
this.Text = "烟台大学";//窗体标题为“烟台大学”
this.MinimizeBox = false;//最小化、最大化按钮不可用
this.MaximizeBox = false;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;//大小不可调
}
private void button2_Click(object sender, EventArgs e)
{
this.label1.Text = DateTime.Now.ToLongTimeString().ToString();//单击按钮时,在标签上显示当前系统时间。
}
private void label1_Click(object sender, EventArgs e)
{
}
}
}
Form1.Designer.cs
namespace WindowsFormsApplication2
{
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.button2 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button2
//
this.button2.Location = new System.Drawing.Point(53, 148);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(180, 39);
this.button2.TabIndex = 1;
this.button2.Text = "单击该按钮显示当前系统时间";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(120, 68);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(41, 12);
this.label1.TabIndex = 2;
this.label1.Text = "";
this.label1.Click += new System.EventHandler(this.label1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 261);
this.Controls.Add(this.label1);
this.Controls.Add(this.button2);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label1;
}
}
2.设计一个如图5-2所示的窗体。窗体上有2个按钮,一个显示文本,一个显示图片。(1)单击上面按钮或按下Alt+B,可以弹出图5-2右图所示的消息框。(2)单击下面按钮也可以弹出图5-2右图所示的消息框(消息对话框参照教材6.5.1节)。
图5-2
运行结果
Form2.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("单击了我","提示!",MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("单击了我", "提示!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
}
private void B(object sender, KeyEventArgs e)
{
if(e.KeyCode==Keys.B&&e.Alt)
MessageBox.Show("单击了我", "提示!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
}
}
}
Form2.Designer.cs
namespace WindowsFormsApplication2
{
partial class Form2
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(89, 74);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(115, 42);
this.button1.TabIndex = 0;
this.button1.Text = "第一个按钮(&B)";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
this.button1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.B);
//
// button2
//
// this.button2.BackgroundImage = global::WindowsFormsApplication2.Properties.Resources.yzh;//注意这是button背景的路径
this.button2.Location = new System.Drawing.Point(89, 138);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(115, 38);
this.button2.TabIndex = 1;
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
this.button2.KeyDown += new System.Windows.Forms.KeyEventHandler(this.B);
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 261);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
}
}
3.设计如图5-3所示的窗体。要求:窗体启动后自动位于屏幕中央;窗体大小不可调;窗体背景色为白色;窗体标题为“我的窗体实验”;窗体上有两个标签,其中一个为链接标签,链接标签字体为宋体16号;单击该链接可以打开烟大主页;单击“结束”按钮程序即可结束。
图5-3
运行结果
Form2.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.MinimizeBox = false;
this.MaximizeBox = false;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;;
}
private void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void Form3_Load(object sender, EventArgs e)
{
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("iexplore.exe", "http://www.ytu.edu.cn/");
}
}
}
Form2.Designer.cs
namespace WindowsFormsApplication2
{
partial class Form3
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.linkLabel1 = new System.Windows.Forms.LinkLabel();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(291, 150);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "结束";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(69, 63);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(317, 12);
this.label1.TabIndex = 1;
this.label1.Text = "下面是一个链接标签,单击该标签即可链接到烟台大学主页";
//
// linkLabel1
//
this.linkLabel1.AutoSize = true;
this.linkLabel1.Font = new System.Drawing.Font("宋体", 16F);
this.linkLabel1.Location = new System.Drawing.Point(52, 147);
this.linkLabel1.Name = "linkLabel1";
this.linkLabel1.Size = new System.Drawing.Size(98, 22);
this.linkLabel1.TabIndex = 2;
this.linkLabel1.TabStop = true;
this.linkLabel1.Text = "烟台大学";
this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
//
// Form3
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(430, 261);
this.Controls.Add(this.linkLabel1);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Name = "Form3";
this.Text = "我的窗体实验";
this.Load += new System.EventHandler(this.Form3_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.LinkLabel linkLabel1;
}
}
4.设计如图5-4所示的窗体。要求:窗体标题为“我文本框实验”;窗体上一个标签,内容如图;窗体上有一个文本框,文本框中只能输入0~9十种数字,最多输入8个字符。单击“结束”按钮程序即可结束。
图5-4
运行结果
Form4.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
private void KeyPress_Digit(object sender, KeyPressEventArgs e)
{
if((e.KeyChar<'0')||(e.KeyChar>'9'))
e.Handled=true;
}
private void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
Form4.Designer.cs
namespace WindowsFormsApplication2
{
partial class Form4
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(62, 69);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(275, 12);
this.label1.TabIndex = 0;
this.label1.Text = "下面文本框只能输入0到9十种数字,且长度最多8个";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(86, 132);
this.textBox1.MaxLength = 8;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(233, 21);
this.textBox1.TabIndex = 1;
this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.KeyPress_Digit);
//
// button1
//
this.button1.Location = new System.Drawing.Point(158, 192);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 2;
this.button1.Text = "结束";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form4
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(396, 261);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Name = "Form4";
this.Text = "我的文本框实验";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
}
}
5.设计一个如下图5-5所示的窗体。窗体上有一个文本框(多行、且带有垂直滚动条)、一个标签(字体颜色红色、字号16)、一个按钮(该按钮被单击时,实现将文本框中选择文本复制到标签。
图5-5
运行结果
Form5.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
this.StartPosition = FormStartPosition.CenterScreen;
}
private void button1_Click(object sender, EventArgs e)
{
label2.Text = textBox1.SelectedText;
}
private void label2_Click(object sender, EventArgs e)
{
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
Form5.Designer.cs
namespace WindowsFormsApplication2
{
partial class Form5
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(359, 111);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(175, 23);
this.button1.TabIndex = 3;
this.button1.Text = "复制选择文本至标签";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("宋体", 16F);
this.label2.ForeColor = System.Drawing.Color.Red;
this.label2.Location = new System.Drawing.Point(24, 210);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(538, 22);
this.label2.TabIndex = 4;
this.label2.Text = "选择文本,然后单击按钮,即可将选中文本复制到标签";
this.label2.Click += new System.EventHandler(this.label2_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(71, 62);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textBox1.Size = new System.Drawing.Size(259, 117);
this.textBox1.TabIndex = 5;
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged_1);
this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
//
// Form5
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(599, 261);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.button1);
this.Name = "Form5";
this.Text = "文本框实例";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox1;
}
}
6.设计一个如图5-6所示的窗体。窗体上有两个文本框:一个文本框中最多输入字符6个;一个文本框中输入任何内容都显示*号。再添加一个按钮、两个单选按钮。实现单击按钮后,根据单选按钮,将对应文本框中内容显示在标签。
图5-6
运行结果
Form6.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace WindowsFormsApplication2
{
public partial class Form6 : Form
{
string s;
public Form6()
{
InitializeComponent();
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
Regex rg = new Regex("^[\u4e00-\u9fa5\b]$"); //\b是退格键
if (!rg.IsMatch(e.KeyChar.ToString()))
{
e.Handled = true;
}
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
s = textBox1.Text;
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
s = textBox2.Text;
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = s;
}
}
}
Form6.Designer.cs
namespace WindowsFormsApplication2
{
partial class Form6
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.radioButton2 = new System.Windows.Forms.RadioButton();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// radioButton1
//
this.radioButton1.AutoSize = true;
this.radioButton1.Location = new System.Drawing.Point(177, 45);
this.radioButton1.Name = "radioButton1";
this.radioButton1.Size = new System.Drawing.Size(131, 16);
this.radioButton1.TabIndex = 0;
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(177, 83);
this.radioButton2.Name = "radioButton2";
this.radioButton2.Size = new System.Drawing.Size(131, 16);
this.radioButton2.TabIndex = 1;
this.radioButton2.TabStop = true;
this.radioButton2.Text = "取第二个文本框内容";
this.radioButton2.UseVisualStyleBackColor = true;
this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton2_CheckedChanged);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(42, 45);
this.textBox1.MaxLength = 6;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 21);
this.textBox1.TabIndex = 2;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(42, 83);
this.textBox2.MaxLength = 6;
this.textBox2.Name = "textBox2";
this.textBox2.PasswordChar = '*';
this.textBox2.Size = new System.Drawing.Size(100, 21);
this.textBox2.TabIndex = 3;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(42, 219);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(41, 12);
this.label1.TabIndex = 4;
this.label1.Text = "label1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(54, 159);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(205, 23);
this.button1.TabIndex = 5;
this.button1.Text = "根据单选按钮,将对应文本框中内容显示在标签";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form6
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(326, 261);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.radioButton2);
this.Controls.Add(this.radioButton1);
this.Name = "Form6";
this.Text = "单选按钮实例";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.RadioButton radioButton1;
private System.Windows.Forms.RadioButton radioButton2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
}
}
7.设计如图5-7所示的窗体,实现下面要求的效果。
(1)复选框中文字在左边;
(2)最下部为水平滚动条。水平滚动条最小值为4、最大值为72,且在窗体Load事件中通过代码设置;
(3)单击任何复选框,标签上文字样式都发生变化;
(4)单击任意单选按钮,标签上文字字体都发生改变;
(5)拖动水平滚动条,标签上文字大小发生变化。
图5-7
运行结果
Form7.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form7 : Form
{
public Form7()
{
InitializeComponent();
label1.Text = "烟台大学";
label1.AutoSize = true;
// label1.Font = new Font("宋体", 48, FontStyle.Regular);
hScrollBar1.Minimum = 4;
hScrollBar1.Maximum = 72;
}
private void hScrollBar1_Scroll_1(object sender, ScrollEventArgs e)
{
label1.Font =new Font(label1.Font.Name, hScrollBar1.Value,label1.Font.Style);
}
private void checkBox1_CheckedChanged_1(object sender, EventArgs e)
{
label1.Font = new Font(label1.Font.Name, label1.Font.Size, checkBox1.Checked ? label1.Font.Style | FontStyle.Italic : label1.Font.Style ^ (FontStyle.Italic));
}
private void checkBox2_CheckedChanged_1(object sender, EventArgs e)
{
label1.Font = new Font(label1.Font.Name, label1.Font.Size, checkBox2.Checked ? label1.Font.Style | FontStyle.Underline : label1.Font.Style ^ (FontStyle.Underline));
}
private void checkBox3_CheckedChanged_1(object sender, EventArgs e)
{
label1.Font = new Font(label1.Font.Name, label1.Font.Size, checkBox3.Checked ? label1.Font.Style | FontStyle.Bold : label1.Font.Style ^ (FontStyle.Bold));
}
private void radioButton2_CheckedChanged_1(object sender, EventArgs e)
{
label1.Font = new Font("黑体", label1.Font.Size, label1.Font.Style);
}
private void radioButton1_CheckedChanged_1(object sender, EventArgs e)
{
label1.Font = new Font("楷体", label1.Font.Size, label1.Font.Style);
}
}
}
Form7.Designer.cs
namespace WindowsFormsApplication2
{
partial class Form7
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.checkBox2 = new System.Windows.Forms.CheckBox();
this.checkBox3 = new System.Windows.Forms.CheckBox();
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.radioButton2 = new System.Windows.Forms.RadioButton();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.hScrollBar1 = new System.Windows.Forms.HScrollBar();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(30, 22);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(53, 12);
this.label1.TabIndex = 0;
this.label1.Text = "烟台大学";
//
// checkBox1
//
this.checkBox1.AutoSize = true;
this.checkBox1.Location = new System.Drawing.Point(23, 47);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(48, 16);
this.checkBox1.TabIndex = 1;
this.checkBox1.Text = "斜体";
this.checkBox1.UseVisualStyleBackColor = true;
this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged_1);
//
// checkBox2
//
this.checkBox2.AutoSize = true;
this.checkBox2.Location = new System.Drawing.Point(77, 47);
this.checkBox2.Name = "checkBox2";
this.checkBox2.Size = new System.Drawing.Size(60, 16);
this.checkBox2.TabIndex = 2;
this.checkBox2.Text = "下划线";
this.checkBox2.UseVisualStyleBackColor = true;
this.checkBox2.CheckedChanged += new System.EventHandler(this.checkBox2_CheckedChanged_1);
//
// checkBox3
//
this.checkBox3.AutoSize = true;
this.checkBox3.Location = new System.Drawing.Point(143, 47);
this.checkBox3.Name = "checkBox3";
this.checkBox3.Size = new System.Drawing.Size(48, 16);
this.checkBox3.TabIndex = 3;
this.checkBox3.Text = "粗体";
this.checkBox3.UseVisualStyleBackColor = true;
this.checkBox3.CheckedChanged += new System.EventHandler(this.checkBox3_CheckedChanged_1);
//
// radioButton1
//
this.radioButton1.AutoSize = true;
this.radioButton1.Location = new System.Drawing.Point(130, 47);
this.radioButton1.Name = "radioButton1";
this.radioButton1.Size = new System.Drawing.Size(47, 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_1);
//
// radioButton2
//
this.radioButton2.AutoSize = true;
this.radioButton2.Location = new System.Drawing.Point(25, 46);
this.radioButton2.Name = "radioButton2";
this.radioButton2.Size = new System.Drawing.Size(47, 16);
this.radioButton2.TabIndex = 5;
this.radioButton2.TabStop = true;
this.radioButton2.Text = "黑体";
this.radioButton2.UseVisualStyleBackColor = true;
this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton2_CheckedChanged_1);
//
// groupBox1
//
this.groupBox1.Controls.Add(this.checkBox1);
this.groupBox1.Controls.Add(this.checkBox2);
this.groupBox1.Controls.Add(this.checkBox3);
this.groupBox1.Location = new System.Drawing.Point(12, 128);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(222, 93);
this.groupBox1.TabIndex = 6;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "字体样式";
//
// groupBox2
//
this.groupBox2.Controls.Add(this.radioButton1);
this.groupBox2.Controls.Add(this.radioButton2);
this.groupBox2.Location = new System.Drawing.Point(294, 128);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(209, 93);
this.groupBox2.TabIndex = 7;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "字体";
//
// hScrollBar1
//
this.hScrollBar1.Location = new System.Drawing.Point(56, 234);
this.hScrollBar1.Maximum = 72;
this.hScrollBar1.Minimum = 4;
this.hScrollBar1.Name = "hScrollBar1";
this.hScrollBar1.Size = new System.Drawing.Size(427, 58);
this.hScrollBar1.TabIndex = 8;
this.hScrollBar1.Value = 4;
this.hScrollBar1.Scroll += new System.Windows.Forms.ScrollEventHandler(this.hScrollBar1_Scroll_1);
//
// Form7
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(550, 301);
this.Controls.Add(this.hScrollBar1);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.label1);
this.Name = "Form7";
this.Text = " 单选和复选框实验";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.CheckBox checkBox2;
private System.Windows.Forms.CheckBox checkBox3;
private System.Windows.Forms.RadioButton radioButton1;
private System.Windows.Forms.RadioButton radioButton2;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.HScrollBar hScrollBar1;
}
}
8.设计如图5-8所示的窗体,窗体上有一个列表框、一个标签和一个按钮。利用Random类产生10个[10,99]之间的随机数,并将这10个随机数在列表框中显示出来,每个数占一项。用户选择某项后,在右边标签中显示所选内容。
图5-8
运行结果
Form8.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form8 : Form
{
public Form8()
{
InitializeComponent();
Random rd=new Random();
int []num=new int [10];
for(int i=0;i<10;i++)
{
num[i] = rd.Next(10,99);
listBox1.Items.Add(num[i]);
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
label1.Text = listBox1.Text;
}
private void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
Form8.Designer.cs
namespace WindowsFormsApplication2
{
partial class Form8
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(35, 50);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(120, 88);
this.listBox1.TabIndex = 0;
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(202, 76);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(0, 12);
this.label1.TabIndex = 1;
//
// button1
//
this.button1.Location = new System.Drawing.Point(185, 167);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 2;
this.button1.Text = "结束";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form8
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 261);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Controls.Add(this.listBox1);
this.Name = "Form8";
this.Text = "列表框实验";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
}
}
9、利用Timer控件,设计一个如下图5-9所示的电子时钟。要求启动窗体后,在窗体的标签上显示系统当前日期和时间,单击“结束”按钮,则停止显示日期和时间。
图5-9
运行结果
Form9.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form9 : Form
{
public Form9()
{
InitializeComponent();
timer1.Enabled = true;
timer1.Interval = 10;
}
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
private void button1_Click_1(object sender, EventArgs e)
{
timer1.Stop();
}
}
}
Form9.Designer.cs
namespace WindowsFormsApplication2
{
partial class Form9
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// timer1
//
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(129, 105);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(41, 12);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(131, 168);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "结束";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click_1);
//
// Form9
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(359, 261);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Name = "Form9";
this.Text = "利用Timer控件,设计电子时钟";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
}
}
10、利用Timer和图片框控件,编写一个图片不断向左向右移动的小动画。效果如图5-10所示,图片文件名称为a.png,位于压缩包内。
要求:
(1)改变图片的Left值,图片向左(右)移动。
(2)利用Random类的Next方法产生一个[1,9]的数字作为Left值。
(3)图片不要移出窗体,如果Left值超出窗体范围,能控制图片在窗体内向左或向右移动。
图5-10
运行结果
Form10.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form10 : Form
{
public Form10()
{
InitializeComponent();
timer1.Enabled = true;
timer1.Interval = 100;
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
Timer t = new Timer();
}
Random rd = new Random();
int p = 1, ind;
private void timer1_Tick(object sender, EventArgs e)
{
ind = rd.Next(1, 9);
if (ind * p > this.pictureBox1.Left || this.pictureBox1.Right + (-p * ind) >= this.Width)
{
p = -p;
}
this.pictureBox1.Left -=ind*p;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
}
}
Form10.Designer.cs
namespace WindowsFormsApplication2
{
partial class Form10
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.pictureBox1 = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// timer1
//
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// pictureBox1
//
this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
this.pictureBox1.Image = global::WindowsFormsApplication2.Properties.Resources.yzh;
this.pictureBox1.Location = new System.Drawing.Point(40, 74);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(120, 48);
this.pictureBox1.TabIndex = 1;
this.pictureBox1.TabStop = false;
this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
//
// Form10
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 261);
this.Controls.Add(this.pictureBox1);
this.Name = "Form10";
this.Text = "Form10";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.PictureBox pictureBox1;
}
}