|
-
Jun 10th, 2004, 09:24 PM
#1
Thread Starter
Lively Member
Validation assistance [Resolved]
It may not be tidy but I am trying to fine tune the code below to handle validation.
Code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar.IsNumber(e.KeyChar) = False Then
e.Handled = True
End If
End Sub
Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
If e.KeyChar.IsNumber(e.KeyChar) = False Then
e.Handled = True
End If
It works great at keeping anything but numbers from being entered in either TextBox. I need some suggestions / examples if possible on how to force input in both boxes.
Example: When my form loads it places focus on TextBox1 and the user must enter a number in TextBox1 and then tab over to TextBox2 and place another number in it before they will be allowed to click a button that runs another function in the form. If they don't put input in both TextBoxes I need a MessageBox to show that reminds them to enter numbers in both TextBoxes before they can click any other buttons on the form.
As always, much thanks for replies.
Last edited by teamdad; Jun 13th, 2004 at 08:23 PM.
-
Jun 10th, 2004, 11:22 PM
#2
Sleep mode
This is the perfect event to validate user input . It only release focus to next control if the e.Cancel=False . This is field validation type . You need to do the same with other textbox2 .
VB Code:
Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
If Me.TextBox1.Text = "blah" Then
e.Cancel = False
Else
e.Cancel = True
End If
End Sub
-
Jun 10th, 2004, 11:54 PM
#3
You could also disable the buttons until both the textboxes have been filled.
Or, in the button click event, check to see if a value exists in both the textboxes.
For example
VB Code:
If TextBox1.Text = "" OR TextBox2.Text = "" Then
MessageBox.Show "Fill up the boxes, MORON!"
Exit Sub
End If
'Rest of your code
-
Jun 11th, 2004, 05:44 AM
#4
PowerPoster
Hi,
Why not Make all controls invisible except TextBox1 and a btnExit
In the TextBox1 TextChanged event make a btnConfirmEntry visible and also make TextBox2 invisible (in case you have to make provision for the entries being changed.)
In the click event of btnConfirmEntry validate the entry using something like
If TextBox2.Visible=True then
(Validate TextBox2 entry, make btnConfirmEntry invisible
and btnCalculate visible)
else
(Validate TextBox1 entry and make TextBox2 visible)
End if
Elementary but visually effective as it concentrates attention on the relative textboxes.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jun 11th, 2004, 07:23 PM
#5
Thread Starter
Lively Member
I used the modified code below to get a combination that I think will fit best. It throws the MessageBox and prevents other functions perfectly but..... the MessageBox doesn't have a title in the title bar. Something like "Error Message" would be nice to notify the user what the MessageBox is telling them other than the text it contains "You must enter a range first!"
Code:
If TextBox1.Text = "" Or TextBox2.Text = "" Then
MessageBox.Show("You must enter a range first!")
Else
Label1.Text = ""
Label2.Text = ""
Label3.Text = ""
Label4.Text = ""
Label5.Text = ""
Label6.Text = ""
TextBox3.Text = Val(Label1.Text)
Exit Sub
End If
I'm still not experienced enough to know if there is a way to build a title in the titlebar of a MessageBox.
-
Jun 11th, 2004, 08:02 PM
#6
PowerPoster
Hi,
Very simple. Just add another string.
MessageBox.Show("You must enter a range first!", "Error Message")
That quick enough for you
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jun 11th, 2004, 08:14 PM
#7
Thread Starter
Lively Member
You guys rock!!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|