restricting data that you can put into box?

W

W. Adam

I have a form which displays info. On top of it there is a text box for
input box which lets you put number from 1 thu 112.I need to verify that
the
number entered by the user is within the range of 1-12. If it isn't, notify
the user and set the focus back to the text box. Do not allow processing to
continue until a valid entry is entered in the textbox.
How can i achive that

Thanks in advance
Here is my code whats wrong with it?

Private Sub btnDisplayGifts_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnDisplayGifts.Click

Dim intI, intJ, intNumDays, intDays As Integer

Dim strName, number As String

Dim dblPrice, dblTotalCost, dblGrandTotal As Double

Dim strSr As IO.StreamReader = IO.File.OpenText("Xmas.txt")

intNumDays = CInt(txtNumDays.Text)

If intNumDays <= 0 And intNumDays > 12 Then

Do

MsgBox("Please enter 1 thru 12 ", , " Number of Days")

txtNumDays.Text = ""

txtNumDays.Focus()

Loop Until (intNumDays > 0 And intNumDays < 12)

lstTable.Items.Clear()

lstTable.Items.Add("The gift for the day" & " " & intNumDays & " " & "are:")

End If

lstTable.Items.Clear()

lstTable.Items.Add("The gift for the day" & " " & intDays & " " & "are:")

For intI = 1 To intDays

strName = strSr.ReadLine

dblPrice = CDbl(strSr.ReadLine)

dblTotalCost += dblPrice * intI

lstTable.Items.Add(intI & "." & " " & strName)

Next

lstTable.Items.Add("Cost:" & "" & FormatCurrency(dblTotalCost, 2))

lstTable.Items.Add("")

strSr.Close()

End Sub
 
V

Visual Barty

This is what the if should look like:

If intNumDays >= 0 AndAlso intNumDays <= 12 Then

"AndAlso" is a new operator in vb.net that provides "short-
circuiting", which is simply when the first condition
fails the test it doesn't bother checking the second which
is a performance increase.

That should do you what you need.

Hope this helps.

Bart A. Robinson, MCP
 
B

Brian

This line is wrong: (IntNumDays can't be < 0 and > 12 at the same time)

If intNumDays <= 0 And intNumDays > 12 Then

It should read:

If intNumDays <= 0 Or intNumDays > 12 Then



Also, your DO loop dosen't give anyone a chance to enter the correct number.
It just keeps on going and going and going......

Try this instead:

Private Sub txtNumDay_Validating(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles txtNumDay.Validating

If intNumDays <= 0 And intNumDays > 12 Then


MsgBox("Please enter 1 thru 12 ", , " Number of Days")

txtNumDays.Text = ""

txtNumDays.Focus()

End if

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top