C# Label 标签控件

您所在的位置:网站首页 label的属性是什么 C# Label 标签控件

C# Label 标签控件

2023-11-05 07:32| 来源: 网络整理| 查看: 265

第1,2节参考:chnyac

1 命名空间与继承关系

命名空间1:System.Windows.Forms 继承关系1:Object→MarshalByRefObject→Component→Control→Label 命名空间2:System.Windows.Controls 继承关系2:Object→DispatcherObject→DependencyObject→Visual→UIElement→FrameworkElement→Control→Content→Control→Label

2 Label常用属性 序号属性名说明1Text用来设置或返回标签控件中显示的文本信息。2BorderStyle用来设置或返回边框。①BorderStyle.None 为无边框(默认)②BorderStyle.FixedSingle为固定单边框③orderStyle.Fixed3D 为三维边框。3Enabled用来设置或返回控件的状态。① true :允许使用控件。②false:禁止使用控件。4Width/Height控件宽度和高度。5Visible控件的可见性 3 Label 的使用

(1)Label的赋值:

Label.Text = "Hello World";

(2)Label支持多行:

`this.label1.AutoSize = true;//可以不写这句,因为默认是true this.label1.BackColor = Color.Red; this.label1.Text = "hello\nhello";`

(3)设置Label背景颜色透明:BackColor属性选择Transparent

this.label1.BackColor = Color.Transparent;

(4)使用Label的Image属性进行显示图像。①首先设置AutoSize=False;②Image属性导入图片。

4 Label加载图像

制作一个随机变换图像的小工具效果如下: ①点击开始按钮:label会不停变换图像 ②点击停止按钮:暂停变换(有点类似于抽奖工具) 在这里插入图片描述 代码如下:

public partial class Form1 : Form { public Form1() { InitializeComponent(); for (int i = 0; i < imgArr.Length; i++) { imgArr[i] = Image.FromFile(@"C:\Users\wcy\Desktop\img" + i + ".jpg"); } } //创建Random对象,Img数组存储图片 Random rand = new Random(); Image[] imgArr = new Image[6]; //设置静态变量pos,pos==0:暂停timer,pos==1:启动timer static int pos = 0; private void btn_Start_Click(object sender, EventArgs e) { //设置label的AutoSize,Text属性 this.label1.AutoSize = false;//否则图像大小与label不匹配 this.label1.Text = ""; pos = 1; } private void btn_Stop_Click(object sender, EventArgs e) { pos = 0; } //timer Tick事件 加入判断语句 private void timer1_Tick(object sender, EventArgs e) { if( pos ==1 ) { int num = rand.Next(6); this.label1.Size = imgArr[num].Size; this.label1.Image = imgArr[num].Clone() as Image; } } }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3