c# 使用button 和 panel 制作伸缩控件

使用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

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值