How can i design this approach ?

A

Agnes

I created my own usercontrol of textbox and my base from . Every windows
will inhertied this mybaseform and every textbox will use that usercontrol.
Now, mybaseform got a function "public overridables function
fieldvalidation(byval _fieldname as string) as boolean"
in my usercontrol's validated event, I want to pass the textbox's name to
that function.
Can I do that ??
my aim ; in each windows form e.g frmInvoice, with a textbox,
txtInvNo,txtCompanyCode.
I want to use a "simple" and global way to pass the 'txtInvNo' to the
fieldvalidation()

public ..... function fieldvalidation(byval _field as string) as boolean
if _field = "txtInvNo" or _field = "CHECKALL"then
if me.txtInvNo.text.trim.length =0 then
messagebox.show("error")
blnFlag = false
end if
if _field = "txtCompanyCode" or _field = "CHECKALL"then
if me.txtInvNo.CompanyCode.trim.length =0 then
messagebox.show("error ")
blnFlag = false
end if
.........
end func

I want to create a BIG function to do the field valiation , instead of do it
in each textbox's validated event.
Anyone understand my question ?
Thanks in advance
 
N

NM

Hi,

In your own usercontrol of textbox, create this new event :

Public Event fieldValidation(ByVal _field as string)
Then define a method that'll raise that event :

Private Sub mytextbox_Validated(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Validated

Raise Event fieldValidation(Me.name)

End Sub

Then in your form define the fieldValidation procedure that handles
fieldValidation event of all textbox :

public sub fieldvalidation(byval _field as string) Handles
txtInvNo.fieldValidation, txtCompanyCode.fieldValidation
if _field = "txtInvNo" or _field = "CHECKALL"then
if me.txtInvNo.text.trim.length =0 then
messagebox.show("error")
blnFlag = false
end if
if _field = "txtCompanyCode" or _field = "CHECKALL"then
if me.txtInvNo.CompanyCode.trim.length =0 then
messagebox.show("error ")
blnFlag = false
end if
........
end sub


I notice that you are doing the same test for all your textbox; so in my
opinion, inplement your test in your usercontrol of textbox :

Private Sub mytextbox_Validated(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Validated
if me.text.trim.length = 0 then
messagebox.show("error")
blnFlag = false
end if
End Sub

The advantages are :
- if you add or delete a textbox from your form, no need to add or remove
the if condition related;
- a small and easy "fieldvalidation" function to manipulate


Hope this help;
Regards
 
A

Agnes

Dear NM,
Your codes is so great.
BUt I wonder public sub fieldvalidation(byval _field as string) Handles
txtInvNo.fieldValidation, txtCompanyCode.fieldValidation......etc
(30fields's validatin)
Will it make the programm too slow ??
Thanks
 

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