[2005] Info on TableLayoutPanel
I use a listview to display tabular data where all I want is a 5 x 4 grid of text labels generated at runtime. I want to be able to control the font, row height, column width etc. The listview is working for me except for the height. Would I have been better off using the tablelayoutpanel? I can't find many code samples on the net.
Re: [2005] Info on TableLayoutPanel
The TableLayoutPanel would be the way to go. See lesson 9 here.
Re: [2005] Info on TableLayoutPanel
In addition to that video, if you haven't already downloaded the 101 Samples for VB 2005, you can do so here: http://msdn2.microsoft.com/en-us/vbasic/ms789075.aspx
Among all the great examples there is one using the TableLayoutPanel. Good luck.
Re: [2005] Info on TableLayoutPanel
thanks.... i wish people had suggested this control when i originally posted in another thread which control was best..... this would have saved me a lot of time...
is it ok to create an instance of a control each time in a loop, add it to the tablelayoutpanel and use the same variable each time as i do below with label1 below:
Code:
For i = sourceIndex To sourceIndex + 19
If i >= sourceArray.Length Then
Exit For
Else
Dim label1 As New Label
label1.Text = sourceArray(i)
label1.TextAlign = ContentAlignment.MiddleCenter
label1.Font = My.Settings.myFont
label1.ForeColor = My.Settings.myForeColor
label1.BackColor = My.Settings.myBackColor
label1.AutoSize = True
Me.TableLayoutPanel1.Controls.Add(label1)
End If
Next
Also, where do the samples get their data from? Whenever I run them they are dataless..
Re: [2005] Info on TableLayoutPanel
Yes, but that's not what you're doing. You are using a different variable each iteration of the loop because it's declared inside the loop. That variable is created and destroyed each iteration so it's a new variable each time. What you suggest is the appropriate way to do it, but this is how that would be implemented:
Code:
Dim label1 As Label
For i = sourceIndex To sourceIndex + 19
If i >= sourceArray.Length Then
Exit For
Else
label1 = New Label
label1.Text = sourceArray(i)
label1.TextAlign = ContentAlignment.MiddleCenter
label1.Font = My.Settings.myFont
label1.ForeColor = My.Settings.myForeColor
label1.BackColor = My.Settings.myBackColor
label1.AutoSize = True
Me.TableLayoutPanel1.Controls.Add(label1)
End If
Next
Now the variable is declared outside the loop so it truly is the same variable each time.
Re: [2005] Info on TableLayoutPanel
Quote:
Originally Posted by jmcilhinney
Yes, but that's not what you're doing. You are using a different variable each iteration of the loop because it's declared inside the loop. That variable is created and destroyed each iteration so it's a new variable each time. What you suggest is the appropriate way to do it, but this is how that would be implemented:
Code:
Dim label1 As Label
For i = sourceIndex To sourceIndex + 19
If i >= sourceArray.Length Then
Exit For
Else
label1 = New Label
label1.Text = sourceArray(i)
label1.TextAlign = ContentAlignment.MiddleCenter
label1.Font = My.Settings.myFont
label1.ForeColor = My.Settings.myForeColor
label1.BackColor = My.Settings.myBackColor
label1.AutoSize = True
Me.TableLayoutPanel1.Controls.Add(label1)
End If
Next
Now the variable is declared outside the loop so it truly is the same variable each time.
Using this implementation, the only way to access the control subsequently is through the control collection of the tablelayout panel?
Re: [2005] Info on TableLayoutPanel
Quote:
Originally Posted by jeffnyc
Using this implementation, the only way to access the control subsequently is through the control collection of the tablelayout panel?
Yes, but that would be the case for any controls created at run time. Normally you would get a control from a TLP by its position, using the GetControlFromPosition method.
Re: [2005] Info on TableLayoutPanel
Quote:
Originally Posted by jmcilhinney
Yes, but that would be the case for any controls created at run time. Normally you would get a control from a TLP by its position, using the GetControlFromPosition method.
So would both accomplish the same thing?
Code:
dim l as label
l = (Label)tlp1.GetControlFromPosition (0,0)
l.text = "Hello"
Code:
Me.tlp1.Controls(0).Text = "Hello"
Is one better? I'm guessing one reason is that with the later you have to know the one dimensional index, where as the former you can specify by row and column which is easier to conceptualize.
Re: [2005] Info on TableLayoutPanel
Quote:
Originally Posted by jeffnyc
I'm guessing one reason is that with the later you have to know the one dimensional index, where as the former you can specify by row and column which is easier to conceptualize.
Exactly. You use a TLP because you want a grid/tabular layout so that is the most logical way to treat the controls it contains.