Question How to set TableLayoutPanel cell's size programitacally

kfirba

Well-known member
Joined
Dec 29, 2012
Messages
77
Programming Experience
1-3
I'm trying to make a TableLayoutPanel with PictureBox + Label in each cell. I have got it all right but I just can't set the cells sizes to be the same! I'm trying to have 4 columns with endless number of rows, and I would like the cell to be in the Label width, unless the Label width is smaller than thePicture width.
For now, my code pretty much works, it just doesn't set the cell size because I have no idea how to do it.

Output image at the bottom of the post

Here is my code:

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim movieN As Integer = MoviesDataSet.movies.Rows.Count
        Dim tablePanel As New TableLayoutPanel


        With tablePanel
            .Size = New Point(650, 450)
            .ColumnCount = 4
            .GrowStyle = TableLayoutPanelGrowStyle.AddRows
            .AutoScroll = True
            .Margin = New System.Windows.Forms.Padding(0)
            .Location = New System.Drawing.Point(5, 50)
            .CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset
        End With
       


        For Each MovieRow As DataRow In MoviesDataSet.Tables("movies").Rows
 
            Dim myLabel As New Label
            Dim myPicture As New PictureBox
            Dim container As New Panel
          
         
            myLabel.Text = MovieRow("movieName")
            myLabel.Location = New System.Drawing.Point(30, 110)
            With myPicture
                .Image = Image.FromFile(MovieRow("moviePhoto"))
                .Tag = MovieRow("ID")
                .Size = New System.Drawing.Size(100, 100)
                .SizeMode = PictureBoxSizeMode.StretchImage
                .Location = New System.Drawing.Point(2, 2)
                .Cursor = Cursors.Hand
            End With


          
            With container
                .Dock = DockStyle.Fill
                .Margin = New System.Windows.Forms.Padding(0)
                .Controls.Add(myPicture)
                .Controls.Add(myLabel)
            End With




            With tablePanel.Controls


                .Add(container)


            End With


            AddHandler myPicture.Click, AddressOf MyPictureClickEvent
        Next
        Me.Controls.Add(tablePanel)
    End Sub




The output I get:



As you can see, the cell's width and height is just not right >.<

Thanks in advance!
 
Just to make it clearer, you created the flow panel and the picture box and the label at design?
A UserControl is something you create in designer, you design it just like you design a form. "New MovieTile" creates new instances of the example UserControl. You are not familiar with the New keyword?
'flow' is just a reference to a FlowLayoutPanel, you can add it in designer or write code manually if you need to.
 
A UserControl is something you create in designer, you design it just like you design a form. "New MovieTile" creates new instances of the example UserControl. You are not familiar with the New keyword?
'flow' is just a reference to a FlowLayoutPanel, you can add it in designer or write code manually if you need to.

Don't want to annoy you but, UserControl is a component just like windows form? When I right click on the project file, and then press Add, am I going to find the UserControl in there? If that so, I will have to do a small research about that
 
Yes, you can use both the Add-New Item and select the UserControl template or Add-User Control in that menu.
 

Latest posts

Back
Top