How do I get the width of a TableLayoutPanel column?, or alternatively how can I determine which ColumnStyle applies to a given column?
Printable View
How do I get the width of a TableLayoutPanel column?, or alternatively how can I determine which ColumnStyle applies to a given column?
Hi,Quote:
Originally Posted by Bulldog
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
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.
Here is some code I was using to add a row at runtime to a TableLayoutPanel.
VB Code:
With TableLayoutPanel1 'Add a row to the table .RowCount += 1 Dim index As Integer = .RowCount - 1 'Verify that we are within the bound of the array If index >= arlblDate.Length Then ReDim Preserve arlblDate(arlblDate.Length + INC_SIZE) 'Create controls for the new row arlblDate(index) = New Label With arlblDate(index) .AutoSize = True .Dock = System.Windows.Forms.DockStyle.Fill .Name = "label" & TableLayoutPanel1.RowCount .Text = "Row " & TableLayoutPanel1.RowCount .TextAlign = System.Drawing.ContentAlignment.MiddleCenter End With 'Add a rowstyle for the new row of the table .RowStyles.Add(New System.Windows.Forms.RowStyle) 'We want the rows to be sized equally, so we need to specify the rowstyle 'If we don't specify, the default settings of the control are used .RowStyles.Item(index).SizeType = SizeType.AutoSize 'Add the label to the table .Controls.Add(arlblDate(index), 0, .RowCount) 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:
Dim nWidth As Integer Dim szType As System.Windows.Forms.SizeType With TableLayoutPanel1.ColumnStyles(0) nWidth = .Width szType = .SizeType End With
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...
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.VB Code:
For i = 0 To TLP.ColumnCount - 1 Dim TLPC As Control = TLP.GetControlFromPosition(i, 1) If Not TLPC Is Nothing Then Dim CRect As New Rectangle(TLP.PointToClient(TLPC.Parent.PointToScreen(TLPC.Location)), TLPC.Size) g.DrawLine(Pens.LightGray, TLP.Left + CRect.Left - 1, TLP.Top, TLP.Left + CRect.Left - 1, TLP.Bottom) End If Next
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.
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? :confused:
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.
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.
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.