9、Lable控件
- 文本控件包含:标签控件(Lable)、按钮控件(button)、文本控件(TexBox)和有格式文本控件(RichTextBox)
- Lable控件可以说是最简单的空间,是System.Windows.Forms.Lable类提供的控件
- Lable控件作用:用来提供其他空间的文字描述文字。例如登录窗体上的用户名,密码
- 设置
- 属性面板
- 代码
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 _9_lable控件
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_MouseEnter(object sender, EventArgs e)
{
int x = this.ClientSize.Width - label1.Width;
int y = this.ClientSize.Height - label1.Height;
Random r = new Random();
label1.Location = new Point(r.Next(0, x + 1), r.Next(0, y + 1));
//MessageBox.Show((r.Next(0,5)).ToString());
}
private void label1_Click(object sender, EventArgs e)
{
MessageBox.Show("你真厉害,竟然点击到了我");
}
}
}
10 Button控件
- button控件是由System.Windows.Forms.Button类提供,该空间最常使用就是编写处理按钮的click事件及MouseEnter事件代码
- 常用属性
- Text:按钮上的文字说明
- Image:替换按钮的北京图片
- 双击控件注册的事件是控件默认选中的事件
Form1
using System;
using System.Windows.Forms;
namespace _10button控件
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//在内存中创建Form2的一个实例
Form2 myfm2 = new Form2();
//展示窗口
myfm2.Show();
}
private void Form1_Load(object sender, EventArgs e)
{
//Form1加载后就将实例存储到了_myfrml字段中
shareclass._myfrm1 = this;
}
}
}
Form2
using System;
using System.Windows.Forms;
namespace _10button控件
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form3 myform3 = new Form3();
myform3.Show();
}
}
}
Form3
using System;
using System.Windows.Forms;
namespace _10button控件
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("你真信了啊,逗你玩的","这你都详细");
//要想关闭所有的窗体,我们只需要关闭主窗体
//通过实例化关闭主窗体是行不通的,
Form1 myform1 = new Form1();
myform1.Close(); //此处我们又新建了一个Form1的实例,并非我们已经打开的那个实例
//关闭主窗口,解决问题的方法是获取到当前打开的实例
//实现方法:静态存储,全局共享,新建一个类存储主窗口实例
shareclass._myfrm1.Close();
}
}
}
静态存储,全局共享
namespace _10button控件
{
public static class shareclass
{
//建立此类主要是用来存储的
public static Form1 _myfrm1;
}
}
11TextBox控件
TextBox控件上有个箭头,MultiLine属性,多行显示。
TextBox控件由Syste.Windows.Forms.TextBox类提供,提供了基本的文本输入和编辑功能
CharacterCasing:字母大小写Upper大写,Lower小写,Normal正常的
MaxLength:最大字符长度,设置文本框输入内容的长度,一般应用于当行模式。单位char
Multiline:控制是否多行显示,True是多行,False是单行
PassWordChar:设置密码掩盖符,为密码设置特殊字符
ReadOnly:控制只读,True为只读,False可写
ScrollBars:滚动条,None不显示,Horizontal垂直,Vertical水平,Both垂直水平滚动条都显示
WordWrap:是否自动换行,True自动换行,False不自动换行;
Text:默认文本框中的文本
AcceptsRetun:控制我们按下回车键是换行还是激活按钮,默认是True—换行,False——激活AccetpButton这个按钮,AcceptButton是窗体的属性,首先要设置窗体的AcceptButton是哪一个按钮,然后再修改TextBox的AccetsRetun属性为True
TextChange事件:当TextBox属性的文本值发生更改时响应的事件,此事件是textBox的默认事件
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 TextBox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("按下2");
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("按下1");
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
label7.Text = textBox1.Text;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
label6.Text = textBox2.Text;
}
}
}
13、RichTextBox控件
RichTExBox控件允许用户输入和编辑文本的同时提供了比普通的TextBox更高级的格式特征
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 RichTextBox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label2_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.SelectionColor = Color.Blue;
}
private void redbtn_Click(object sender, EventArgs e)
{
richTextBox1.SelectionColor = Color.Red;
}
private void ls18btn_Click(object sender, EventArgs e)
{
richTextBox1.SelectionFont = new Font("隶书", 18);
}
private void ls20btn_Click(object sender, EventArgs e)
{
richTextBox1.SelectionFont = new Font("隶书", 20);
}
private void ks18btn_Click(object sender, EventArgs e)
{
richTextBox1.SelectionFont = new Font("楷体", 18);
}
private void ks20btn_Click(object sender, EventArgs e)
{
richTextBox1.SelectionFont = new Font("楷体", 20);
}
private void xmfhbtn_Click(object sender, EventArgs e)
{
richTextBox1.SelectionBullet = true;
}
private void dwbtn_Click(object sender, EventArgs e)
{
richTextBox1.SelectionBackColor = Color.Gray;
}
private void fybtn_Click(object sender, EventArgs e)
{
richTextBox1.SelectionFont = new Font("宋体", 15);
richTextBox1.SelectionColor = Color.Black;
richTextBox1.BackColor = Color.White;
richTextBox1.SelectionBullet = false;
}
}
}
14、Timer控件
- Timer控件是定期引发事件的控件,事件间隔的长度由Interval属性定义,其值以毫秒为单位。若启用了该组件,则每个时间间隔引发一个Tick时间。Timer组件的主要方法包括Start和Stop,分别标识打开和关闭定时器
- DateTime.Now.ToString()是获取当前事件并转换成字符串。
注意:需要将Enable的值改为True,才有效
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 _15_timer控件
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
MessageBox.Show("关不掉了吧,我会不停地弹出","常常我的厉害");
}
}
}
实例;
Timer控件不能保证所经过的时间精确,若要确保精确,计时器应根据需要检查系统使用,而不是常识在内部跟踪所积累的时间。
using System;
using System.Windows.Forms;
namespace _15_timer控件
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
//MessageBox.Show("关不掉了吧,我会不停地弹出","常常我的厉害");
label1.Text = label1.Text.Substring(1) + label1.Text.Substring(0,1);
}
}
}
定时音乐播放器
using System;
using System.Windows.Forms;
using System.Media;
namespace _15_timer控件
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
//MessageBox.Show("关不掉了吧,我会不停地弹出","常常我的厉害");
label1.Text = label1.Text.Substring(1) + label1.Text.Substring(0,1);
}
private void timer2_Tick(object sender, EventArgs e)
{
label2.Text = DateTime.Now.ToString();
SoundPlayer sp = new SoundPlayer();
sp.SoundLocation = @"D:\Screen\01-27-16_15_20.mp4"; //必须是音频文件
if (DateTime.Now.Hour == 14 && DateTime.Now.Minute == 36 && DateTime.Now.Second == 35)
{
//sp.Play(); //单次播放
sp.PlayLooping(); //循环播放
}
if (DateTime.Now.Hour == 14 && DateTime.Now.Minute == 36 && DateTime.Now.Second == 35)
{
//sp.Play(); //单次播放
sp.Stop(); //停止播放
}
}
private void Form1_Load(object sender, EventArgs e)
{
label2.Text = DateTime.Now.ToString();
}
}
}
定时器
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 Timer控件3
{
public partial class Form1 : Form
{
public int t=0;
public Form1()
{
InitializeComponent();
}
private string FormatString(int str)
{
string mystr;
if (str < 10) mystr = "0" + str.ToString();
else mystr = str.ToString();
return mystr;
}
public string getTimeformat(int t)
{
string hh, mm, ss, mms;
int temp=t/100; //获取以秒为单位的总的秒数;
int ms = t % 100;
int h = temp / 3600;
int m = temp / 60 % 60; //temp/60是计算一共多少分钟,在进行取余60就是去除了整小时的秒数
int s = temp % 60;
mms = FormatString(ms);
hh = FormatString(h);
mm = FormatString(m);
ss = FormatString(s);
return FormatString(h) + ":" + FormatString(m) + ":" + FormatString(s) + "." + FormatString(ms);
}
private void timer1_Tick(object sender, EventArgs e)
{
t++;
label1.Text = getTimeformat(t);
}
private void startbtn_Click(object sender, EventArgs e)
{
if (timer1.Enabled)
{
timer1.Stop();
startbtn.Text = "开始计时";
clearbtn.Enabled = true;
}
else
{
timer1.Start();
startbtn.Text = "停止计时";
clearbtn.Enabled = false;
}
}
private void clearbtn_Click(object sender, EventArgs e)
{
label1.Text = getTimeformat(0);
}
}
}
15、checkBox控件(字体设置)
- 选择类控件:主要包含复选框控件(checkBox)、单选框控件(RadioButton)、下拉组合框控件(ComboBox)、列表控件(ListBox)和复选组控件(CheckBoxList)述职选择控件(NumericUpDown)等。
- 作用:用于将一个或少数选项累出,让用户从中选择一个或者多个。由System.Windows.Forms.checkBox类提供
- TreeState的三种属性
- checked:被选中
- Unchecked:未被选中
- Indeterminate:在这种状态下复选框是灰色的,并且不可选
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 _18_checkBox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void xgbtn_Click(object sender, EventArgs e)
{
if (ccbox.Checked)
{
if (xcbox.Checked)
{
if (xhchbox.Checked)
{
textBox1.Font = new Font(textBox1.Font, FontStyle.Bold | FontStyle.Italic | FontStyle.Underline);
}
else
{
textBox1.Font = new Font(textBox1.Font, FontStyle.Bold | FontStyle.Italic);
}
}
else
{
if (xhchbox.Checked)
{
textBox1.Font = new Font(textBox1.Font, FontStyle.Bold | FontStyle.Underline);
}
else
{
textBox1.Font = new Font(textBox1.Font, FontStyle.Bold);
}
}
}
else
{
if (xcbox.Checked)
{
if (xhchbox.Checked)
{
textBox1.Font = new Font(textBox1.Font, FontStyle.Italic | FontStyle.Underline);
}
else
{
textBox1.Font = new Font(textBox1.Font, FontStyle.Italic);
}
}
else
{
if (xhchbox.Checked)
{
textBox1.Font = new Font(textBox1.Font, FontStyle.Underline);
}
else
{
textBox1.Font = new Font(textBox1.Font, FontStyle.Regular);
}
}
}
}
}
}
16、RadioButton(清除文本框,及聚焦控件)
用于让用户从给定列表中选定一个选项。有System.Windows.Forms.RadioButton类提供
- checked:被选中,true是被选中,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 _20_RadioButton控件
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void loginbtn_Click(object sender, EventArgs e)
{
if (adminRadio.Checked || customerRadio.Checked)
{
string name = usertexbox.Text;
string password = passwordBox.Text;
if (name == "admin" && password == "admin" && adminRadio.Checked)
{
MessageBox.Show("登录成功", "管理员账号");
}
else if (name == "guest" && password == "guest" && customerRadio.Checked)
{
MessageBox.Show("登录成功", "来宾账号");
}
else
{
MessageBox.Show("用户名或密码有误,请重新输入", "来宾账号");
usertexbox.Clear(); //清除textBox中的内容
passwordBox.Clear();
usertexbox.Focus(); //聚焦控件
}
}
else
{
MessageBox.Show("请选择客户类型,管理员或来宾","错误提示");
}
}
private void Form1_Load(object sender, EventArgs e)
{
usertexbox.Focus();
}
}
}
17、ComboBox、ListBox、CheckedListBox
17.1 ComboBox
ComboBox控件被称为下拉框组合控件,是由System.Windows.Forms.ComboBox类提供的。主要作用是将一个集合数据以组合框的形式显示给用户,当用户单击时,将以下拉框显示给用户供用户从中选择一项。
17.2ListBox
ListBox控件是由System.Windows.Form.ListBox类提供。主要作用是将一个集合以列表框的形式显示给用户,供用户从中选择一项或多项
SelectionMode:SelectionMode的值决定列表内容是多选还是单选
17.3 checkedListBox
CheckedBoxList控件是由System.Windows.Forms.CheckedListBox类提供的。相当于多个CheckBox控件组合,比较适合用于替代多个CheckBox。例如要选择一个人的爱好时,就要用到许多选择,如用CheckBox控件就要用多个CheckBox,如果用CheckedListBox控件只需一个即可
17.4
三类控件共性:这三类控件都有一个同一的存放集合的属性Items属性,可通过SelectedItem返回选择对象。ListBox控件和CheckedListBox控件都有一个SelectionMod属性,用于设置是单向选择,多项选择还是不可选择
对于三类控件,当用户更改选择项是,都可响应SelectedIndexChanged事件。
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 _21ComboBox_ListBox_CheckedListBox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Clear();
listBox1.Items.Clear();
listBox1.SelectionMode = SelectionMode.MultiSimple;
checkedListBox1.Items.Clear();
string[] mystr = { "语文", "数学", "英语", "物理", "化学", "生物" };
for (int i = 0; i < mystr.Length; i++)
{
comboBox1.Items.Add(mystr[i]);
listBox1.Items.Add(mystr[i]);
checkedListBox1.Items.Add(mystr[i]);
}
label6.Text = "";
label7.Text = "";
label8.Text = "";
label9.Text = "";
label10.Text = "";
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
label6.Text = comboBox1.SelectedItem.ToString();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
label7.Text = listBox1.SelectedItem.ToString();
label10.Text = "";
foreach (string ourstr in listBox1.SelectedItems)
{
label10.Text += ourstr +" ";
}
}
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
label8.Text = checkedListBox1.SelectedItem.ToString();
label9.Text = "";
foreach (string ourstr in checkedListBox1.CheckedItems)
{
label9.Text += ourstr + " ";
}
}
}
}
18、NumericUpDown控件
NumericUpDown控件由System.Windows.Forms.NumericUpDown类提供的,主要作用是将一个数按一定的值进行增减或减少。它主要有四个常用的属性
Increment:每次单击按钮增加或者减少的量
Maximum:最大值
Minimum:最小值
Value:当前值
常用属性Value
值更改是所响应的valueChanged的事件
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 numericUpDown
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "结果显示";
}
private void button2_Click(object sender, EventArgs e)
{
try
{
numericUpDown1.Increment = int.Parse(textBox2.Text);
}
catch (Exception)
{
MessageBox.Show("设置的增量值格式错误,请设置为大于0的数字类型");
textBox2.Clear();
textBox2.Focus();
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
numericUpDown1.DecimalPlaces = int.Parse(textBox1.Text);
}
catch (Exception)
{
MessageBox.Show("请设置为整型的正整数");
textBox1.Clear();
textBox1.Focus();
}
}
private void button3_Click(object sender, EventArgs e)
{
numericUpDown1.UpButton();
}
private void button4_Click(object sender, EventArgs e)
{
numericUpDown1.DownButton();
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
label1.Text = numericUpDown1.Value.ToString();
}
}
}
Panel控件
分组类控件
面板控件:Panel
分组框控件:GroupBox
选项卡控件:TabControl等控件
Panel控件(窗体总是指定显示及button显示样式设置)
- Panel控件是由System.Windows.Forms.Panel类提供的,主要作用就是将其他控件组合在一起放在一个面板上,使这些控件更容易管理。当Panel控件面板上要显示过多的控件是,可设置Autoscroll属性为True
- Panel控件面板在默认情况下不显示边控,如果把BorerStyle属性设置为不是none的其他值,就可以使用面板可视化地组合相关控件。
- panel控件里面可以放置前面的任何控件,panel中显示的内容是加载不同的窗体来实现的,因此加载到Panel的窗体需要设置与Panel同样大小(适当放大一点,因为窗体大小包含标题框)
主窗体
外观显示窗体
常规显示窗体
远程下载窗体
下载窗体
主程序代码
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 _25_Panel控件
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnRuguler_Click(object sender, EventArgs e)
{
Regular myregular = new Regular();
//添加窗体默认为顶级显示,在添加到Panel时需要取消其顶级显示,
myregular.TopLevel = false; //取消窗口的顶级显示
this.panel1.Controls.Add(myregular); //将窗口添加到Panel中
myregular.FormBorderStyle = FormBorderStyle.None;
myregular.BringToFront(); //将窗体放置在最前端
myregular.Show(); //将窗体显示出来
}
private void btnDownload_Click(object sender, EventArgs e)
{
DownLoad mydownload = new DownLoad();
mydownload.TopLevel = false;
this.panel1.Controls.Add(mydownload);
mydownload.FormBorderStyle = FormBorderStyle.None;
mydownload.BringToFront();
mydownload.Show();
}
private void btnAppears_Click(object sender, EventArgs e)
{
Appearance myapperance = new Appearance();
myapperance.TopLevel = false;
this.panel1.Controls.Add(myapperance);
myapperance.FormBorderStyle = FormBorderStyle.None;
myapperance.BringToFront();
myapperance.Show();
}
private void btnRemoteDownload_Click(object sender, EventArgs e)
{
RemoteDownLoad myRemoteDownLoad = new RemoteDownLoad();
myRemoteDownLoad.TopLevel = false;
this.panel1.Controls.Add(myRemoteDownLoad);
myRemoteDownLoad.FormBorderStyle = FormBorderStyle.None;
myRemoteDownLoad.BringToFront();
myRemoteDownLoad.Show();
}
}
}