Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate event

Status
Not open for further replies.

sillysod

Technical User
Jan 6, 2004
300
GB
Hi there,

is there a way to "call" the validation event of a textbox via code

Basically i have populated a textbox from code, and wish to raise a validation event.

The validation will always be ok since its validating information its taken from the database (which was validated before it went into the database)

However my validation event calls a function which performs another take
it populates a label with the supplier name for example, after validating the supplier code the user entered
 
there is no validation event (which there was in VB6).

The choices you've got now are Validating and validated

to call the validating event a basic example would be

textbox1_validating(nothing,nothing)

if you're calling from an event then you've already got sender and event arguments so try

textbox1_validating(sender,e)

the event arguments probably won't match between different control events so that might need some tinkering

a better solution might be to build a generic handling sub then add the textbox validating handlers to it.This would save having to know the name of the event (ie textbox1_validating) and is a bit more compact

eg
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler TextBox1.Validating, AddressOf SomeValidating
AddHandler TextBox2.Validating, AddressOf SomeValidating
End Sub

Private Sub SomeValidating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
Dim t As TextBox
t = CType(sender, TextBox)
If t.Name = "TextBox1" Then
MsgBox("validatin textbox1")
End If
End Sub

 
hi,

Thanks for the reply.

I cannot get it to work, i think your right in that the event args dont match.

and the second example went right over my head.

Do you have any idea what the event arg should be?

Thanks
 
those event args can be a real pain sometimes.

usually I find if calling an event in code I rarely need them anyway so just set them to nothing.

in the method that you want to kick off the textbox validation try

textbox1_validating(nothing,nothing)

or whatever the textbox is called.

If this doesn't work then it might be because the label event is requiring some sender or event arg. Post some code and I'll try and spot it.

Just as a thought are you sure the code is in the validating event and not the Leave event or something similar?
 
Hi there
Code:
    Private Sub txtSupplier_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtSupplier.Validating
        txtBuyer.Text = txtBuyer.Text.ToUpper

        Dim f As Form = Me
        Dim t As AssistedTextBox = Me.txtSupplier
        Dim l As Label = Me.lblSupplier

        e.Cancel = Not ValidateAgainstDS(txtSupplier.Text, f, t, l, "tblSuppliers", "SupplierCode", "SupplierName", "# Invalid supplier code")

    End Sub

Thats the code im using

Thanks in advance
 
Is Validating roughly equivalent to the VB6 change event (eg it gets called multiple times as you type in the textbox) and does Validated roughly correspond to the old VBA after updated event (eg called once when the control is about to lose focus)? Sorry if these are obvious questions!

Have a great day!

j2consulting@yahoo.com
 
validating fires once, and its not like the afterupdate as you can cancel it using
e.cancel = true

cancelling the event will stop the user from being able to take focus away from control

after the control has been successfully validiad the validated event fires.
 
the only thing I can see there is that assistedtextbox and textbox may not be compatible

try...


Dim t As AssistedTextBox = ctype(Me.txtSupplier,AssistedTextBox)


...this will make it obvious when stepping through the code whether you need to build t (the AssistedTextBox) properties from scratch rather than just setting it to txtSupplier

I'd also recommend adding...


option explicit on
option strict on


...to the top of your document-this will also make problems with converting types obvious


are you getting an error or is nothing happening?

in the case of an error the problem is that you've got no error trapping which at first glance of an error stack trace won't make it obvious where your error is actually originating.

To fix this add a try..catch statment in this sub AND ValidateAgainstDS

The problem may be coming from ValidateAgainstDS.

Hope this helps
 
Hi There,

The assistedtextbox is a custom control which inherits from textbox, so i ont think thats the problem

The validateagainstds does have a try catch statement, and this code fires ok when the validating event is called normally

There is an error message, but cant remember what exactly it says, and off to bed at the moment.

I will report back tomorrow :)

Thanks
Sillysod
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top