none
Dinamically added Textbox in TablelLayoutPanel is not visible RRS feed

  • Question

  • HI

    I have dynamically added some textbox controls in TableLayoutPanel named "tlb". The textboxes are generated but not visible in the TableLayoutPanel. Here is my code:

    Dim txtDefault(30) As TextBox
    Dim txtMin(30) As TextBox
       
    Private Sub cctxt()
    
            Dim txt As TextBox
            txt = New TextBox
    
            For i = 1 To tlb.RowCount
    
                txtDefault(i) = txt
                tlb.Controls.Add(txtDefault(i), 1, i)
    
                With txtDefault(i)
                    .Width = 74
                    .Height = 28
                    .Dock = DockStyle.Fill
                    .Font = New Font("Georgia", 8)
                    .BackColor = System.Drawing.Color.Green
                    .ForeColor = System.Drawing.Color.Black
                    .TextAlign = HorizontalAlignment.Center
                    .BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
                    .TabIndex = 46
                    .Visible = True
                End With
                AddHandler txtDefault(i).KeyPress, AddressOf frmActual_Data.decimalKeyHandler
    
                txtMin(i) = txt
                tlb.Controls.Add(txtMin(i), 2, i)
    
                With txtMin(i)
                    .Width = 74
                    .Height = 28
                    .Dock = DockStyle.Fill
                    .Font = New Font("Georgia", 8)
                    .BackColor = System.Drawing.Color.Green
                    .ForeColor = System.Drawing.Color.Black
                    .TextAlign = HorizontalAlignment.Center
                    .BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
                    .TabIndex = txtDefault(i).TabIndex + 1
                    .Visible = True
                End With
                AddHandler txtMin(i).KeyPress, AddressOf frmActual_Data.decimalKeyHandler
    
                Next
    End Sub
     

    I have called the procedure from a button.Click. I seems to me that text boxes are generated but not visible. What i am missing? Your assistance will be highly appreciated.

    Regards.

    Tariq

    Saturday, July 7, 2018 5:07 PM

Answers

  • Hi,

    As leshay suggested, you get to add the txt = New TextBox inside the loop otherwise, your will get only one textbox.

            For i = 1 To tlb.RowCount
                txt = New TextBox

    Apart from that, your code should be working fine. Please make sure that the back color of the form or the TableLayoutPanel are not green as you already setting the back color of the textbox as green.

    • Marked as answer by Tariq Hasan Sunday, July 8, 2018 8:54 AM
    Saturday, July 7, 2018 6:13 PM
  • Hi

    Suggest you move the txt = New TextBox inside the For ... Next loop.


    Regards Les, Livingston, Scotland

    Thanks Les. I have done so. But no change. Still the texboxes are hidden inside the TableLayoutPanel, not visible.

    Hi

    One other thing. You are adding the SAME textbox twice,once to column1 and once to column2. Suggest you work with 2 x New TextBoxes.


    Regards Les, Livingston, Scotland

    • Marked as answer by Tariq Hasan Sunday, July 8, 2018 8:54 AM
    Saturday, July 7, 2018 6:23 PM
  • Hi

    To get your code to work, I made a couple of small changes.

    You were referencing the columns as base 1 instead of base 0 (this caused the second column to be transferred one row down and into the first column).

    I added some dummy text to enable me to see the indexes.

    Anyway, here is what I got:

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        cctxt()
      End Sub
      Dim txtDefault(8), txtMin(8) As TextBox
      Private Sub cctxt()
    
        For i As Integer = 1 To tlb.RowCount
    
          txtDefault(i) = New TextBox
          tlb.Controls.Add(txtDefault(i), 0, i)
    
          With txtDefault(i)
            .Width = 74
            .Height = 28
            .Dock = DockStyle.Fill
            .Font = New Font("Georgia", 12)
            .BackColor = Color.LightGreen
            .ForeColor = Color.Black
            .TextAlign = HorizontalAlignment.Center
            .BorderStyle = BorderStyle.Fixed3D
            .TabIndex = 46 'tabInd
            .Visible = True
            .Text = i.ToString
          End With
          AddHandler txtDefault(i).KeyPress, AddressOf DUMMY
    
          txtMin(i) = New TextBox
          tlb.Controls.Add(txtMin(i), 1, i)
    
          With txtMin(i)
            .Width = 74
            .Height = 28
            .Dock = DockStyle.Fill
            .Font = New Font("Georgia", 12)
            .BackColor = Color.Pink
            .ForeColor = Color.Black
            .TextAlign = HorizontalAlignment.Center
            .BorderStyle = BorderStyle.Fixed3D
            .TabIndex = txtDefault(i).TabIndex + 1
            .Visible = True
            .Text = i.ToString
          End With
          AddHandler txtMin(i).KeyPress, AddressOf DUMMY
        Next
      End Sub
    
      Sub DUMMY(sender As Object, e As KeyPressEventArgs)
    
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Tariq Hasan Sunday, July 8, 2018 8:54 AM
    Saturday, July 7, 2018 6:42 PM

