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

Last question, refer to an uncreated textbox

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
    Here is the code I am using.

    Code:
    For i = 1 To tbnumberofitems
                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
    
    
    

    Now what I am trying to do is access the textbox text and store it into an array. I thought I could simply

    Code:
    
            Dim itemname(_numberOfItems) As String        
         _numberOfItems(0) = txt1.text
            MsgBox(test)
    
    
    
    But it returns an error that txt1 doesn't exist, which is true. But it will be created by the program. How do I refer to an as of yet uncreated variable?

    Last question for a while, promise.
     
  2. boomboomer

    boomboomer Executive VIP

    Joined:
    Feb 7, 2008
    Messages:
    705
    Likes Received:
    865
    You have created the textbox with the name txt but are trying to access txt1. That's why the error.

    What you'll have to do is get a list of all textboxes for this new form and then extract their values.

    Do you want help with this?
     
    Last edited: Jun 11, 2013
  3. TehEpidemick

    TehEpidemick BANNED BANNED

    Joined:
    Oct 31, 2011
    Messages:
    778
    Likes Received:
    181
    It creates txt1, txt2, txt3, txt4 whatever value i is, at least thats how I read it. Is this not correct?
     
  4. boomboomer

    boomboomer Executive VIP

    Joined:
    Feb 7, 2008
    Messages:
    705
    Likes Received:
    865
    That's right. But the reference to these textboxes is lost outside the For loop in which they are created.

    If you want to read the values in the textboxes, you're most likely doing it outside the for loop that created the textboxes.

    You'll have to do something like the following:

    Code:
            Dim ctrls As ControlCollection = frm.Controls()
            For Each ctrl In ctrls
                If TypeOf (ctrl) Is TextBox Then
                    Dim txt As TextBox = DirectCast(ctrl, TextBox)
    
                End If
            Next
    
    In here you can get the names and values of the textboxes.

    If you just want the value of a particular textbox, you can do this:

    Code:
    Dim txt1 As TextBox = DirectCast(frm.Controls.Find("txt1", True)(0), TextBox)
    
     
    • Thanks Thanks x 1
    Last edited: Jun 11, 2013
  5. TehEpidemick

    TehEpidemick BANNED BANNED

    Joined:
    Oct 31, 2011
    Messages:
    778
    Likes Received:
    181
    That is exactly what I need.

     
  6. boomboomer

    boomboomer Executive VIP

    Joined:
    Feb 7, 2008
    Messages:
    705
    Likes Received:
    865
    My last post shows how you can retrieve the dynamically created textboxes from a form. Did that work for you?
     
  7. TehEpidemick

    TehEpidemick BANNED BANNED

    Joined:
    Oct 31, 2011
    Messages:
    778
    Likes Received:
    181
    yes, but im not sure exactly how to add this into the coding. im not familiar with ctrls, im looking up tutorials on them now. Im sure this will work as soon as i figure out what it says.
     
  8. boomboomer

    boomboomer Executive VIP

    Joined:
    Feb 7, 2008
    Messages:
    705
    Likes Received:
    865
    Once you have added the textboxes, your users will be able to enter values in them.

    After this, you will want to read these values based on an event. (could be a button click or something else)

    The code that I provided will go in the event handler of this event.
     
  9. TehEpidemick

    TehEpidemick BANNED BANNED

    Joined:
    Oct 31, 2011
    Messages:
    778
    Likes Received:
    181
    Ok one last huge question and I think I have it. Heres what im doing.

    Code:
    Dim i As Integer = 0
            Dim itemnamearray(numberofitems) As String
            Dim ctrls As ControlCollection = Me.Controls()
            While i < numberofitems
                For Each ctrl In ctrls
                    If TypeOf (ctrl) Is TextBox Then
                        Dim txt As TextBox = DirectCast(ctrl, TextBox)
                    End If
                    If i = 1 Then
                        itemnamearray(0) = ctrl.Text
                    Else
                        itemnamearray(i) = ctrl.Text
                    End If
                    i = i + 1
                Next
            End While
    
    Is this right? also how do i get into the array, itemnamearray, from another form, event, whereever. Im trying to find it but not sure what to look for besides a global variable.
     
  10. boomboomer

    boomboomer Executive VIP

    Joined:
    Feb 7, 2008
    Messages:
    705
    Likes Received:
    865
    This is fine but instead of using all the if/else blocks inside the for loop, you'd be better off getting the values from the 10 textboxes with 10 statements like the following one:

    Code:
    Dim txt1 As TextBox = DirectCast(frm.Controls.Find("txt1", True)(0), TextBox)
    
    If the array you want to access is not in the same form/class, you have 2 options:

    1. declare the array as public at the class level when declaring it (not such a good option)
    2. Create a public method in the class which holds the array and use this public method to add elements to the array.
     
    • Thanks Thanks x 1
  11. TehEpidemick

    TehEpidemick BANNED BANNED

    Joined:
    Oct 31, 2011
    Messages:
    778
    Likes Received:
    181
    Do you mean this

    Code:
       public sub
          dim itemnamearray(numberofitems)
       end sub
    
     
  12. boomboomer

    boomboomer Executive VIP

    Joined:
    Feb 7, 2008
    Messages:
    705
    Likes Received:
    865
    It'll be something like this:

    Code:
    Class Form1
       Private _items() As String
       
       Public Sub AddItemToArray(itemName as String, arrayIndex as Integer)
          _items(arrayIndex) = itemName
       End Sub
    
    End Class
    
    This is assuming that your array (_items) is defined in the class Form1

    Ideally though, you should be accessing the textboxes from the same class in which your array is defined. However, for the manner in which you are implementing, this code should work
     
    • Thanks Thanks x 1
    Last edited: Jun 12, 2013
  13. TehEpidemick

    TehEpidemick BANNED BANNED

    Joined:
    Oct 31, 2011
    Messages:
    778
    Likes Received:
    181
    thanks man. id be stuck if you didnt help.
     
  14. theMagicNumber

    theMagicNumber Regular Member

    Joined:
    May 13, 2010
    Messages:
    345
    Likes Received:
    195
    Why don't you declare a
    Dim dictTextBoxes AsNew Dictionary(Of String, TextBox)
    and add the textboxes into the dictionary when you are instancing them. The Key is the name of the TextBox and the Value is the actual reference to the TextBox.


    Iterating over all of the form controls also works, but if you have a lot of controls, the UI might be a litle unresponsive.
     
  15. TehEpidemick

    TehEpidemick BANNED BANNED

    Joined:
    Oct 31, 2011
    Messages:
    778
    Likes Received:
    181
    so if i do this for 100 items, this will make the ui unresponsive?