Results 1 to 9 of 9

Thread: [RESOLVED] [2005] Width of TableLayoutPanel column

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Resolved [RESOLVED] [2005] Width of TableLayoutPanel column

    How do I get the width of a TableLayoutPanel column?, or alternatively how can I determine which ColumnStyle applies to a given column?

  2. #2
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [2005] Width of TableLayoutPanel column

    Quote Originally Posted by Bulldog
    How do I get the width of a TableLayoutPanel column?, or alternatively how can I determine which ColumnStyle applies to a given column?
    Hi,

    To get the width of a TabellayoutPanel Column go to the properties and select Rows collection -> opens a Column and Row Styles form.
    Then you can change the rows and Columns.

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: [2005] Width of TableLayoutPanel column

    I want to find out what the column width is so I can build a graphics image to print the table. Since the ColumnStyles are AutoSize, I need to determine what the width of each column ended up as for a given table content. So I need the ColumnWidths programatically.

    I can get the ColumnStyles and their widths, but I dont know how to associate a given style with a given column.

  4. #4
    Lively Member
    Join Date
    Jan 2006
    Posts
    65

    Re: [2005] Width of TableLayoutPanel column

    Here is some code I was using to add a row at runtime to a TableLayoutPanel.

    VB Code:
    1. With TableLayoutPanel1
    2.  
    3.             'Add a row to the table
    4.             .RowCount += 1
    5.  
    6.             Dim index As Integer = .RowCount - 1
    7.  
    8.             'Verify that we are within the bound of the array
    9.             If index >= arlblDate.Length Then ReDim Preserve arlblDate(arlblDate.Length + INC_SIZE)
    10.  
    11.             'Create controls for the new row
    12.             arlblDate(index) = New Label
    13.             With arlblDate(index)
    14.                 .AutoSize = True
    15.                 .Dock = System.Windows.Forms.DockStyle.Fill
    16.                 .Name = "label" & TableLayoutPanel1.RowCount
    17.                 .Text = "Row " & TableLayoutPanel1.RowCount
    18.                 .TextAlign = System.Drawing.ContentAlignment.MiddleCenter
    19.             End With
    20.  
    21.             'Add a rowstyle for the new row of the table
    22.             .RowStyles.Add(New System.Windows.Forms.RowStyle)
    23.  
    24.             'We want the rows to be sized equally, so we need to specify the rowstyle
    25.             'If we don't specify, the default settings of the control are used
    26.             .RowStyles.Item(index).SizeType = SizeType.AutoSize
    27.  
    28.             'Add the label to the table
    29.             .Controls.Add(arlblDate(index), 0, .RowCount)
    30.  
    31.         End With


    To obtain the column properties use: TableLayoutPanel1.ColumnStyles(i) where "i" is the column you wish to examine. Note the ColumStyles is a collection that you could also iterate through with a For Each.

    VB Code:
    1. Dim nWidth As Integer
    2.         Dim szType As System.Windows.Forms.SizeType
    3.         With TableLayoutPanel1.ColumnStyles(0)
    4.             nWidth = .Width
    5.             szType = .SizeType
    6.         End With
    Last edited by brianafischer; Jan 1st, 2007 at 11:45 AM.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: [2005] Width of TableLayoutPanel column

    Brian,

    Thanks, your post has helped me, but there is still a problem. If a ColumnStyle is set as AutoSize, when I inspect the ColumnStyle, the width is reported as 0.

    It seems the only way I can get the actual column width is to find a control in the table (one in each column), inspect its .Left edge and then offset that to the origin of the table.

    So I have been able to do this...

    VB Code:
    1. For i = 0 To TLP.ColumnCount - 1
    2.                 Dim TLPC As Control = TLP.GetControlFromPosition(i, 1)
    3.                 If Not TLPC Is Nothing Then
    4.                     Dim CRect As New Rectangle(TLP.PointToClient(TLPC.Parent.PointToScreen(TLPC.Location)), TLPC.Size)
    5.                     g.DrawLine(Pens.LightGray, TLP.Left + CRect.Left - 1, TLP.Top, TLP.Left + CRect.Left - 1, TLP.Bottom)
    6.                 End If
    7.             Next
    Which is a little ugly, but its more or less correct. I have to take row 1 incidentally since row 0 is a header the full span of the table.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Width of TableLayoutPanel column

    I strongly suggest that you read the documentation for the classes you're using, including their member listing, before asking any questions. With the TableLayoutPanel that would have led you to the GetColumnWidths method very quickly.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: [2005] Width of TableLayoutPanel column

    Well actually, I've spent all day on this, most of it googling. After many hours of hunting, I gave up, came to the forum to ask if anyone knew how to do this. This is what all members should do, isnt it?

    Anyway, I would have got there a lot quicker but thanks to your post, I found out something new....

    Why does GetColumnWidths not appear in the list of available methods when I use intellisense? If I place a "Dim T as TableLayoutPanel" and then do "T." to see the list, it isnt there, but VS *does* recognize it? I installed the latest service pack a few weeks ago, so my install should be up to date. What is going on? If I dont see an available method, I dont normally consult MSDN, so guess I've learnt something new today.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Width of TableLayoutPanel column

    I have no idea why the GetColumnWidths method isn't listed by Intellisense. I always recommend consulting MSDN frequently (as many on this forum will attest to ), and especially when you want to do something to or with a specific class. I attribute the bulk of my knowledge to MSDN, so I know from personal experience how beneficial its use can be.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: [2005] Width of TableLayoutPanel column

    There's no doubt that you are one of the top "guru's" on the forum (especially since I notice your posts exceed 18,000!). Anyway, going forwards, guess I need to ping the MSDN oracle, even if no apparent method is listed, it would saved me hours.

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