All replies

  • Hi

    Suggest you move the txt = New TextBox inside the For ... Next loop.


    Regards Les, Livingston, Scotland

    Saturday, July 7, 2018 5:14 PM
  • Hi

    Suggest you move the txt = New TextBox inside the For ... Next loop.


    Regards Les, Livingston, Scotland

    Thanks Les. I have done so. But no change. Still the texboxes are hidden inside the TableLayoutPanel, not visible.

    Saturday, July 7, 2018 5:37 PM
  • Hi,

    As leshay suggested, you get to add the txt = New TextBox inside the loop otherwise, your will get only one textbox.

            For i = 1 To tlb.RowCount
                txt = New TextBox

    Apart from that, your code should be working fine. Please make sure that the back color of the form or the TableLayoutPanel are not green as you already setting the back color of the textbox as green.

    • Marked as answer by Tariq Hasan Sunday, July 8, 2018 8:54 AM
    Saturday, July 7, 2018 6:13 PM
  • Hi

    Suggest you move the txt = New TextBox inside the For ... Next loop.


    Regards Les, Livingston, Scotland

    Thanks Les. I have done so. But no change. Still the texboxes are hidden inside the TableLayoutPanel, not visible.

    Hi

    One other thing. You are adding the SAME textbox twice,once to column1 and once to column2. Suggest you work with 2 x New TextBoxes.


    Regards Les, Livingston, Scotland

    • Marked as answer by Tariq Hasan Sunday, July 8, 2018 8:54 AM
    Saturday, July 7, 2018 6:23 PM
  • Hi

    To get your code to work, I made a couple of small changes.

    You were referencing the columns as base 1 instead of base 0 (this caused the second column to be transferred one row down and into the first column).

    I added some dummy text to enable me to see the indexes.

    Anyway, here is what I got:

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        cctxt()
      End Sub
      Dim txtDefault(8), txtMin(8) As TextBox
      Private Sub cctxt()
    
        For i As Integer = 1 To tlb.RowCount
    
          txtDefault(i) = New TextBox
          tlb.Controls.Add(txtDefault(i), 0, i)
    
          With txtDefault(i)
            .Width = 74
            .Height = 28
            .Dock = DockStyle.Fill
            .Font = New Font("Georgia", 12)
            .BackColor = Color.LightGreen
            .ForeColor = Color.Black
            .TextAlign = HorizontalAlignment.Center
            .BorderStyle = BorderStyle.Fixed3D
            .TabIndex = 46 'tabInd
            .Visible = True
            .Text = i.ToString
          End With
          AddHandler txtDefault(i).KeyPress, AddressOf DUMMY
    
          txtMin(i) = New TextBox
          tlb.Controls.Add(txtMin(i), 1, i)
    
          With txtMin(i)
            .Width = 74
            .Height = 28
            .Dock = DockStyle.Fill
            .Font = New Font("Georgia", 12)
            .BackColor = Color.Pink
            .ForeColor = Color.Black
            .TextAlign = HorizontalAlignment.Center
            .BorderStyle = BorderStyle.Fixed3D
            .TabIndex = txtDefault(i).TabIndex + 1
            .Visible = True
            .Text = i.ToString
          End With
          AddHandler txtMin(i).KeyPress, AddressOf DUMMY
        Next
      End Sub
    
      Sub DUMMY(sender As Object, e As KeyPressEventArgs)
    
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Tariq Hasan Sunday, July 8, 2018 8:54 AM
    Saturday, July 7, 2018 6:42 PM
  • Thanks the problem is solved now.

    placing "txtDefault(i) = New TextBox" inside the For...next loop is the solution

    Regards to all.

    Tariq

    Sunday, July 8, 2018 8:54 AM