Results 1 to 15 of 15

Thread: [RESOLVED] How to change properties of added control in table layout panel

  1. #1

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    354

    Resolved [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...
    Name:  tablelayoutexample.png
Views: 1361
Size:  6.3 KB

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    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:
    1. 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.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    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)

  4. #4
    Addicted Member
    Join Date
    Jan 2022
    Posts
    211

    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)

  5. #5

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    354

    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

  6. #6
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,474

    Re: How to change properties of added control in table layout panel

    Quote Originally Posted by pourkascheff View Post
    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.

  7. #7

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    354

    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...

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

    Re: How to change properties of added control in table layout panel

    Quote Originally Posted by pourkascheff View Post
    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.

  9. #9
    Addicted Member
    Join Date
    Jan 2022
    Posts
    211

    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

  10. #10

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    354

    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.

  11. #11
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    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:
    1. Public Property TextBox1Text As String
    2.     Get
    3.         Return TextBox1.Text
    4.     End Get
    5.     Set
    6.         TextBox1.Text = value
    7.     End Set
    8. End Property
    then you can do this:
    vb.net Code:
    1. 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.

  13. #13

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    354

    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.

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How to change properties of added control in table layout panel

    Quote Originally Posted by pourkascheff View Post
    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:
    1. For Each uc In myTableLayoutPanel.Controls.Cast(Of SomeUserControl)()
    2.     If uc.CheckBox1.Checked Then
    3.         '...
    4.     End If
    5. Next

  15. #15

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    354

    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
  •  



Click Here to Expand Forum to Full Width