CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    May 2010
    Posts
    9

    TableLayoutPanel height issue

    Hey guys,

    I've been having problems when removing rows from a tableLayoutPanel. Let's say I have a table with 5 rows, then I remove the last row. The result is 4 rows, but it's like the last row's height got increased with whatever the 5th row's value was. I can't seems to decrease it no matter what. Changing the last item's value from RowStyle doesn't take effect. Any ideas?

    Cheers!

  2. #2
    Join Date
    Apr 2010
    Location
    Scotland
    Posts
    9

    Re: TableLayoutPanel height issue

    Hi Jason,

    Are you using the Visual Studio forms designer?

    If you are using the forms designer, then you can right click on the table panel and choose "Edit Rows and Columns". From this dialog you can switch between showing columns or rows using the combo box at the top. Once you are viewing the rows you can set the exact size or proportions of the row. I find this dialog to be particularly useful when working with table layout panels.

    I hope this helps and let me know if I have misunderstood.

    Alan
    www.rapidqualitysystems.com

  3. #3
    Join Date
    May 2010
    Posts
    9

    Re: TableLayoutPanel height issue

    Hi Alan, thanks for the feedback!

    Actually, I'm not using the designer, I'm coding the table. What I'm trying to do is create a user control of a thumbnail viewer. I exposed a method to remove an item from a given index, and have the items shift to a new cell. The problem is once that method is called enough so that the last row has no items, I remove it, but the height is all wrong. I included some screen grabs to illustrate my point.

    The 1st image is before. The 2nd image is after removing two items from the control. I'd expect the scroll bar to snap up where Zapp and Zoidberg are, but it just stays where it was originally. I can't seem to decrease the height of that last row. I even tried setting it to 0 with no luck.
    Attached Images Attached Images   

  4. #4
    Join Date
    Apr 2010
    Location
    Scotland
    Posts
    9

    Re: TableLayoutPanel height issue

    Hi Jason,

    I see what you mean.

    It seems that you have to decrement the RowCount property as well as removing the RowStyle. I got the following code to work:
    tableLayoutPanel1.RowCount--;
    tableLayoutPanel1.RowStyles.RemoveAt(tableLayoutPanel1.RowCount);

    But this doesn't reduce the height of the table as whole. You would also need to recalculate its Height property to fix your scrolling issue.

    Also, you must remove any controls from the row before you remove it.

    I don't know if you have come across this post already but it may be of some help to you: http://bytes.com/topic/c-sharp/answe...blelayoutpanel

    Hope this helps.

    Alan
    www.rapidqualitysystems.com

  5. #5
    Join Date
    May 2010
    Posts
    9

    Re: TableLayoutPanel height issue

    Hi Alan,

    I think I missed recalculating the table's height and just assumed it would do it itself once the row was removed. I'll give it a try when I get home tonight.

    Thank you for the info!

  6. #6
    Join Date
    May 2010
    Posts
    9

    Re: TableLayoutPanel height issue

    So I made a new table to test this out, and it looks like AutoSize needs to be set to true and AutoSizeMode to GrowAndShrink, otherwise it never shrinks.

    Hope this helps anyone having the same problem!
    Last edited by jasonlabbe; May 3rd, 2010 at 10:32 PM.

  7. #7
    Join Date
    May 2010
    Posts
    9

    Re: TableLayoutPanel height issue

    It turned out that didn't work at all. It looks like it works when a row gets deleted, but instead of getting a scroll bar, the size of the control just increases. There's a MaximumSize property to cap the table's size, but then removing a row has the same problem!

    I tested on a table that had 2 columns and 3 rows, each cell has a button in them. The height of each row is an absolute value of 100, and the table's height is 250. Here's some code I tried, but I just get the result from the 2nd image.

    public Form1()
    {
    InitializeComponent();

    button5.Parent = null;
    button6.Parent = null;

    table.RowCount -= 1;
    table.RowStyles.RemoveAt(table.RowCount);

    table.RowStyles[table.RowCount-1].Height = 100;
    }
    Attached Images Attached Images   

  8. #8
    Join Date
    May 2010
    Posts
    9

    Re: TableLayoutPanel height issue

    Hot d*mn, I think I got a working solution! It's not complicated either!

    http://www.windows-tech.info/3/f899cf70670e6daa.php

    The OP does a better job than me at explaining the issue. Just to sum it up, all that needs to be done is to parent the table layout panel to a panel. Then set the following properties for the table: AutoSize = true, AutoSizeMode = GrowAndShrink, AutoScroll = FALSE. Set AutoScroll to true on the panel.

    The result is a table where you can freely add, and remove rows without having to worry about awkward empty space or control resizing. Voilà!

  9. #9
    Join Date
    Apr 2010
    Location
    Scotland
    Posts
    9

    Re: TableLayoutPanel height issue

    Hi Jason,

    Thanks for sharing your working solution. Much simpler!

    Alan
    www.rapidqualitysystems.com

  10. #10
    Join Date
    May 2011
    Posts
    1

    Re: TableLayoutPanel height issue

    I added a empty row to the bottom with fixed width 1, as I add new rows I make sure this remains at the bottom

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured