Jad Jad - 7 months ago 12
Vb.net Question

shortening code for a big amount of textboxes

I have a big number of textboxes in my application contained in a panel. The textboxes are grouped by 4. Example: l1,w1,q1,p1,l2,w2,q2,p2,.... The code inside is working perfectly but taking so much time while compiling.

If l24.Text <> "" And w24.Text <> "" And q24.Text <> "" And p24.Text <> "" Then
counter += 1
Dim a, b, c, d As Integer
a = Convert.ToInt16(p24.Text.Substring(0, 1))
b = Convert.ToInt16(p24.Text.Substring(1, 1))
c = Convert.ToInt16(p24.Text.Substring(2, 1))
d = Convert.ToInt16(p24.Text.Substring(3, 1))

If a = 4 Or a = 5 Or b = 4 Or b = 5 Or c = 4 Or c = 5 Or d = 4 Or d = 5 Then
forpress += (Convert.ToInt16(l24.Text) + pressing) * (Convert.ToInt16(w24.Text) + pressing) * (Convert.ToInt16(q24.Text))
End If
Select Case a
Case 1
half += (Convert.ToInt16(l24.Text) + add) * Convert.ToInt16(q24.Text)
ehalf += Convert.ToInt16(l24.Text) * Convert.ToInt16(q24.Text)
Case 2
two += (Convert.ToInt16(l24.Text) + add) * Convert.ToInt16(q24.Text)
etwo += Convert.ToInt16(l24.Text) * Convert.ToInt16(q24.Text)
Case 3
three += (Convert.ToInt16(l24.Text) + add) * Convert.ToInt16(q24.Text)
ethree += Convert.ToInt16(l24.Text) * Convert.ToInt16(q24.Text)
Case 4
four += (Convert.ToInt16(l24.Text) + add) * Convert.ToInt16(q24.Text)
efour += Convert.ToInt16(l24.Text) * Convert.ToInt16(q24.Text)
Case 5
five += (Convert.ToInt16(l24.Text) + add) * Convert.ToInt16(q24.Text)
efive += Convert.ToInt16(l24.Text) * Convert.ToInt16(q24.Text)
End Select

Select Case b
Case 1
half += (Convert.ToInt16(l24.Text) + add) * Convert.ToInt16(q24.Text)
ehalf += Convert.ToInt16(l24.Text) * Convert.ToInt16(q24.Text)
Case 2
two += (Convert.ToInt16(l24.Text) + add) * Convert.ToInt16(q24.Text)
etwo += Convert.ToInt16(l24.Text) * Convert.ToInt16(q24.Text)
Case 3
three += (Convert.ToInt16(l24.Text) + add) * Convert.ToInt16(q24.Text)
ethree += Convert.ToInt16(l24.Text) * Convert.ToInt16(q24.Text)
Case 4
four += (Convert.ToInt16(l24.Text) + add) * Convert.ToInt16(q24.Text)
efour += Convert.ToInt16(l24.Text) * Convert.ToInt16(q24.Text)
Case 5
five += (Convert.ToInt16(l24.Text) + add) * Convert.ToInt16(q24.Text)
efive += Convert.ToInt16(l24.Text) * Convert.ToInt16(q24.Text)
End Select

Select Case c
Case 1
half += (Convert.ToInt16(w24.Text) + add) * Convert.ToInt16(q24.Text)
ehalf += Convert.ToInt16(w24.Text) * Convert.ToInt16(q24.Text)
Case 2
two += (Convert.ToInt16(w24.Text) + add) * Convert.ToInt16(q24.Text)
etwo += Convert.ToInt16(w24.Text) * Convert.ToInt16(q24.Text)
Case 3
three += (Convert.ToInt16(w24.Text) + add) * Convert.ToInt16(q24.Text)
ethree += Convert.ToInt16(w24.Text) * Convert.ToInt16(q24.Text)
Case 4
four += (Convert.ToInt16(w24.Text) + add) * Convert.ToInt16(q24.Text)
efour += Convert.ToInt16(w24.Text) * Convert.ToInt16(q24.Text)
Case 5
five += (Convert.ToInt16(w24.Text) + add) * Convert.ToInt16(q24.Text)
efive += Convert.ToInt16(w24.Text) * Convert.ToInt16(q24.Text)
End Select

Select Case d
Case 1
half += (Convert.ToInt16(w24.Text) + add) * Convert.ToInt16(q24.Text)
ehalf += Convert.ToInt16(w24.Text) * Convert.ToInt16(q24.Text)
Case 2
two += (Convert.ToInt16(w24.Text) + add) * Convert.ToInt16(q24.Text)
etwo += Convert.ToInt16(w24.Text) * Convert.ToInt16(q24.Text)
Case 3
three += (Convert.ToInt16(w24.Text) + add) * Convert.ToInt16(q24.Text)
ethree += Convert.ToInt16(w24.Text) * Convert.ToInt16(q24.Text)
Case 4
four += (Convert.ToInt16(w24.Text) + add) * Convert.ToInt16(q24.Text)
efour += Convert.ToInt16(w24.Text) * Convert.ToInt16(q24.Text)
Case 5
five += (Convert.ToInt16(w24.Text) + add) * Convert.ToInt16(q24.Text)
efive += Convert.ToInt16(w24.Text) * Convert.ToInt16(q24.Text)
End Select

End If


This is an example for one set of textboxes (l24,w24,q24,p24). How can i join all the sets of textboxes to minimize my code lines and compiling time ?
Any help is really appreciated

Answer

You can use two-dimensional arrays. For example, you have 10 groups of 4 TextBox each.

You would use:

Dim Values(9, 3) As String

because you have 10 groups (0 ~ 9) of 4 elements (0 ~ 3) each.

For example, if you want to read the value from the first TextBox in the sixth group:

You would use:

Values(5, 0) = l6.Text

So the first value in this case refers to the group number and the second refers to the number in that group. It also works the other way around if you prefer. This certainly will reduce the size of your code.

Hope this helped.