Click here to Skip to main content
15,903,012 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Suppose: Winform Contains following textboxes...

txtPrice01
txtPrice02
txtPrice03
txtPrice04
txtPrice05
....
....
txtPrice60


txtQty01
txtQty02
txtQty03
txtQty04
txtQty05
....
....
txtQty60


What I have tried:

I am using following code...which did not work as required....


 Dim ItemID As Integer() = {0, 1, 2, 61, 3, 62, 14, 13, 63, 12, 11, 64, 18, 16, 15, 65, 8, 66, 9, 10, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60}
 Dim n as string 

 For i As Integer = 1 To 60
        If i < 10 Then n = "0" + i.ToString() Else n = i.ToString()

        txtQty = DirectCast("txtQty" + n, TextBox)
        txtPrice = DirectCast("txtPrice" + n, TextBox)

        txtQty.Clear()
        txtPrice.Text = ITM.PriceSalesSP(ItemID(i))
 Next
Posted
Updated 20-Jun-17 22:45pm
Comments
Richard MacCutchan 21-Jun-17 5:05am    
120 textboxes on a single form?

1 solution

Create an array of TextBox for the two type of box you are using, assign the form controls to the array and your done.

To get the index of the control you can substring and parse the last two digits of the name or you can put the index into the Tag field of the control (bit more work involved).

VB
Dim Quantities(61) as TextBox
Dim Prices(61) As TextBox

For Each ctrl As Control in Me.Controls
  If ctrl.Name.StartsWith("txtQty") = True Then
     Dim idx as Integer

     idx = Integer.Parse(ctrl.Name.Substring(ctrl.Name.Length - 2))

     Quantities(idx) = ctrl
   end if
'
' Do the same for Prices
'
Next


Or using the Tag method;

VB
For Each ctrl As Control in Me.Controls
  If ctrl.Name.StartsWith("txtQty") = True Then
     Quantities(ctrl.Tag) = ctrl
   end if
Next


If you put the controls into a container to keep them together then you only need to loop through the containers controls not the whole form, if you are loping the form and have container controls you want to also search you need to iterate the containers separately the .HasChildren = True indicates it is a container with child controls.


Now your set you simply use the TextBox arrays to perform your calculations and manipulations on the controls;

VB
For i As Integer = 1 To 60
       Quantities(i).Clear()
       Prices(i).Text = ITM.PriceSalesSP(ItemID(i))
Next
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900