LOGO OA教程 ERP教程 模切知識(shí)交流 PMS教程 CRM教程 開(kāi)發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

C#控件的常用操作

admin
2024年12月1日 8:2 本文熱度 325

創(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ì)齊方式

挺簡(jiǎn)單,鼠標(biāo)放上去會(huì)告訴你都是什么意思

1.文本控件

  • Label:標(biāo)簽控件,主要用于顯示不可編輯,通過(guò)Text屬性設(shè)置顯示的文本

  • Button:按鈕控件

  • TextBox:文本控件

  • RichTextBox : 富文本控件

Label控件

  • label.Text="";設(shè)置顯示的文本,獲取控件上的值也是通過(guò)Text

  • label.Visible=True;//設(shè)置顯示可見(jiàn),不可見(jiàn)設(shè)置false


Button

  • AcceptButton屬性,當(dāng)用戶按下Enter鍵,相當(dāng)于按了Enter

  • 窗體的取消按鈕:用戶按下Esc觸發(fā),this.CancelButton=buuton1;


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:下拉組合控件

  • CheckBox:復(fù)選框控件

  • RadioButton: 單選按鈕控件

  • NumericupDown:數(shù)值選擇控件

  • ListBox:列表控件

ComboBox

屬性:DropDownStyle

  • Simple:列表值部分可見(jiàn)

  • DropDown: 可以編輯,默認(rèn)值,單擊右側(cè)箭頭才能顯示列表

  • DropDownList:不可編輯,只顯示

//設(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

  • CheckState:返回值是Checked(選中) 或Unchecked(未選中)

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
? ?if(checkBox1.CheckState==CheckState.Checked)
? ?{
? ? ? ?MessageBox.Show("復(fù)選框被選中", "");

? ?}
? ?else
? ?{
? ? ? ?MessageBox.Show("復(fù)選框被取消", "");
? ?}
}

RadioButton

  • Checked:true(選中)否則false(未選中)

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枚舉成員

  • MultiExtended:可以多選使用Shift

  • MultiSimple:可以選擇多項(xiàng)

  • None:無(wú)法選擇項(xiàng)目

  • One:只能選一個(gè)

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ò)
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點(diǎn)晴ERP是一款針對(duì)中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國(guó)內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對(duì)港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場(chǎng)、車隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場(chǎng)作業(yè)而開(kāi)發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉(cāng)儲(chǔ)管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購(gòu)管理,倉(cāng)儲(chǔ)管理,倉(cāng)庫(kù)管理,保質(zhì)期管理,貨位管理,庫(kù)位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號(hào)管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved

黄频国产免费高清视频,久久不卡精品中文字幕一区,激情五月天AV电影在线观看,欧美国产韩国日本一区二区
亚洲专区首页在线观看 | 在线亚洲中文精品第1页 | 午夜精品褔利一区二区三区蜜桃 | 在线亚洲精品字募免费视频 | 亚洲五月天激情在线视频 | 中日高清字幕一区二区版在线观看 |