GroupBox、TextBox、CheckBox、ToolStrip、RichTextBox、Timer控件

时间:2021-04-08 04:12:05

GroupBox:划分窗体区域,内部可以拖放组件

GroupBox、TextBox、CheckBox、ToolStrip、RichTextBox、Timer控件

TextBox:可编辑文本框,也可设置为只读

属性:ReadOnly(只读)、PasswordChar(密码显示的符号,如*)、Multiline(多行,可改变高度)

事件:TextChanged(文本改变触发)

CheckBox:复选框控件

属性:CheckState(是否被选中)

事件:CheckStateChange(选中状态改变触发)

ToolStrip:工具栏

属性:DisplayStyle(如只显示图片)

RichTextBox:有格式文本框,如字体颜色、加粗等

属性:ScrollBar(滚动条)、Font、ForeColor(字体属性)、BulletIndent(项目符号缩进)

事件:LinkClicked(超链接文本被单击触发),在事件中添加代码如下

System.Diagnostics.Process.Start(e.LinkText); //浏览器打开链接

注意:BulletIndent(项目符号缩进),必须在界面代码里打开项目符号,否则缩进值无效。

this.richTextBox1.SelectionBullet = true; //手动输入
this.richTextBox1.BulletIndent = ; //设置缩进值后,自动生成

GroupBox、TextBox、CheckBox、ToolStrip、RichTextBox、Timer控件

Timer:计时器,定期引发事件

属性:Enabled(是否运行,类似Start)、Interval(触发Tick事件的时间间隔,毫秒)、Start、Stop

事件:Tick(超过指定时间,且处于启用状态,才不断触发)

举例:倒计时

timer1用于刷新显示系统时间,属性设置:Enable True,Interval 1000

timer2用于刷新剩余时间,属性默认

GroupBox、TextBox、CheckBox、ToolStrip、RichTextBox、Timer控件

using System;
using System.Windows.Forms; namespace Demo2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//界面启动,控件默认值为系统时间
numericUpDown1.Value = DateTime.Now.Hour;
numericUpDown2.Value = DateTime.Now.Minute;
numericUpDown3.Value = DateTime.Now.Second;
}
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "倒计时")
{
button1.Text = "停止";
timer2.Start();//启动timer2
}else if (button1.Text == "停止")
{
button1.Text = "倒计时";
timer2.Stop();
label7.Text = "倒计时已取消";
}
}
private void timer2_Tick(object sender, EventArgs e)
{
//剩余时间,秒
int timeRemaining=(int)(numericUpDown1.Value*+ numericUpDown2.Value*+ numericUpDown3.Value)-(DateTime.Now.Hour * + DateTime.Now.Minute * + DateTime.Now.Second);
if (timeRemaining> )
{
label7.Text="倒计时开始,剩余" + timeRemaining+ "秒";
}
else
{
label7.Text="倒计时已到";
}
}
private void timer1_Tick(object sender, EventArgs e)
{
label6.Text = DateTime.Now.ToLongTimeString();//每秒刷新一次内容
}
}
}

Timer案例2:随机数

GroupBox、TextBox、CheckBox、ToolStrip、RichTextBox、Timer控件

        private void button1_Click(object sender, EventArgs e)
{
timer1.Start();//启动时钟,1s(Interval属性)
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Stop();
}
private void timer1_Tick(object sender, EventArgs e)
{
Random r = new Random();
label1.Text = r.Next(, ).ToString("");//两位格式
label2.Text = r.Next(, ).ToString("");//两位格式
label3.Text = r.Next(, ).ToString("");//两位格式
label4.Text = r.Next(, ).ToString("");//两位格式
}