1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Create x labels and textboxes

Discussion in 'Visual Basic .NET' started by TehEpidemick, Jun 11, 2013.

  1. TehEpidemick

    TehEpidemick BANNED BANNED

    Joined:
    Oct 31, 2011
    Messages:
    778
    Likes Received:
    181
    Does anyone know a code for creating x textboxes and labels. Heres what I have so far.


    Dim x As Integer = 0 ' looop count '
    Dim y As Integer = 1 ' name count'
    Dim label1name As String = "label"
    Dim textbox1name As String = "textbox"
    While x <= numberofitems
    y = y + 1
    If x = 0 Then y = 1


    Convert.ToString(y)


    Dim label1 As New Label
    label1.Name = label1name & y
    label1.Text = "Item" & y
    label1.SetBounds(x + 5, x + 5, 50, 50)
    Me.Controls.Add(label1)


    Dim textbox1 As New TextBox
    textbox1.Name = textbox1name & y


    'code to create text box


    x = x + 1
    label1name = "label"
    textbox1name = "textbox"


    End While
     
  2. boomboomer

    boomboomer Executive VIP

    Joined:
    Feb 7, 2008
    Messages:
    705
    Likes Received:
    865
    Can you elaborate on what is the outcome you intend to achieve?

    Do you want label - textbox pairs to appear one below another or in some other layout?
     
  3. innozemec

    innozemec Jr. VIP Jr. VIP

    Joined:
    Aug 19, 2011
    Messages:
    5,288
    Likes Received:
    1,799
    Location:
    www.Indexification.com
    Home Page:
  4. boomboomer

    boomboomer Executive VIP

    Joined:
    Feb 7, 2008
    Messages:
    705
    Likes Received:
    865
    Code:
    Public Class Form1
        Private _numberOfItems As Integer = 10
        Private _rowHeight As Integer = 25
        Private _labelColumnWidth As Integer = 100
        Private _leftMargin As Integer = 10
        Private _topMargin As Integer = 10
    
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            For i = 1 To _numberOfItems
                Dim lbl As New Label
                lbl.Text = "Label " & CStr(i)
                lbl.Name = "lbl" & CStr(i)
                lbl.Parent = Me
                lbl.Location = New Point(_leftMargin, _topMargin + (i - 1) * _rowHeight)
    
    
                Dim txt As New TextBox
                txt.Name = "txt" & CStr(i)
                txt.Parent = Me
                txt.Location = New Point(_leftMargin + _labelColumnWidth, _topMargin + (i - 1) * _rowHeight)
            Next
        End Sub
    End Class
    
    Does this achieve what you want?
     
    • Thanks Thanks x 1
  5. neutralhatter

    neutralhatter Jr. VIP Jr. VIP Premium Member

    Joined:
    Jun 23, 2010
    Messages:
    430
    Likes Received:
    330
    you're doing things right, just missing to add the control to the form


    just try this

    me.controls.add(textbox1);
     
    • Thanks Thanks x 1
  6. TehEpidemick

    TehEpidemick BANNED BANNED

    Joined:
    Oct 31, 2011
    Messages:
    778
    Likes Received:
    181
    That pretty much got it.

    What im trying to do is on form1 let the user input how many items he has, and on form2 create that number of labels for each items and a textbox beside it to put the name of the item.