Generelized Code

  • Thread starter Thread starter Anil
  • Start date Start date
A

Anil

How same functionality can be given to mulitiple forms in a VB.Net
project?
e.g. Suppose on each form, toolbar is present. If we click on New
button then Add, Edit button gets disabled and Save, Cancel button is
enabled. If we click on Save button, records gets saved.
In short how the generelized code can be written?
 
Anil,

If it's something that appears on multiple forms, I'd either create a User
Control composed of the constituent controls and plonk it on each form
required, or I'd create the toolbar on a 'master' form and use visual
inheritance to have it appear on your child forms.

HTH,

Adam
 
Thank you sir, What idea U have gievn will be definitely helpful for
me.

But my second problem is how (outline) modules can be written to
provide functionality e.g. saving,updating of records throu various
forms.

I tried to implement shared methods in modules to access those without
instance creation. Then pass argument to recognize which form is
calling that shared method. But I feel its not the right way.

So please give me proper direction.
once aggain Thanks a lot sir.
 
Hi Anil,

Let me see if I understand you correctly - you're asking how top create a
method that can be used across multiple forms?

If the forms are all similar, I'd use visual inheritence. For this example,
I've created three forms - one parent form (ParentForm.vb) and two inheriting
child forms (ChildForm1.vb and ChildForm2.vb).

The parent form has the data save method (SaveData). This can be called from
any of the three forms, but for ease of use, I've created a button on the
parent form that calls the code. This would then be inherited by the children
forms.

Here's the code:

ParentForm.vb
-----------------
Public Class ParentForm
Inherits System.Windows.Forms.Form

' Removed Windows generated code for brevity

Protected Sub SaveData()
'Save your data here
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
SaveData()
End Sub
End Class

ChildForm1.vb:
-----------------
Public Class ChildForm1
Inherits WindowsApplication2.ParentForm

' Removed Windows generated code for brevity

End Class

ChildForm2.vb:
-----------------
Public Class ChildForm2
Inherits WindowsApplication2.ParentForm

' Removed Windows generated code for brevity


End Class

As you can see, there's no code in either of the children forms to do the
save data - that's all inherited from the parent (base) class. If you need
need to call the parent's SaveData method from the children, it's still
possible by using MyBase.SaveData().

Hope this example helps you along your way.

Adam
 
Back
Top