Results 1 to 6 of 6

Thread: How to get a Control cell row and column in TableLayoutPanel?

  1. #1

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

    How to get a Control cell row and column in TableLayoutPanel?

    Hi, I suppose the thread name is totally obvious but here's the better question:

    Consider a changeable row and column TableLayoutPanel matrix-shaped table. Filled with a UserControl. A label in these UserControls (Which are not fix in quantity) should get cell row and column like an address. for a 3x4 tables we would like to have this result:
    0,0 0,1 0,2
    1,0 1,1 1,2
    2,0 2,1 2,2
    3,0 3,1 3,2

    Controls are automatically added as below:
    Code:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            For i = 0 To RowCount.Value - 1
                For j = 0 To ColumnCount.Value - 1
                    TableLayoutPanel1.Controls.Add(New UserControl1, j, i)
    
                Next
            Next
    End Sub
    * I'm struggling a lot with this Table Layout Panel control in spite of being a very useful component both for reducing coding and improving visual friendly, it is a very tricky control.
    I have a couple more questions. Can I ask them in following replies or moderators recommend me to dedicate threads for them individually? Will I be banned as spammer for it?

  2. #2

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

    Re: How to get a Control cell row and column in TableLayoutPanel?

    For instance first question is repeating the button1 click action/event will cause adding new rows and columns due to .Add(New UserControl1, j, i) method. Is there an .IsExisting subproperty or something like in ListBox control to avoid these kind of unstable response(s)?

  3. #3

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

    Re: How to get a Control cell row and column in TableLayoutPanel?

    And second question is "How can I access added UserControl1s properties?" Consider you have a complicated UserControl in size and want to dock it in fill everytime you add them. What would you do? I remember "With ... EndWith" structure but not sure how to use it.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to get a Control cell row and column in TableLayoutPanel?

    It would probably be a less tricky control if you bothered to read the documentation. If you'd done that then you have already seen that it has a GetCellPosition method.

    Just like every control, a TableLayoutPanel has a Controls collection property. Just like every collection, if you want to know whether it contains an item, you call its Contains method.

    If you want a control to fill its parent container then you set its Dock property to Fill. That goes for any control in any container. In the case of a TableLayoutPanel, the area filled will just be a single cell, rather than the entire panel.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

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

    Re: How to get a Control cell row and column in TableLayoutPanel?

    Right. I got here so far:
    Code:
    Public Class Form2
        Dim i As Integer = 0
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'INCREMENT
            i += 1
            'MAKE GROWING ALL 50%
            TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50.0!))
            'ADD A LABEL FOR INSTENCE
            TableLayoutPanel1.Controls.Add(New Label, TableLayoutPanel1.ColumnCount - 1, 0)
            'MANIPULATE ITS PROPERTIES
            TableLayoutPanel1.Controls.Item(TableLayoutPanel1.ColumnCount - 1).Text = i.ToString()
            TableLayoutPanel1.Controls.Item(TableLayoutPanel1.ColumnCount - 1).Dock = DockStyle.Top
        End Sub
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'MAKE TABLE GROW IN ROWS
            TableLayoutPanel1.GrowStyle = TableLayoutPanelGrowStyle.AddColumns
        End Sub
    End Class
    Questions:
    1) Why by pressing button1 (Incrementing a column filled with a label which contains current i value) make previous text string empty?
    2) Why I don't have access to other 'Label' properties? (Text orientation, Autosize, consider someone have a UserControl in it.)

    I might use wrong code structure but it still runs a bit. If I did, Please tell me where's my fault.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to get a Control cell row and column in TableLayoutPanel?

    Quote Originally Posted by pourkascheff View Post
    1) Why by pressing button1 (Incrementing a column filled with a label which contains current i value) make previous text string empty?
    I'm not really sure what you mean. Can you be specific about exactly what you expect to happen and exactly what does happen.
    Quote Originally Posted by pourkascheff View Post
    2) Why I don't have access to other 'Label' properties? (Text orientation, Autosize, consider someone have a UserControl in it.)
    The Controls collection can contain any type of control, so getting an item from it will return a Control reference. That means that you can only access members of the Control class via that reference. If you want to access members of a specific control type then you have to cast as that type. Obviously the object has to be that type or the cast will fail. If you know that the control is a specific type then you can use DirectCast. If you don't know whether it's that type or not then you can use TryCast.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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