|
-
Nov 7th, 2022, 02:41 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] How to change properties of added control in table layout panel
Hi, Consider you want to add a label inside of a table layout panel programmatically after pressing a button and it gets the value of current pressedcounts. Here's the approach (IT MIGHT BE NOT A GOOD IDEA AND NOT THE BEST WAY BUT IT WORKS):
Code:
Public Class Form1
Public i As Integer = 0
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
i += 1
TableLayoutPanel1.Controls.Add(New Label, 0, 0)
TableLayoutPanel1.Controls.Item(i - 1).Text = i.ToString
End Sub
End Class
- The problem is you may access only to few properties of added control like "text". I even made a UserControl and added it but still not able to access things like "dock", "textorientation", "autosize".
- It would be better if all new added columns be 50% in size and decent.
- I remember jmcilhinney mentioned something about "TryCast" thing in another similar thread but I'm not familiar with it, plus Learn.Microsoft says something about expression ctype and stuffs which is confusing... :'(
As a visual example the goal is to add controls in order of occurrence in rows and columns from left to right all middle oriented docked top and bottom with bold and regular properties but programmatically...
-
Nov 7th, 2022, 04:14 AM
#2
Re: How to change properties of added control in table layout panel
The TableLayoutControlCollection.Item property is type Control, so it will return a reference of type Control, allowing you to access any member of the Control class but not members of any derived class. The thing is, why put the control into the TLP first, then get it out again to configure it? Create the control, set its properties, then add it to the TLP as the last thing you do. You don't even have to take the constructor out of the Add line because you can use an object initialiser:
vb.net Code:
TableLayoutPanel1.Controls.Add(New Label With {.Text = i.ToString}, 0, 0)
You can initialise as many properties as you like that way and you should.
-
Nov 7th, 2022, 05:18 AM
#3
Re: How to change properties of added control in table layout panel
To set more than one property using jmcilhinney’s suggestion, separate each property and value with a comma…
Code:
TableLayoutPanel1.Controls.Add(New Label With {.Text = i.ToString, .Tag = “whatever”}, 0, 0)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 7th, 2022, 06:00 AM
#4
Addicted Member
Re: How to change properties of added control in table layout panel
If you must change the property after initialization there is a Find method on control collection. Ive found that initializing some properties related to UI will have no effect unless the control is visible on the form.
You could do this on the strict condition that you know the following control name is a textbox, for example.
Code:
Dim Tbox As TextBox = DirectCast(tlp.Controls.Find("ControlName", True).First, TextBox)
-
Nov 7th, 2022, 12:59 PM
#5
Thread Starter
Hyperactive Member
Re: How to change properties of added control in table layout panel
FANTASTIC!
Thanks all for contribution ♥
Can someone describe it too, to "how add a row/column and make it sizetype of 50% instantly?" or do it for all in a loop? This can be a different topic but it was mentioned earlier in starting comment.
I used what microsoft suggested here but it works for first 2, I'm not sure what's wrong with it but am suspected to the way I put fifty percent.
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
i += 1
TableLayoutPanel1.Controls.Add(New Label With {.Text = i.ToString, .Dock = DockStyle.Fill, .TextAlign = ContentAlignment.MiddleCenter}, i - 1, 0)
'MAKE THEM ALL 50% HOW?????
Dim styles As TableLayoutColumnStyleCollection = Me.TableLayoutPanel1.ColumnStyles
For Each style As ColumnStyle In styles
style.SizeType = SizeType.Percent
style.Width = 50.0!
Next
End Sub
-
Nov 7th, 2022, 03:16 PM
#6
Re: How to change properties of added control in table layout panel
 Originally Posted by pourkascheff
FANTASTIC!
Thanks all for contribution ♥
Can someone describe it too, to "how add a row/column and make it sizetype of 50% instantly?" or do it for all in a loop? This can be a different topic but it was mentioned earlier in starting comment.
I used what microsoft suggested here but it works for first 2, I'm not sure what's wrong with it but am suspected to the way I put fifty percent.
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
i += 1
TableLayoutPanel1.Controls.Add(New Label With {.Text = i.ToString, .Dock = DockStyle.Fill, .TextAlign = ContentAlignment.MiddleCenter}, i - 1, 0)
'MAKE THEM ALL 50% HOW?????
Dim styles As TableLayoutColumnStyleCollection = Me.TableLayoutPanel1.ColumnStyles
For Each style As ColumnStyle In styles
style.SizeType = SizeType.Percent
style.Width = 50.0!
Next
End Sub
Presumably the third one isn't working because the three columns can't take up 150% of the remaining space.
-
Nov 8th, 2022, 12:38 AM
#7
Thread Starter
Hyperactive Member
Re: How to change properties of added control in table layout panel
So you mean the value should be a coefficient of remaining space? My prior point was the character "!" next to the percentage.
Is there another way? consider "you opened columns collection window, selected them all, marked them all on percentage sizetype and gave even an 100% value. It still shrinks them all fix and space will be divided equally among all members", in a programmatically way...
-
Nov 8th, 2022, 01:39 AM
#8
Re: How to change properties of added control in table layout panel
 Originally Posted by pourkascheff
My prior point was the character "!" next to the percentage.
That's just forcing the literal value to be type Single, where it would be Double otherwise.
-
Nov 8th, 2022, 05:22 AM
#9
Addicted Member
Re: How to change properties of added control in table layout panel
Ive made a quick snippet for you to look at which you could modify for your purpose
Code:
'get the columns list from somewhere
Dim ColList As New List(Of String) From {"Lbale1", "Label2", "Label3"}
'create TLP in code, not drag/drop
Dim TLPMain As New TableLayoutPanel With {.Dock = DockStyle.Fill, .ColumnCount = ColList.Count, .RowCount = 3}
'Add TLP to form
Controls.Add(TLPMain)
'Add some rowsstyles
TLPMain.RowStyles.Add(New RowStyle With {
.SizeType = SizeType.Absolute,
.Height = 20})
TLPMain.RowStyles.Add(New RowStyle With {
.SizeType = SizeType.Absolute,
.Height = 25})
'Last row probably not needed but I do this for piece of mind
TLPMain.RowStyles.Add(New RowStyle With {
.SizeType = SizeType.Percent,
.Height = 100})
'Iterate your column list
For i As Integer = 0 To ColList.Count - 1
'Create labels for row1
Dim Lbl As New Label With {
.Name = "label" & ColList.Item(i),
.Text = ColList.Item(i)}
'Create textbox (or labels) for row2
Dim Tbox As New TextBox With {
.Name = "textbox" & ColList.Item(i)}
'Add Column Style to TLP
TLPMain.ColumnStyles.Add(New ColumnStyle With {
.SizeType = SizeType.Percent,
.Width = 50})
'add label/textbox controls to tlp
TLPMain.Controls.Add(Lbl, i, 0)
TLPMain.Controls.Add(Tbox, i, 1)
Next
-
Nov 13th, 2022, 02:13 PM
#10
Thread Starter
Hyperactive Member
Re: How to change properties of added control in table layout panel
My approach was to set first one manually and force the rest to 50% like first code.
*Update: It could be rectified also by changing 50% to all 100% in a single line very easily:
Code:
For index = 0 To Table.ColumnCount - 1
Table.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100.0F))
Next
I might change title of the thread because it doesn't cover following queation:
How can I access a property of a control inside of a user control programmatically? =)))
I already tried: (Code doesn't work)
Code:
TableLayoutPanel1.Controls.Add(New UserControl1 With {.Textbox1.Text = "SOMETHING"}, 0, 0)
Does not open the possible/recommended terms to use corresponding to the controls of added user-controls.
Last edited by pourkascheff; Nov 14th, 2022 at 01:39 AM.
-
Nov 14th, 2022, 03:26 PM
#11
Re: How to change properties of added control in table layout panel
Code:
TableLayoutPanel1.Controls.Add(New UserControl1 With {.Textbox1.Text = "SOMETHING"}, 0, 0)
You should be exposing a .Text property in your UC that then sets (and gets) the .Text of the textbox on the UC. Shouldn't be trying to access the properties of child controls directly like that.
-tg
-
Nov 14th, 2022, 07:41 PM
#12
Re: How to change properties of added control in table layout panel
As tg says, you should have pass-through properties in your user control for such things. Once the control has been created, you can access child controls if they are public but you shouldn't do so. You definitely can't do so via an object initialiser, where you can only access properties of the object being initialised.
If you do this in your user control:
vb.net Code:
Public Property TextBox1Text As String
Get
Return TextBox1.Text
End Get
Set
TextBox1.Text = value
End Set
End Property
then you can do this:
vb.net Code:
TableLayoutPanel1.Controls.Add(New UserControl1 With {.Textbox1Text = "SOMETHING"}, 0, 0)
You should do that for all members of child controls that you want to be accessible fromn the outside, including events, and then declare all your child controls Private.
-
Nov 15th, 2022, 07:35 AM
#13
Thread Starter
Hyperactive Member
Re: How to change properties of added control in table layout panel
Thanks immensely. It worked.
What is your approach to check state of a user-control's control, which you were programmatically added?
Consider a for loop checks a checkbox.checked state in all UserControls which previously programmatically added to that TableLayoutPanel1?
Last edited by pourkascheff; Nov 15th, 2022 at 07:40 AM.
-
Nov 15th, 2022, 07:41 AM
#14
Re: How to change properties of added control in table layout panel
 Originally Posted by pourkascheff
What is your approach to check state of a user-control's control, which you were programmatically added?
Consider a for loop checks a checkbox.checked state in all UserControls which previously programmatically added to that TableLayoutPanel1?
This sort of thing:
vb.net Code:
For Each uc In myTableLayoutPanel.Controls.Cast(Of SomeUserControl)()
If uc.CheckBox1.Checked Then
'...
End If
Next
-
Nov 15th, 2022, 07:52 AM
#15
Thread Starter
Hyperactive Member
Re: How to change properties of added control in table layout panel
wow, thanks mate and congrats for your 109K posts.
Tags for this Thread
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
|