This is not a question it is a tip. It may help someone else.
I have a group of 25 labels named Label1, Label2, Label3, ... Label25 in a square of 5 per row, 5 per column and I wanted to assign the backcolor to all of them on a loop and assign a value of "True" or "False" to a matrix of 25 boolean values representing them.
But I was getting errors on the pattern created.
My approach was to use a substring containing a number copied from the label names:
Num = Val(sender.name.substring(5))
And the subindexes of the matrix:
p = (Num - 1) / 5
q = (Num - 1) Mod 5
The values for the lower corner of the matrix were rouded to 5 instead of being 4. Instead of (4,0) ,(4,1), (4,2), (4,3) (4,4) I was getting (5,0),(5,1)...(5,5)
So I solved the problem by creating two single variables "A" and "B", doing the divission and MOD operations on them, finding the floor of them and then converting to integer:
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
Label1.Click, Label2.Click, Label3.Click, Label4.Click, Label5.Click, _
Label6.Click, Label7.Click, Label8.Click, Label9.Click, Label10.Click, _
Label11.Click, Label12.Click, Label13.Click, Label14.Click, Label15.Click, _
Label16.Click, Label17.Click, Label18.Click, Label19.Click, Label20.Click, _
Label21.Click, Label22.Click, Label23.Click, Label24.Click, Label25.Click
If cbManual.Checked = True Then
Dim A, B As Single
Dim Num, p, q As Integer
'Avoid Problem Of Rounding Up
Num = Val(sender.name.substring(5))
A = Math.Floor((Num - 1) / 5)
p = CInt(A)
B = Math.Floor((Num - 1) Mod 5)
q = CInt(B)/>
If sender.backcolor = Color.Black Then
sender.backcolor = Color.Red
PX(p, q) = True
Else
sender.backcolor = Color.Black
PX(p, q) = False
End If
End If
End Sub
This may be a bad side effect if you are wanting to truncate a number. Don't trust all the operations, do a side check. You will avoid getting some bugs.
I hope this helps someone.
ricardosms.

New Topic/Question
Reply



MultiQuote




|