使用Button和Panel 设计伸缩面板控件,界面效果图如下:
第一步,主界面
界面布局:button和panel
button 点击事件,循环插入数据
private void button1_Click(object sender, EventArgs e)
{
this.panel1.Controls.Clear();
for (int i = 5; i >= 0; i--)
{
ViewDate ve = new ViewDate();
ve.title = "标题" + i.ToString();
ve.data1 = i.ToString() + " 111111";
ve.data2 = i.ToString() + " 222222";
ve.data3 = i.ToString() + " 333333";
ve.tag = i;
UserDataView us = new UserDataView(ve);
this.panel1.Controls.Add(us);
us.Dock = DockStyle.Top;
//us.myEven_click += new UserDataView.ButtonClick(ViewDock);
//UsList.Add(us);
}
}
第二步,添加用户自定义控件
界面布局 : 顶部 button,panel使用 Fill, panel中添加 label 显示内容
private int pHeight;
public UserDataView(ViewDate ve)
{
// TODO: Complete member initialization
InitializeComponent();
this.button1.Text = ve.title;
this.button1.Tag = ve.tag;
this.label1.Text = ve.data1;
this.label2.Text = ve.data2;
this.label3.Text = ve.data3;
pHeight = this.Height; // 获取按钮的高度
this.pictureBox1.BackColor = Color.Transparent;
this.pictureBox1.Parent = this.button1;
string filePath = System.Environment.CurrentDirectory + System.IO.Path.DirectorySeparatorChar + "Image" + "\\timg2.png";
this.pictureBox1.Image = System.Drawing.Image.FromFile(filePath);
}
private void button1_Click(object sender, EventArgs e)
{
if (this.panel1.Visible)
{
this.panel1.Visible = false;
this.Height = this.button1.Height; // 修改控件的高度
string filePath = System.Environment.CurrentDirectory + System.IO.Path.DirectorySeparatorChar + "Image" + "\\timg1.png";
this.pictureBox1.Image = System.Drawing.Image.FromFile(filePath);
}
else
{
this.panel1.Visible = true;
this.Height = pHeight; // 修改控件的高度
string filePath = System.Environment.CurrentDirectory + System.IO.Path.DirectorySeparatorChar + "Image" + "\\timg2.png";
this.pictureBox1.Image = System.Drawing.Image.FromFile(filePath);
}
}
// 修改右边图标位置
private void button1_Resize(object sender, EventArgs e)
{
int y = Convert.ToInt32((this.button1.Height - this.pictureBox1.Height) * 0.5);
int x = this.button1.Width - 35 - pictureBox1.Width;
this.pictureBox1.Location = new Point(x, y);
}
这里添加 button 的点击事件, 点击后,修改控件的高度 和右边图标,隐藏或显示panel