TableLayout面板调整大小 [英] TableLayout Panel resize

查看:547
本文介绍了TableLayout面板调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在c#运行时调整tablelayoutpanel控件的大小?



如何实现这一目标? 。

I want to resize tablelayoutpanel control during runtime in c#?

How can I achieve this? .

推荐答案

这对你来说并不容易,但是我想出了这个代码,可能会让你知道你可以做的事情。无论如何都要调整大小。



Well its not going to be easy for you to do but I come up with this code to maybe give you an idea of something you could do as far as resizing goes anyway.

public partial class Form1 : Form
{
    bool resizing = false;
    TableLayoutRowStyleCollection rowStyles;
    TableLayoutColumnStyleCollection columnStyles;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        rowStyles = tableLayoutPanel1.RowStyles;
        columnStyles = tableLayoutPanel1.ColumnStyles;
    }

    private void tableLayoutPanel1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            resizing = true;
        }
    }

    private void tableLayoutPanel1_MouseMove(object sender, MouseEventArgs e)
    {
        if (resizing)
        {
            columnStyles[0].SizeType = SizeType.Absolute;
            rowStyles[0].SizeType = SizeType.Absolute;
            rowStyles[0].Height = e.Y;
            columnStyles[0].Width = e.X;
        }
    }

    private void tableLayoutPanel1_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            resizing = false;
        }
    }
}





编辑:我还将CellBorderStyle属性设置为Single,以便我可以看到这些行



I also set the CellBorderStyle propery to Single so that I could see the lines

tableLayoutPanel1.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;


使用Size propery即

Using the Size propery i.e.
tableLayoutPanel1.Size = new Size(100, 100);





这是你想要的吗?



Is this what you wanted?


我的答案是使用鼠标只调整tablelayoutpanel的列,但可以轻松更新以执行相同操作对于行。当然,我宁愿使用分割板作为行,但仍然。在代码中,我可以提供4列,但将其扩展到更多或更少是很容易的。 cursurToDefault事件将被添加到tablelayoutpanel内容器控件的mousemove事件中。



My answer is for using the mouse to resize only the columns of the tablelayoutpanel but can be easily updated to do the same for rows. I'd rather use a splitpanel for the rows of course, but still. In the code I cater for 4 columns but extending it to more or less is easy enough. The cursurToDefault event will be added to the mousemove-event of the container controls inside the tablelayoutpanel.

private int beamMoving;
private void tableLayoutPanel1_MouseMove(object sender, MouseEventArgs e)
   {
       this.Cursor = Cursors.VSplit;
       if (e.Button == System.Windows.Forms.MouseButtons.Left)
       {
           int beam0 = (int)tableLayoutPanel1.ColumnStyles[0].Width;
           int beam1 = (int)tableLayoutPanel1.ColumnStyles[0].Width + (int)tableLayoutPanel1.ColumnStyles[1].Width;
           int beam2 = (int)tableLayoutPanel1.ColumnStyles[0].Width + (int)tableLayoutPanel1.ColumnStyles[1].Width + (int)tableLayoutPanel1.ColumnStyles[2].Width;

           switch (beamMoving)
           {
               case 0:
                   {
                       if (e.X > 0)
                       {
                           tableLayoutPanel1.ColumnStyles[0].Width = e.X;
                       }
                       break;
                   }
               case 1:
                   {
                       if (e.X - beam0 > 0)
                       {
                           tableLayoutPanel1.ColumnStyles[1].Width = e.X - beam0;
                       }
                       break;
                   }
               case 2:
                   {
                       if (e.X - beam1 > 0)
                       {
                           tableLayoutPanel1.ColumnStyles[2].Width = e.X - beam1;
                       }
                       break;
                   }
           }
       }
   }
   private void cursorToDefault(object sender, MouseEventArgs e)
   {
       this.Cursor = Cursors.Default;
   }

   private void tableLayoutPanel1_MouseDown(object sender, MouseEventArgs e)
   {
       int beam0 = (int)tableLayoutPanel1.ColumnStyles[0].Width;
       int beam1 = (int)tableLayoutPanel1.ColumnStyles[0].Width + (int)tableLayoutPanel1.ColumnStyles[1].Width;
       int beam2 = (int)tableLayoutPanel1.ColumnStyles[0].Width + (int)tableLayoutPanel1.ColumnStyles[1].Width + (int)tableLayoutPanel1.ColumnStyles[2].Width;
       if (e.X + 20 > beam0 && e.X - 20 < beam1)
       {
           beamMoving = 0;
       }
       if (e.X + 20 > beam1 && e.X - 20 < beam2)
       {
           beamMoving = 1;
       }
       if (e.X + 20 > beam2 && e.X - 20 < tableLayoutPanel1.Width)
       {
           beamMoving = 2;
       }
   }


这篇关于TableLayout面板调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