create/use form event/property to validate txtEntry

R

Rich

Hello,

I have 10 textboxes on a form. I would like to validate
text-data entry on each textbox. I was looking for a
Form_Current event or Form_Change event but did not
recognize any such events. Or maybe there is a property
for the Textboxes where I can set data entry conditions?
Wondering about CausesValidation textbox property-how to
use.

I was thinking maybe I could create a custom form event
that would loop through an array of textboxes when the
event is raise and would check the content of each textbox
like as soon as the textbox loses focus. Is there such a
form event? Do I need to create one? How to create this
event and raise the event?

Thanks,
Rich
 
C

Cor

Hi Rich,

Did I not send you an example with that in it?

Set the handler for the lostfocus event?
Using an array of controls?

Cor
 
R

Rich

Yes you did. Thank you. Now I see an application for
this. I don't have dotnet loaded at the workplace (yet)
just at home. So have to wait to try the following, but
would something like this work?

Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim txtboxarea As TextBox() = New TextBox() {txt0,txt1,
txt2,txt3,...txt10}
dim txt as TextBox
For Each txt In txtboxarea
AddHandler txt.LostFocus, AddressOf TextBox_LostFocus
Next
End Sub

Private Sub TextBox_LostFocus(ByVal sender As Object, _
ByVal e As System.EventArgs)
DirectCast(sender, TextBox).Text = _
Trim(DirectCast(sender, TextBox).Text)
If sender.Name = "txt0" Then
If sender.Text < 1990 Or sender.Text > 2010 Then
sender.Text = ""
Beep()
MsgBox "Out of bounds!"
Else
If sender.Text > 1000 Then
sender.Text = ""
Beep()
MsgBox "Out of Bounds!"
End If
End Sub

---what's best way to invoke TextBox_LostFocus? Can I
call the same TextBox_LostFocus for each textbox or do I
have to make one for each textbox? Do I need to cast
sender.Text or do I need to change that to

DirectCast(sender, TextBox).Text < 1990 ...

Thank you always for your help,
Rich
 
C

Cor

Hi Rich,

When you do that directcast around all "sender" than I think something as
that yes.
You also can look at the "leave" event and the oposite from that "enter"

Cor
 
R

Rich

If I understand correctly then:

Private Sub TextBox_LostFocus(ByVal sender As Object, _
ByVal e As System.EventArgs)
DirectCast(sender, TextBox).Text = _
Trim(DirectCast(sender, TextBox).Text)
If (DirectCast(sender, TextBox).Name = "txt0" Then
If DirectCast(sender, TextBox).Text < 1990 Or DirectCast
(sender, TextBox).Text > 2010 Then
DirectCast(sender, TextBox).Text = ""
Beep()
MsgBox "Out of bounds!"
Else
If DirectCast(sender, TextBox).Text > 1000 Then
DirectCast(sender, TextBox).Text = ""
Beep()
MsgBox "Out of Bounds!"
End If
End Sub

And I believe the opposite of "Enter" for VB would
be "Exit". So you suggest I could look at the "Leave"
or "Exit" events in the help files? I will have a look.

Many thanks for your suggestions.
Rich
 
H

Herfried K. Wagner [MVP]

* "Rich said:
I have 10 textboxes on a form. I would like to validate
text-data entry on each textbox. I was looking for a
Form_Current event or Form_Change event but did not
recognize any such events. Or maybe there is a property
for the Textboxes where I can set data entry conditions?
Wondering about CausesValidation textbox property-how to
use.

Setting 'CausesValidation' to 'True' will prevent raising the
'Validating' event for other controls.
I was thinking maybe I could create a custom form event
that would loop through an array of textboxes when the
event is raise and would check the content of each textbox
like as soon as the textbox loses focus. Is there such a
form event? Do I need to create one? How to create this
event and raise the event?

You can store some data in the textboxs' 'Tag' property.
 
R

Rich

Thank you for this explanation. So if I set
CausesValidation to False, how do I raise the validate
Event for other controls?

Thanks,
Rich
 
R

Rich

Just reporting back that I used Cor's TextBox_LostFocus
Event handler and it worked perfectly! Thanks.

Thank you all for your help.

Rich
 

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