#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    5
    Rep Power
    0

    How to refer to a textbox that is created in code?


    I have 6 of these types of subs in my program. They create textboxes and add the control to a flow panel and then name the control based on the value of a variable. These textboxes are created by a loop based off of a number picker.

    PHP Code:
    Sub UtilControl()
            
    Dim txtUtil As TextBox

            txtUtil 
    = New TextBox
            txtUtil
    .Width 50
            flpAnalyze
    .Controls.Add(txtUtil)
            
    txtUtil.Name "txtUtil" intResCount
        End Sub 
    I need to do calculations based on the values users type in these text boxes but the code doesn't build because my calculation code is referring to textbox's that aren't yet created. What is the work around for this problem?
    Thanks in advance for your time.
    Last edited by dominofoe; October 23rd, 2013 at 09:59 AM. Reason: typo's - more clarification
  2. #2
  3. No Profile Picture
    Grumpier old Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2003
    Posts
    14,424
    Rep Power
    4539
    It's hard to tell from the isolated snip of code you posted, but it sounds to me like you need to re-think the flow of your code and/or your naming scheme for the controls you create.
    ======
    Doug G
    ======
    The man who doesn't read good books has no advantage over the man who can't read them.
    --Mark Twain
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    5
    Rep Power
    0

    Here is everything


    Originally Posted by Doug G
    It's hard to tell from the isolated snip of code you posted, but it sounds to me like you need to re-think the flow of your code and/or your naming scheme for the controls you create.
    Here is my whole code. I am creating the textboxs with code and naming the groups of boxs the same way except for the number at the end of the name which is based on the value of intResCount.
    if the number picker is set to 1. i should have 6 textboxes created which i do. they should be named all differently but all should end with a 1. if the number picker is set to 2. i should have 12 textboxes created, which i do. i will now have to sets of textboxs, the first set ending in 1 and the second set ending in 2. and so on. Am i actually naming the boxes when adding the variable value to the end of the textbox name? i don't know how to test for this. and since these textboxes are created in real time, how can i refer to them before they are created?


    PHP Code:
    Public Class frmTest
        Dim intResCount 
    As Integer
        Dim intRes 
    As Integer

        Sub ClearLabels
    ()
            
    lblRes1.Visible False
            lblRes2
    .Visible False
            lblRes3
    .Visible False
            lblRes4
    .Visible False
            lblRes5
    .Visible False
            lblRes6
    .Visible False
            lblRes7
    .Visible False
            lblRes8
    .Visible False
            lblRes9
    .Visible False
            lblRes10
    .Visible False

        End Sub

        Sub DemandControl
    ()
            
    Dim txtDemand As TextBox

            txtDemand 
    = New TextBox
            txtDemand
    .Width 50
            flpResources
    .Controls.Add(txtDemand)
            
    txtDemand.Name "txtDemand" intResCount
        End Sub

        Sub CapacityControl
    ()
            
    Dim txtCapacity As TextBox

            txtCapacity 
    = New TextBox
            txtCapacity
    .Width 50
            flpAnalyze
    .Controls.Add(txtCapacity)
            
    txtCapacity.Name "txtCapacity" intResCount
        End Sub

        Sub WorkersControl
    ()
            
    Dim txtWorkers As TextBox

            txtWorkers 
    = New TextBox
            txtWorkers
    .Width 50
            flpResources
    .Controls.Add(txtWorkers)
            
    txtWorkers.Name "txtWorkers" intResCount
        End Sub

        Sub UtilControl
    ()
            
    Dim txtUtil As TextBox

            txtUtil 
    = New TextBox
            txtUtil
    .Width 50
            flpAnalyze
    .Controls.Add(txtUtil)
            
    txtUtil.Name "txtUtil" intResCount
        End Sub

        Sub HoursControl
    ()
            
    Dim txtHours As TextBox

            txtHours 
    = New TextBox
            txtHours
    .Width 50
            flpResources
    .Controls.Add(txtHours)
            
    txtHours.Name "txtHours" intResCount

        End Sub

        Sub Bottleneck
    ()
            
    Dim txtBottleneck As TextBox

            txtBottleneck 
    = New TextBox
            txtBottleneck
    .Width 50
            flpAnalyze
    .Controls.Add(txtBottleneck)
            
    txtBottleneck.Name "txtBottleneck" intResCount
        End Sub

        

        
    Private Sub nprResources_ValueChanged(sender As ObjectAs EventArgsHandles nprResources.ValueChanged

            intRes 
    nprResources.Value
            intResCount 
    0

            Select 
    Case nprResources.Value

                
    Case 1

                    flpResources
    .Controls.Clear()
                    
    flpAnalyze.Controls.Clear()
                    
    ClearLabels()

                    Do 
    Until intResCount intRes

                        On Error Resume Next

                        DemandControl
    ()
                        
    WorkersControl()
                        
    HoursControl()
                        
    UtilControl()
                        
    CapacityControl()
                        
    Bottleneck()

                        
    intResCount intResCount 1
                    Loop

                    lblRes1
    .Visible True
                    lblRes6
    .Visible True

                
    Case 2

                    flpResources
    .Controls.Clear()
                    
    flpAnalyze.Controls.Clear()
                    
    ClearLabels()

                    Do 
    Until intResCount intRes

                        On Error Resume Next

                        DemandControl
    ()
                        
    WorkersControl()
                        
    HoursControl()
                        
    UtilControl()
                        
    CapacityControl()
                        
    Bottleneck()

                        
    intResCount intResCount 1
                    Loop

                    lblRes1
    .Visible True
                    lblRes2
    .Visible True
                    lblRes6
    .Visible True
                    lblRes7
    .Visible True

                
    Case 3

                    flpResources
    .Controls.Clear()
                    
    flpAnalyze.Controls.Clear()
                    
    ClearLabels()

                    Do 
    Until intResCount intRes

                        On Error Resume Next

                        DemandControl
    ()
                        
    WorkersControl()
                        
    HoursControl()
                        
    UtilControl()
                        
    CapacityControl()
                        
    Bottleneck()

                        
    intResCount intResCount 1
                    Loop

                    lblRes1
    .Visible True
                    lblRes2
    .Visible True
                    lblRes3
    .Visible True
                    lblRes6
    .Visible True
                    lblRes7
    .Visible True
                    lblRes8
    .Visible True

                
    Case 4

                    flpResources
    .Controls.Clear()
                    
    flpAnalyze.Controls.Clear()
                    
    ClearLabels()

                    Do 
    Until intResCount intRes

                        On Error Resume Next

                        DemandControl
    ()
                        
    WorkersControl()
                        
    HoursControl()
                        
    UtilControl()
                        
    CapacityControl()
                        
    Bottleneck()

                        
    intResCount intResCount 1
                    Loop
                    lblRes1
    .Visible True
                    lblRes2
    .Visible True
                    lblRes3
    .Visible True
                    lblRes4
    .Visible True
                    lblRes6
    .Visible True
                    lblRes7
    .Visible True
                    lblRes8
    .Visible True
                    lblRes9
    .Visible True

                
    Case 5

                    flpResources
    .Controls.Clear()
                    
    flpAnalyze.Controls.Clear()
                    
    ClearLabels()

                    Do 
    Until intResCount intRes

                        On Error Resume Next

                        DemandControl
    ()
                        
    WorkersControl()
                        
    HoursControl()
                        
    UtilControl()
                        
    CapacityControl()
                        
    Bottleneck()

                        
    intResCount intResCount 1
                    Loop

                    lblRes1
    .Visible True
                    lblRes2
    .Visible True
                    lblRes3
    .Visible True
                    lblRes4
    .Visible True
                    lblRes5
    .Visible True
                    lblRes6
    .Visible True
                    lblRes7
    .Visible True
                    lblRes8
    .Visible True
                    lblRes9
    .Visible True
                    lblRes10
    .Visible True

            End Select
        End Sub

        
    Private Sub cboUnits_SelectedIndexChanged(sender As ObjectAs EventArgsHandles cboPtUnits.SelectedIndexChanged


        End Sub

        
    Private Sub btnProcess_Click(sender As ObjectAs EventArgsHandles btnProcess.Click



        End Sub
    End 
    Class 
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    289
    Rep Power
    44
    You can cycle through all the controls to find a particular type and access any of the properties of that control.
    Code:
        Dim Ctrl As Control
        For Each Ctrl In Form1.Controls()
        If TypeOf Ctrl Is TextBox Then
            Debug.Print Ctrl.Name & " is a " & TypeName(Ctrl)
        End If
        Next Ctrl
    J.A. Coutts
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    5
    Rep Power
    0

    I am naming text boxes correctly. Back to the main question!


    Originally Posted by couttsj
    You can cycle through all the controls to find a particular type and access any of the properties of that control.
    Code:
        Dim Ctrl As Control
        For Each Ctrl In Form1.Controls()
        If TypeOf Ctrl Is TextBox Then
            Debug.Print Ctrl.Name & " is a " & TypeName(Ctrl)
        End If
        Next Ctrl
    J.A. Coutts
    Thank you. your code worked and i now know that i am naming the textboxes correctly. Now how do i reference them. When i put code in a button that references txtDemand1, vb doesnt know what i am talking about because txtDemand1 isn't created until after the program is running. AKA i get build errors.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    289
    Rep Power
    44
    Originally Posted by dominofoe
    Thank you. your code worked and i now know that i am naming the textboxes correctly. Now how do i reference them. When i put code in a button that references txtDemand1, vb doesnt know what i am talking about because txtDemand1 isn't created until after the program is running. AKA i get build errors.
    There are many ways of doing that. Once you have identified the index number of the control in the control collection, you can access it by that index value. Here I have printed the controls in the order in which they were added to the form.
    Code:
        Dim N%
        For N% = Form1.Controls.Count - 1 To 0 Step -1
            Debug.Print Form1.Controls(N%).Name & " is a " & TypeName(Form1.Controls(N%))
        Next N%
    J.A. Coutts

IMN logo majestic logo threadwatch logo seochat tools logo