Basic logic help

xceL

New member
Joined
Apr 24, 2009
Messages
4
Programming Experience
Beginner
Okay what I'm trying to do here is error handling.
Pretty much I have a text box called "TEST" and if I put in a value that is not a number or is not an increment of 0.1 then I get an error with the flashing exclamation mark next to my TEST textbox.

I got everything working fine, I just can't work out the logic for the increments of 0.1! ( Meaning the input can be 0.1, 0.2, 0.3..... 9999.9 but not 4.3321 etc)
I figured:
If (CDec(distance) Mod 0.1 > 0) Then
result = errMsg3
End If
Would work, but it doesnt! Any help on this? Thanks.

VB.NET:
    Private Sub TEST_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TEST.Leave
        ErrorProvider.SetError(test, controlsCheck(TEST.Text))
    End Sub
VB.NET:
    Public Function distanceCheck(ByVal distance As String)
        Dim result As String
        Dim total As Integer
        Const errMsg As String = "Field should not be empty"
        Const errMsg2 As String = "Field should be more than 0.1"
        Const errMsg3 As String = "Field must be in increments of 0.1"
        Const errMsg4 As String = "Field must be an integer"
        result = ""

        If IsNumeric(distance) = False Then
            result = errMsg4
            Return result
        End If
        If (distance.Length = 0) Then
            result = errMsg
        End If
        If (CDec(distance) Mod 0.1 > 0) Then
            result = errMsg3
        End If
        If (CDec(distance) < 0.1) Then
            result = errMsg2
        End If

        Return result
    End Function
 
Last edited:
This will validate all possible input values. If incorrect, it will clear the textbox and reset the focus for a new input. If the increment has more than one place after the decimal point, it will delete the extra digits. A messagebox will inform the user if input is incorrect or if it has been changed. No messagebox will pop up if input is correct.

The TryParse method will test for an empty textbox or non-numeric text in the first If block. String manipulation in the last If block will correct the input if increment is too great. I assume a number larger than 0 is permitted, followed by an increment of at least .1.


VB.NET:
	Private Sub Textbox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
		Dim test As String, number As Double, loca, ln As Integer, ok As Boolean
		test = TextBox1.Text

		ok = Double.TryParse(test, number)
		If Not ok Then
			MessageBox.Show("Field must be a number more than 0.1 in increments of 0.1")
			TextBox1.Clear()
			TextBox1.Focus()
			Exit Sub
		ElseIf number < 0.1 Then
			MessageBox.Show("Field should be more than 0.1")
			TextBox1.Clear()
			TextBox1.Focus()
			Exit Sub
		End If

		loca = test.IndexOf(".")
		If loca = -1 Then
			MessageBox.Show("Field must be in increments of 0.1")
			TextBox1.Clear()
			TextBox1.Focus()
		ElseIf loca > -1 Then
			ln = test.Substring(loca).Length
			If ln < 2 Or ln > 1 AndAlso test.Substring(loca + 1, 1) = "0" Then				MessageBox.Show("Field must be in increments of 0.1")
				TextBox1.Clear()
				TextBox1.Focus()
			ElseIf ln > 2 Then
				test = test.Substring(0, loca) & test.Substring(loca, 2)
				TextBox1.Text = test
				MessageBox.Show("Field must be in increments of 0.1", "Corrected")
			End If
		End If
	End Sub
 
Last edited:
Note: I made changes to the code in the above post: Combined the first two If blocks into one; Added another condition to the last If block to make sure the digit following the decimal was not a 0. (Used AndAlso to make sure error would not result if Substring tried to check for a non-existent character.)
 
Last edited:
Why aren't you using the NumericUpDown control?
 
Back
Top