Text Boxes with Similar Names txt1, txt2 and so forth


Author
Message
Terry Bottorff
Terry Bottorff
StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)
Group: Forum Members
Posts: 448, Visits: 12K
I have several text boxes on a SF win Form with similar names txt1 through txt10 and I have an array ar(0) through ar(9) that has integers stored. I would like to set up a loop that would let me store the following:



me.txt1.text = ar(0)

me.txt2.text = ar(1)



and so forth. Is there a way to do this using a loop of some sort? I also know I need to convert the integers to strings I think. TIA.
Replies
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (4.8K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Terry Bottorff (02/18/2008)
OK I understand and that is OK since I got it working Trents way and that is cool. But, is there a way to put this in a loop instead of typing them all?



myControls.Add(txt1)

myControls.Add(txt2)

myControls.Add(txt3)



I tried this but it would not work



for cnt = 1 to 40



cntrl = "txt" & cnt.tostring()



myControls.Add(cntrl)



next cnt



Thanks again.




You need to put the actual controls into the list/array, rather than the name of the control. I.e. in you top example, you are putting the actual control into the collection and within the loop you are putting the name into the collection. If you use the method Trent/Ben are suggesting, you will need to put them in individually, then after that you can easily loop through the collection to do whatever you need to them. Best bet would be to have a private method to just load them, then you have one place to update if you change the controls being loaded. If you want to loop, then you need to know what container they are in and you could use the method I suggested, to use a loop to build the names. A third option would be to loop, create the controls (not the name, but the actual control), add them to your collection and add them to the appropriate container on the form.



After reading this, I'd suggest you definitely use your own collection to do manage the controls. As mentioned, you can get into trouble if the controls are in different containers etc. If you use your own collection and then have a method to load that collection, you can then mess with how you are loading it, but the rest of your code is unaffected. Hope that makes sense.
Trent Taylor
Trent Taylor
StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 7K
Really, automating the process is better by creating a recursive method like Greg was talking about.  And instead of testing on a name, if you know the control type or if a certain value on that control will be set, you could test on it instead of a name:

Private Function GetTextControls(Byval testControls As ControlCollection) As System.Collection.Generic.List(Of Control)
    '-- Establish Locals
    Dim r As New List(Of Control)

    For each ctrl As Control in testControls
        '-- Check for a textbox type
        If ctrl.GetType().IsSubClassOf(GetType(System.Windows.Forms.TextBox)) Then
             '-- Add the control to the collection
             r.Add(ctrl)
        End If

       '-- See if there are additional children to check for
       If ctrl.Controls.Count > 0 Then r.AddRange(GetTextControls(ctrl.Controls))
    Next
End Function

Then you could call the method in the form constructor or load like this:

'-- Get all of the text controls
r.AddRange(GetTextControls(Me.Controls))

'-- Then add your logic to cycle through the collection and do whatever you need to
For each ctrl as Control in r
    ctrl.Enabled = False
Next


GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...





Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search