Access MDI child properties generically

M

Mark Hoffy

Hello,

I have an MDI app. There are many child forms and they all have several
things in common. They all have a Save button, a public variable
boolNavigate, and they all have a TreeView called treeBrowse.

I have a generic routine to create the TreeView and add it to a form since I
do this on a dozen or so forms. I also would like to add event handlers in
this generic routine. The handler routines perform the same function on
every form, but they need to reference the boolNavigate variable of the
particular calling form. How can I do this without typing the form in a
case with every possible child form?

Here is my code from a librabry module...

Public Sub CreateBrowserTree(ByVal frmParent As Form, ByVal treeIn As
TreeView)
'sets up a generic browser tree view attached to the passed in
parent control
Dim splitBrowse As Splitter

'create a splitter
splitBrowse = New Splitter
With splitBrowse
'...set properties
End With
frmParent.Controls.Add(splitBrowse)

'create a tree
treeIn = New TreeView
With treeIn
'...set properties
End With
frmParent.Controls.Add(treeIn)

'attach event handlers
AddHandler treeIn.BeforeSelect, AddressOf TreeBrowse_BeforeSelect
'.... more of same type handlers
End Sub

Private Sub TreeBrowse_BeforeSelect(ByVal sender As Object, ByVal e As
System.Windows.Forms.TreeViewCancelEventArgs)
'dont allow user to leave this node if bollNavigate is false
If (not sender.boolNavigate) Then
Beep()
e.Cancel = True
End If
End Sub

The part about 'sender.boolNavigate' does not work. How can I access this
property that I know exists for every given form?

Also, I the code for each form's 'Save' button is the same. Is there a way
I can make a generic handler for that and have it apply to all the forms so
I can maintain 1 routine? Currently every form's save routine looks about
like this...

Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdSave.Click
If (SaveFormData(boolAddingNew, me) Then 'SaveFormData is a
generic data saving routine, pass a flag for new and the parent control
(form)
'reload the data
LoadFormData() 'this routine is different on each
form, but the Save handler would call each form's version
FirstNode(treeBrowse)
cmdNew.Enabled = True 'again, each form has a cmdNew
End If
End Sub

Thanks in advance.
 
C

CMM

I'm not really sure if I completely understand the problem... but from what
I could surmise, wouldn't having your child forms implement an Interface
solve your problems?

First off, for the love of god, wrap the boolNavigate var into a Property
called "AllowNavigate" or something. Add a new Interface file:

Interface IMyChildForm
Property AllowNavigate As Boolean
End Interface

Then add "Implements IMyChildForm" to all your forms.

Then you use as so:

If Not DirectCast(Sender, IMyChildForm).AllowNavigate Then
Beep()
e.Cancel = True
End If
 

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