|
-
Jan 1st, 2007, 10:15 AM
#1
[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?
-
Jan 1st, 2007, 10:30 AM
#2
Re: [2005] Width of TableLayoutPanel column
 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
-
Jan 1st, 2007, 10:37 AM
#3
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.
-
Jan 1st, 2007, 11:38 AM
#4
Lively Member
Re: [2005] Width of TableLayoutPanel 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
Last edited by brianafischer; Jan 1st, 2007 at 11:45 AM.
-
Jan 1st, 2007, 12:18 PM
#5
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:
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
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.
-
Jan 1st, 2007, 07:14 PM
#6
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.
-
Jan 1st, 2007, 07:48 PM
#7
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.
-
Jan 1st, 2007, 08:12 PM
#8
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.
-
Jan 1st, 2007, 08:27 PM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|