2.2 C#控件的常用操作
创建控件
-
使用new 来创建,比如 TextBox txt=new TextBox();
-
使用控件对象.Loction= new Point(x,y);设置控件的初始位置
-
使用this.Controls.Add(控件对象);将控件对象添加至当前窗体
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 CreateControls
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
TextBox my_txt = new TextBox();
my_txt.Location=new Point(25,25);//设置初始位置
this.Controls.Add(my_txt);//将控件添加至当前窗体
}
}
}
控件的对齐方式
挺简单,鼠标放上去会告诉你都是什么意思
1.文本控件
- Label:标签控件,主要用于显示不可编辑,通过Text属性设置显示的文本
- Button:按钮控件
- TextBox:文本控件
- RichTextBox : 富文本控件
Label控件
- label.Text="";设置显示的文本,获取控件上的值也是通过Text
- label.Visible=True;//设置显示可见,不可见设置false
Button
- AcceptButton属性,当用户按下Enter键,相当于按了Enter
- 窗体的取消按钮:用户按下Esc触发 ,this.CancelButton=buuton1;
private void Form1_Load(object sender, EventArgs e)
{
this.AcceptButton = button1;
}
RichTextBox
- Both属性:文本超出范围后,行、列的滚动条显示
- None:从不显示滚动条
- Horizontal:横向超出范围,显示水平滚动条
- Vertical:纵向超出范围时,显示垂直滚动条
- ForcedHorizontal:当WordWrap设置为false,显示水平滚动条,未文本超出范围,变成灰色
- ForcedVertical:始终显示垂直滚动条,未超出范围,显示为灰色
- ForcedBoth:强制显示水平和垂直方向的滚动条
private void Form1_Load(object sender, EventArgs e)
{
this.AcceptButton = button1;
richTextBox1.Multiline = true;//多行显示
richTextBox1.ScrollBars = RichTextBoxScrollBars.Vertical;//
//字体设置
richTextBox1.SelectionFont = new Font("Courier New", 16, FontStyle.Bold);
//字体颜色
richTextBox1.SelectionColor = System.Drawing.Color.Blue;
//段落显示,每行显示一个黑点
richTextBox1.SelectionBullet = true;
/ /控件做边缘与文本间隔8px
richTextBox1.SelectionIndent = 8;
//右边设置12
richTextBox1.SelectionRightIndent=12;
}
//打开超链接
private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
{
System.Diagnostics.Process.Start(e.LinkText);
}
2.选择控件
- ComboBox:下拉组合控件
- CheckBox: 复选框控件
- RadioButton: 单选按钮控件
- NumericupDown:数值选择控件
- ListBox:列表控件
ComboBox
属性:DropDownStyle
- Simple:列表值部分可见
- DropDown: 可以编辑,默认值,单击右侧箭头才能显示列表
- DropDownList:不可编辑,只显示
//设置下拉不可编辑
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
//添加值
comboBox1.Items.Add("C++");
comboBox1.Items.Add("C#");
comboBox1.Items.Add("JS");
comboBox1.Items.Add("Python");
使用SelectAll方法可以选择可编辑部分的所有文本,但是DropDownStyle必须设置成DropDown
private void button2_Click(object sender, EventArgs e)
{
//当再次查看下拉表时,可编辑文本中内容已经被选中
comboBox1.SelectAll();
}
CheckBox
- CheckState:返回值是Checked(选中) 或Unchecked(未选中)
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if(checkBox1.CheckState==CheckState.Checked)
{
MessageBox.Show("复选框被选中", "");
}
else
{
MessageBox.Show("复选框被取消", "");
}
}
RadioButton
- Checked:true(选中)否则false(未选中)
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if(radioButton1.Checked==true)
{
MessageBox.Show("单选按钮被选中", "");
}
}
NumericUpDown
- Maximum:设置上限最大值
- Minimum:设置最小值
- Value:获得选中的值
private void Form1_Load(object sender, EventArgs e)
{
//设置数值控件的选择范围
numericUpDown1.Maximum = 100;
numericUpDown1.Minimum=0;
//数值后显示小数两位
numericUpDown1.DecimalPlaces = 2;
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
label1.Text = "当前值是:" + numericUpDown1.Value;
}
ListBox
- Items属性中的Remove方法删除项目
- Items属性中的Add方法添加值
- HorizontalScrollbar:设置水平固定弄条,true
- ScrollAlwaysVisible:垂直显示滚动条,true
SelectionMode枚举成员
- MultiExtended:可以多选使用Shift
- MultiSimple:可以选择多项
- None:无法选择项目
- One:只能选一个
private void button3_Click(object sender, EventArgs e)
{
if(textBox1.Text!="")
{
listBox1.Items.Add(textBox1.Text);
}
}
private void button4_Click(object sender, EventArgs e)
{
if(listBox1.SelectedItems.Count!=0)//判断是否选择数据
{
listBox1.Items.Remove(listBox1.SelectedItem);
}
}
3. 分组控件
- Pannel:可用于设置滚动条, Visiable:true显示,false隐藏
- GroupBox:分组控件,Text设置分组标题
- TabControl:选项卡控件,Add方法用于添加控件 tabPage1.Controls.Add(btn1),tabControl1.TabPages.Add(),clear清除所有控件