用户控件

it2025-02-06  14

一、创建用户控件

1.新建用户控件

选择操作空间,右键选择添加,点击用户控件

2.设计用户控件

在用户控件中添加几个控件

3.编写后台功能

private void First_MouseEnter(object sender, EventArgs e) { //鼠标移入,背景变红 this.BackColor = Color.Red; } private void First_MouseLeave(object sender, EventArgs e) { //鼠标移出,背景恢复 this.BackColor = Color.Transparent; } private void First_DoubleClick(object sender, EventArgs e) { //双击控件后两个label发生改变 Form2 f2 = new Form2(label1.Text, label2.Text); f2.Show(); }

4.主窗口使用用户控件

主窗口后台代码

private void button1_Click(object sender, EventArgs e) { for (int i = 1; i <= 15; i++) { First f = new First(); //更改用户控件中pictureBox背景 f.pictureBox1.BackgroundImage = Image.FromFile("G:\\0425\\6、WinForm\\2016-7-4\\用户控件\\用户控件\\images\\1.png"); //更改图片的填充方式 f.pictureBox1.BackgroundImageLayout = ImageLayout.Stretch; //更改label标签内容 f.label1.Text = "用户" + i; f.label2.Text = "签名" + i; flowLayoutPanel1.Controls.Add(f); } }

鼠标未移入

鼠标移入

双击控件

 

转载于:https://www.cnblogs.com/bosamvs/p/5648050.html

最新回复(0)