Results 1 to 9 of 9

Thread: [2005] Info on TableLayoutPanel

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    115

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

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

    Re: [2005] Info on TableLayoutPanel

    The TableLayoutPanel would be the way to go. See lesson 9 here.
    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

  3. #3
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    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.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    115

    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..
    Last edited by jeffnyc; Jul 31st, 2007 at 10:15 AM.

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

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

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    115

    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?

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

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

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    115

    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.

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

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

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