MDI and Interface question

  • Thread starter Thread starter SStory
  • Start date Start date
S

SStory

I am doing the following with my MDI app..

Please advise if I am going about this wrong or there is a better way.

I thought, well, the main form needs to be able to ask the child form if it
can save

So I made the following interfaces.

'all child forms implement ICanSave and return true or false;main can thus
always call CanSave to see if it can save
Public Interface ICanSave
ReadOnly Property CanSave() As Boolean
End Interface

'if they can save they should then implement ISave which has the
following... So when user clicks Save icon, this Save method 'is called
Public Interface ISave
Function IsDirty() As Boolean
Function Save() As Boolean
Function SaveAs(ByVal FileName As String) As Boolean
End Interface



on main form
Private Sub frmMain_MdiChildActivate(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.MdiChildActivate
Dim Savable As ICanSave
Dim ISaver As ISave
Try
'see if can save
Savable = Me.ActiveMdiChild
If Savable.CanSave Then
ISaver = Me.ActiveMdiChild
mnuFileSave.Enabled = ISaver.IsDirty
End If

mnuFileSaveAs.Enabled = Savable.CanSave
Catch ex As Exception
'don't know so disable them
mnuFileSave.Enabled = False
mnuFileSaveAs.Enabled = False
End Try
End Sub

Is there a better way?

Am I doing something dumb?

Is there another event to check when child form becomes dirty because I
think ChildActivate requires a click.

So how can I know when to enable the Save icon otherwise?



Hope this all makes sense..

Any help appreciated.



Shane
 
SStory said:
I am doing the following with my MDI app..

Please advise if I am going about this wrong or there is a better
way.

I thought, well, the main form needs to be able to ask the child form
if it can save

So I made the following interfaces.

'all child forms implement ICanSave and return true or false;main can
thus always call CanSave to see if it can save
Public Interface ICanSave
ReadOnly Property CanSave() As Boolean
End Interface

'if they can save they should then implement ISave which has the
following... So when user clicks Save icon, this Save method 'is
called Public Interface ISave
Function IsDirty() As Boolean
Function Save() As Boolean
Function SaveAs(ByVal FileName As String) As Boolean
End Interface


IMO it is sufficient to implement the second interface. If the child
implements the interface, the child is "savable".
on main form
Private Sub frmMain_MdiChildActivate(ByVal sender As Object, ByVal e
As System.EventArgs) Handles MyBase.MdiChildActivate
Dim Savable As ICanSave
Dim ISaver As ISave
Try
'see if can save
Savable = Me.ActiveMdiChild
If Savable.CanSave Then
ISaver = Me.ActiveMdiChild
mnuFileSave.Enabled = ISaver.IsDirty
End If

mnuFileSaveAs.Enabled = Savable.CanSave
Catch ex As Exception
'don't know so disable them
mnuFileSave.Enabled = False
mnuFileSaveAs.Enabled = False
End Try
End Sub

Is there a better way?

Am I doing something dumb?

Is there another event to check when child form becomes dirty because
I think ChildActivate requires a click.

So how can I know when to enable the Save icon otherwise?

I'd probably declare an event in the ISavable interface (Event
IsDirtyChanged). On creation of a child form, the Mdi container can attach a
handler to the event. Either you know what type of child is created, so
you'll be able to type cast to ISavable and attach the handler, or, if each
child is sent through a common procedure, the procedure can do the same but
only after checking if it implements the interface.


The event handler above I'd probably write this way:

Private Sub frmMain_MdiChildActivate(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.MdiChildActivate

Dim EnableMenu As Boolean

EnableMenu = TypeOf Me.ActiveMdiChild Is ISavable _
AndAlso DirectCast(Me.ActiveMdiChild, ISavable).IsDirty

mnuFileSave.Enabled = EnableMenu
mnuFileSaveAs.Enabled = EnableMenu

End Sub
 
Back
Top