注册 登录
编程论坛 C# 论坛

C# 怎样实现自动布局

litao31415 发布于 2014-10-31 10:53, 659 次点击
描述:怎样实现通过按下一个按钮来删除某一个控件时,其他的控件会自动伸展以补齐空位,当下次需要显示该控件时其他控件就缩小,以腾出空间放下该控件?有什么思路?
3 回复
#2
xydddaxia2014-10-31 11:02
TableLayoutPanel控件
#3
litao314152014-11-03 09:49
回复 2 楼 xydddaxia
用鼠标拖动控件(譬如richtexbox)边缘,伸缩是需要自己添加事件处理吗?譬如重新计算里面的控件位置和尺寸?
#4
xydddaxia2014-11-04 09:59
通过一些鼠标事件可以解决调整大小
程序代码:

  private bool isMouseDown = false;
        private int mouseX = 0;
        private int baseWidth = 0;
        private void listBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseDown)
            {
                this.listBox1.Width = baseWidth + e.X - mouseX;
                if (this.listBox1.BorderStyle != BorderStyle.FixedSingle)
                {
                    this.listBox1.BorderStyle = BorderStyle.FixedSingle;
                }
            }
            else
            {
                int n = Math.Abs(e.X - this.listBox1.Width);
                this.label1.Text = n.ToString();
                if (n < 10)
                {
                    if (this.Cursor != Cursors.SizeWE)
                    {
                        this.listBox1.Cursor = this.Cursor = Cursors.SizeWE;

                    }
                }
                else
                {
                    if (this.Cursor != Cursors.Default)
                        this.listBox1.Cursor = this.Cursor = Cursors.Default;
                }
            }
        }
        private void listBox1_MouseUp(object sender, MouseEventArgs e)
        {
            isMouseDown = false;
            if (this.listBox1.BorderStyle != BorderStyle.Fixed3D)
            {
                this.listBox1.BorderStyle = BorderStyle.Fixed3D;
            }
        }

        private void listBox1_MouseDown(object sender, MouseEventArgs e)
        {
            isMouseDown = true;
            baseWidth = this.listBox1.Width;
            mouseX = e.X;
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            listBox1_MouseMove(sender, e);
        }
1