Click here to Skip to main content
15,922,894 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
In table layout panel, if i drag some particular row,gthe entire panel is moving. I just want to move only the selected row or column. How can i do this?

Using this code to move entire tablelayoutpanel:
private void tableLayoutPanel1_MouseMove(object sender, MouseEventArgs e)
    {
      int GRow = 0;
      int GColumn = 0;

        try
        {
            if (resizing)
            {

                columnStyles[GColumn].SizeType = SizeType.Absolute;
                rowStyles[GRow].SizeType = SizeType.Absolute;
                rowStyles[GRow].Height += e.Y - rowStyles[GRow].Height;
                columnStyles[GColumn].Width += e.X - columnStyles[GColumn].Width;

            }
        }
        catch (Exception ex)
        {
           // MessageBox.Show(ex.ToString());
        }
    }


To get the GRow and GColumn used this below code:

private void tableLayoutPanel1_MouseClick(object sender, MouseEventArgs e)
       {
           try
           {
               int Row = 0;

               int VSpace = 0;
               GRow = Row;
               foreach (int h in tableLayoutPanel1.GetRowHeights())
               {
                   int Column = 0;
                   int HSpace = 0;

                   foreach (int w in tableLayoutPanel1.GetColumnWidths())
                   {
                       Rectangle Rect = new Rectangle(HSpace, VSpace, w, h);
                       if ((Rect.Contains(e.Location)))
                       {
                           GColumn = Column;
                           MessageBox.Show("Row Value = " + Row.ToString() + " " + "Column Value = " + Column.ToString(), "TableLayoutPanel", MessageBoxButtons.OK, MessageBoxIcon.Information);
                           return;
                       }
                       HSpace += w;
                       Column += 1;
                   }
                   VSpace += h;
                   Row += 1;
               }

           }

           catch (Exception ex)
           {
               MessageBox.Show(ex.ToString ());
           }
       }


I want to move only the specific row or column. Help me.
Posted
Updated 27-Nov-11 17:20pm
v2
Comments
[no name] 27-Nov-11 23:20pm    
EDIT: added <pre> tag.
srinivasan_indian 28-Nov-11 0:40am    
Want me to add <pre> tag
[no name] 28-Nov-11 0:51am    
I already added.
srinivasan_indian 28-Nov-11 23:20pm    
Did u find any solution for my issue?
Sergey Alexandrovich Kryukov 28-Nov-11 0:10am    
Tag it: WPF, Silverlight, Forms, ASP.NET, what?
--SA

1 solution

If I understand correctly, You want to move the border between two columns. So from that border's point of view, you have a left and a right column.

I'd try something like this:
C#
private System.Windows.Forms.TableLayoutPanelColumnStyle _leftColumnStyle = null;
private System.Windows.Forms.TableLayoutPanelColumnStyle _rightColumnStyle = null;
private System.Drawing.Point _locationWhereUserStartedDragging = new Point(-1, -1);

tableLayoutPanel1_MouseDown(object sender, MouseEventArgs e)
{
    _locationWhereUserStartedDragging = e.Location;

    GetColumnsThatUserClickedInBetween(e, ref _leftColumnStyle, ref _rightColumnStyle);

    foreach(ColumnStyle style in tableLayoutPanel1.ColumnStyles)
    {
        if( style.Equals(_rightColumnStyle))
        {
            style.SizeType = SizeTypes.AutoSize;
        }
        else
        {
            style.SizeType = SizeTypes.Absolute;
        }
    }
}

tableLayoutPanel1_MouseMove(object sender, MouseEventArgs e)
{
    _leftColumnStyle.Width = e.Location.X - _locationWhereUserStartedDragging.X;
}
When the user presses the mouse button down, our application remembers where that was to calculate how far the user may have moved the mouse.
It also remembers the single column that is set to have an automatically calculated width. So when we change the left column's width, the right one should follow.
 
Share this answer
 
Comments
srinivasan_indian 2-Dec-11 0:03am    
It shows some errors:1. GetColumnsThatUserClickedInBetween-where its declared

2.In Mousedown event, i used your code. But,it is not at all moving the specific column as well as all the columns. That is, I cant able to move any columns.

Help me for these issues..
lukeer 2-Dec-11 4:42am    
1. There is no such method. It's up to you to create it.
You should be able to do it since the functionality behind it must already be included in your original calculation of GColumn as you have used it in columnStyles[GColumn].

2. My first guess is that it doesn't work because of the first error. My MouseMove suggestion needs _leftColumnStyle and _locationWhereUserStartedDragging to be set in MouseDown event handler.
Actually, it should throw an exception because _leftColumnStyle is still set to null.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900