Hello all of you, good and nice VB Net helpers.
Here again, in my private VB 2010 Wrestling Event. By the way, Take a guess: Who is winning?
I wrote a code to draw 10 labels & 10 textboxes. These textboxes are formatted to accept only numbers, positive or negative, integers or decimals.
I gave up, after many days trying to get the textboxes to accept up to 5 characters, including de minus sign, if it is needed, and allowing ONE DECIMAL PLACE.
Then, I have been trying to round the textboxes.text to only ONE DECIMAL PLACE with a LostFocus handler. The code did it, but only with the first textbox of the array, that is because the (i) that defines the elements of the array (tX(i)) is not passed from the main Sub to the Private one, I even had to Dim it again.
This is the code:
Any help is going to be highly appreciated.
Thank you,
Nelson
Here again, in my private VB 2010 Wrestling Event. By the way, Take a guess: Who is winning?
I wrote a code to draw 10 labels & 10 textboxes. These textboxes are formatted to accept only numbers, positive or negative, integers or decimals.
I gave up, after many days trying to get the textboxes to accept up to 5 characters, including de minus sign, if it is needed, and allowing ONE DECIMAL PLACE.
Then, I have been trying to round the textboxes.text to only ONE DECIMAL PLACE with a LostFocus handler. The code did it, but only with the first textbox of the array, that is because the (i) that defines the elements of the array (tX(i)) is not passed from the main Sub to the Private one, I even had to Dim it again.
This is the code:
HTML Code:
Public tX(0 To 9) As TextBox, lb(0 To 9) As Label
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i as integer, m as integer
For i = 0 To 9
lb(i) = New Label
tX(i) = New TextBox
Next
m = 20
For i = 0 To 9
'LABLES TEXT, LOCATION & PROPERTIES
lb(i).Text = "Point " + (i + 1).ToString
lb(i).Anchor = AnchorStyles.Top
lb(i).Anchor = AnchorStyles.Left
lb(i).Top = m + n
lb(i).Left = 20
lb(i).Size = New System.Drawing.Size(86, 25)
lb(i).BorderStyle = System.Windows.Forms.BorderStyle.None
lb(i).Font = New System.Drawing.Font("Arial", 11.0F, System.Drawing.FontStyle.Bold)
lb(i).Enabled = False
lb(i).BackColor = Color.FromArgb(255, 221, 217, 195)
'TEXTBOX tX LOCATION & PROPERTIES
tX(i).Anchor = AnchorStyles.Top
tX(i).Anchor = AnchorStyles.Left
tX(i).Top = m + n
tX(i).Left = 125
tX(i).Size = New System.Drawing.Size(40, 15)
tX(i).BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
tX(i).Font = New System.Drawing.Font("Arial", 10.0F, System.Drawing.FontStyle.Bold)
tX(i).Enabled = False
tX(i).BackColor = Color.FromArgb(255, 217, 217, 217)
tX(i).MaxLength = 5
'´TEXTBOXES ACCEPT ONLY NUMBERS, DOT & MINUS SIGN
AddHandler tX(i).KeyPress, AddressOf TestKeyPress
TEXTBOXES ACCEPT ENTER KEY
AddHandler tX(i).KeyDown, AddressOf textkey
ROUND TEXTBOXES TO ONE DECIMAL
AddHandler tX(i).Leave, AddressOf undecX
'DRAW LABEL & TEXTBOX
Me.Controls.Add(lb(i))
Me.Controls.Add(tX(i))
m = m + 32
next
End Sub
Public Sub TestKeyPress(ByVal source As Object, ByVal e As KeyPressEventArgs)
'tX() ACCEPT ONLY NUMBERS, MINUS SIGN AND DOT
If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) And Not e.KeyChar = "." And Not e.KeyChar = "-" Then
e.Handled = True
End If
End Sub
Private Sub textkey(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
'tX() ACCEPT ENTER KEY
If e.KeyCode = Keys.Enter Then
Me.SelectNextControl(DirectCast(sender, TextBox), True, True, False, True)
End If
End Sub
Private Sub undecX(ByVal sender As System.Object, ByVal e As System.EventArgs)
'ROUND tX(i) TO ONE DECIMAL PLACE
Dim i As Integer
For i = 0 To 9
If tX(i + 1).Text = "" And tX(i).Text <> "" Then
tX(i).Text = String.Format("{00:n1}", Val(tX(i).Text))
End If
Next
End Sub
Thank you,
Nelson