創(chuàng)建控件
使用new 來(lái)創(chuàng)建,比如 TextBox txt=new TextBox();
使用控件對(duì)象.Loction= new Point(x,y);設(shè)置控件的初始位置
使用this.Controls.Add(控件對(duì)象);將控件對(duì)象添加至當(dāng)前窗體
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);//設(shè)置初始位置
? ? ? ? ? ?this.Controls.Add(my_txt);//將控件添加至當(dāng)前窗體
? ? ? ?}
? ?}
}
控件的對(duì)齊方式
![](/files/attmgn/2024/12/admin20241204152202511_0.jpg)
挺簡(jiǎn)單,鼠標(biāo)放上去會(huì)告訴你都是什么意思
1.文本控件
Label控件
label.Text="";設(shè)置顯示的文本,獲取控件上的值也是通過(guò)Text
label.Visible=True;//設(shè)置顯示可見(jiàn),不可見(jiàn)設(shè)置false
Button
private void Form1_Load(object sender, EventArgs e)
{
? ? ? ? ? ?this.AcceptButton = button1;
}
RichTextBox
Both屬性:文本超出范圍后,行、列的滾動(dòng)條顯示
None:從不顯示滾動(dòng)條
Horizontal:橫向超出范圍,顯示水平滾動(dòng)條
Vertical:縱向超出范圍時(shí),顯示垂直滾動(dòng)條
ForcedHorizontal:當(dāng)WordWrap設(shè)置為false,顯示水平滾動(dòng)條,未文本超出范圍,變成灰色
ForcedVertical:始終顯示垂直滾動(dòng)條,未超出范圍,顯示為灰色
ForcedBoth:強(qiáng)制顯示水平和垂直方向的滾動(dòng)條
private void Form1_Load(object sender, EventArgs e)
{
? ?this.AcceptButton = button1;
? ?richTextBox1.Multiline = true;//多行顯示
? ?richTextBox1.ScrollBars = RichTextBoxScrollBars.Vertical;//
? ?//字體設(shè)置
? ?richTextBox1.SelectionFont = new Font("Courier New", 16, FontStyle.Bold);
? ?//字體顏色
? ?richTextBox1.SelectionColor = System.Drawing.Color.Blue;
? ?//段落顯示,每行顯示一個(gè)黑點(diǎn)
? ? richTextBox1.SelectionBullet = true;
/ /控件做邊緣與文本間隔8px
? ? ? ? ? ?richTextBox1.SelectionIndent = 8;
? ? ? ? ? ?//右邊設(shè)置12
? ? ? ? ? ?richTextBox1.SelectionRightIndent=12;
}
//打開(kāi)超鏈接
?private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
? ? ? ?{
? ? ? ? ? ?System.Diagnostics.Process.Start(e.LinkText);
? ? ? ?}
2.選擇控件
ComboBox
屬性:DropDownStyle
//設(shè)置下拉不可編輯
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
//添加值
comboBox1.Items.Add("C++");
comboBox1.Items.Add("C#");
comboBox1.Items.Add("JS");
comboBox1.Items.Add("Python");
使用SelectAll方法可以選擇可編輯部分的所有文本,但是DropDownStyle必須設(shè)置成DropDown
private void button2_Click(object sender, EventArgs e)
? ? ? ?{
? ? ? ? ? ?//當(dāng)再次查看下拉表時(shí),可編輯文本中內(nèi)容已經(jīng)被選中
? ? ? ? ? ?comboBox1.SelectAll();
? ? ? ?}
CheckBox
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
? ?if(checkBox1.CheckState==CheckState.Checked)
? ?{
? ? ? ?MessageBox.Show("復(fù)選框被選中", "");
? ?}
? ?else
? ?{
? ? ? ?MessageBox.Show("復(fù)選框被取消", "");
? ?}
}
RadioButton
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
? ?if(radioButton1.Checked==true)
? ?{
? ? ? ?MessageBox.Show("單選按鈕被選中", "");
? ?}
}
NumericUpDown
Maximum:設(shè)置上限最大值
Minimum:設(shè)置最小值
Value:獲得選中的值
private void Form1_Load(object sender, EventArgs e)
{
? ?//設(shè)置數(shù)值控件的選擇范圍
? ?numericUpDown1.Maximum = 100;
? ?numericUpDown1.Minimum=0;
?//數(shù)值后顯示小數(shù)兩位
? ? ?numericUpDown1.DecimalPlaces = 2;
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
? ? ?label1.Text = "當(dāng)前值是:" + numericUpDown1.Value;
}
ListBox
Items屬性中的Remove方法刪除項(xiàng)目
Items屬性中的Add方法添加值
HorizontalScrollbar:設(shè)置水平固定條,true
ScrollAlwaysVisible:垂直顯示滾動(dòng)條,true
SelectionMode枚舉成員
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)//判斷是否選擇數(shù)據(jù)
? ?{
? ? ? ?listBox1.Items.Remove(listBox1.SelectedItem);
? ?}
}
3. 分組控件
Pannel:可用于設(shè)置滾動(dòng)條, Visiable:true顯示,false隱藏
GroupBox:分組控件,Text設(shè)置分組標(biāo)題
TabControl:選項(xiàng)卡控件,Add方法用于添加控件 tabPage1.Controls.Add(btn1),tabControl1.TabPages.Add(),clear清除所有控件
該文章在 2024/12/4 15:23:10 編輯過(guò)