Problem arranging/ accessing controls in TableLayoutPanel created dynamically

vistakon

New member
Joined
Nov 19, 2012
Messages
3
Programming Experience
Beginner
Hi,

I m a newbee in VB.NET and am trying to create a TableLayoutPanel with rows, columns, and controls associated with it being added dynamically.
From the look of it everything appears fine as images and labels seem to show up on the area as I want.

However, when I try accessing the controls, some cells strangely return null (for _analysed values 3, 8,9) when total cell count =10
and some strangely return the panel that appears to be from a different cell (for _analysed values 3, 8,9) when total cell count =10!!

I m getting crazy... and I have been stuck on it since quite some time.
Any help would be greatly appreciated.
Thanks in advance.

VB.NET:
Dim _analysed as integer = 3


 Private Sub SetImage(ByVal path As String)
        Dim cntrl As Control = tblParts.GetControlFromPosition(_analysed / tblParts.RowCount, _analysed Mod tblParts.RowCount)
        If Not cntrl Is Nothing Then
            cntrl.BackgroundImage = Image.FromFile(path)
        End If
 End Sub



' Code for creating the layout
Private Sub SetPartsLayout()


       pnlTableLayoutHolder.SuspendLayout()
        tblParts.SuspendLayout()
        
        tblParts.ColumnStyles.Clear()
        tblParts.RowStyles.Clear()


        tblParts.ColumnCount = 2
        tblParts.RowCount = 5


        Dim w As Integer
        Dim h As Integer


        Dim colCount As Integer = tblParts.ColumnCount
        Dim rowCount As Integer = tblParts.RowCount




        tblParts.Margin = New System.Windows.Forms.Padding(0)
        tblParts.Padding = New System.Windows.Forms.Padding(0)


        If colCount > 1 Then
            w = 40
            h = 40
            tblParts.Width = w * colCount
            tblParts.Height = h * rowCount


            tblParts.GrowStyle = TableLayoutPanelGrowStyle.FixedSize
        Else
            w = 120
            h = 200
            tblParts.Width = w
            tblParts.Height = h
            tblParts.GrowStyle = TableLayoutPanelGrowStyle.FixedSize
        End If


        Dim counter As Integer = 1
        For cc As Integer = 0 To colCount - 1
            tblParts.ColumnStyles.Add(New ColumnStyle(SizeType.AutoSize))


            For rc As Integer = 0 To rowCount - 1


                If counter - 1 < rowCount Then
                    tblParts.RowStyles.Add(New RowStyle(SizeType.AutoSize))
                    tblParts.ResumeLayout(False)
                    tblParts.PerformLayout()
                    tblParts.SuspendLayout()
                End If




                Dim l As Label = New Label()
                l.ForeColor = Color.Black
                l.TextAlign = ContentAlignment.MiddleCenter
                l.Height = h
                l.Width = w
                l.Name = String.Concat("INSERT", counter)
                l.Text = counter
                l.Margin = New System.Windows.Forms.Padding(0)
                l.Padding = New System.Windows.Forms.Padding(0)


                Dim p As Panel = New Panel()
                p.Name = String.Concat("PANEL_INSERT", counter)
                p.BackgroundImage = Image.FromFile(CONST_INSERT_BLUE_IMAGE)
                p.Width = w
                p.Height = h
                p.Margin = New System.Windows.Forms.Padding(0)
                p.Padding = New System.Windows.Forms.Padding(0)
                p.Controls.Add(l)


                tblParts.Controls.Add(p, cc, rc)
                counter = counter + 1
            Next
        Next


        tblParts.ResumeLayout(False)
        tblParts.PerformLayout()
        pnlTableLayoutHolder.ResumeLayout()
    End Sub
 
Last edited:

vistakon

New member
Joined
Nov 19, 2012
Messages
3
Programming Experience
Beginner
Sorry.. if I had taken your time.
Problem doesn't seem to be where I m setting the layout.
However while getting the controls back ... GetControl( _analysed/ Rowcount, ...) .. is taking ceil of the double value that is returned..
Being from C# Background.. I couldn't even think.... what I saw... until I saw !!! :)
In C#... Int/ int .. returns an Int.. by truncating the decimal part !!!

God... it cost me 2 hrs of my time.. made me ridiculous in front of the clients and ..... and don't know how many more hrs of others time put together !!!
 
Top Bottom