Question about MDI in VB.NET

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

SStory

3rd time trying to post this but never see it in the list:
===================================

In VB.NET, 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
 
I'm sorry does that have anything to do with my question about MDI and
interfaces?
And if I am following a good desigin technique? Don't see the relation
between the two..

Shane
 
* "SStory said:
I'm sorry does that have anything to do with my question about MDI and
interfaces?
And if I am following a good desigin technique? Don't see the relation
between the two.. [...]
Turn on 'Option Strict On', have a look at the documentation on the
'DirectCast' operator.

If you want to follow a good programming technique, you should read my
reply...

:-)
 
SStory said:
3rd time trying to post this but never see it in the list:


It's the 3rd time it arrived. I copied the answer I sent to the other
thread:

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
 
Thanks Armin...

Sorry I am a bit ignorant still but trying to learn and do this right.

I thought I might only need one of the 2 interfaces.
But then how do you find out if a form implemented the interfaces?

Also I guess Herfried was trying to tell me something with DirectCast. I
will have to read up on it to know what you are both talking about.

Thanks for the reply.

If you could tell me how to know if a form implemented an interface I'd
appreciate it.

thanks,

Shane
 
hmm..
could it be that you mean that my MDI form needs to just call whatever
childform using

try
dim I as ISave
ISave=DirectCast(Me.ActiveMdiChild,ISave) or something like that
catch ex as InvalidCastException
end try

something like this, and if the child didn't implement ISave that it would
fail with this exception....

Is this what you wanted me to see?

If so, I see.

Thanks,

Shane

Herfried K. Wagner said:
* "SStory said:
I'm sorry does that have anything to do with my question about MDI and
interfaces?
And if I am following a good desigin technique? Don't see the relation
between the two.. [...]
Turn on 'Option Strict On', have a look at the documentation on the
'DirectCast' operator.

If you want to follow a good programming technique, you should read my
reply...

:-)
 
* "SStory said:
Sorry I am a bit ignorant still but trying to learn and do this right.

I thought I might only need one of the 2 interfaces.
But then how do you find out if a form implemented the interfaces?

\\\
If TypeOf f Is ICanSave Then
MsgBox("Implements ICanSave")
End If
///
 
* "SStory said:
could it be that you mean that my MDI form needs to just call whatever
childform using

try
dim I as ISave
ISave=DirectCast(Me.ActiveMdiChild,ISave) or something like that
catch ex as InvalidCastException
end try

You don't need a 'Try...Catch' block here if you are sure all children
implement the interface. Nevertheless, it's good practice to use
'DirectCast' in this case.
something like this, and if the child didn't implement ISave that it would
fail with this exception....

It won't fail if all children implement the interface. But it's
better...
 
SStory said:
Thanks Armin...

Sorry I am a bit ignorant still but trying to learn and do this
right.

I thought I might only need one of the 2 interfaces.
But then how do you find out if a form implemented the interfaces?


If TypeOf Me.ActiveMdiChild Is ISavable

The line above checks if the active mdi child implements the interface.

Also I guess Herfried was trying to tell me something with
DirectCast. I will have to read up on it to know what you are both
talking about.

DirectCast(Me.ActiveMdiChild, ISavable)

The type of the property ActiveMdiChild is System.Windows.Forms.Form. The type
does not have a IsDirty property and also not the methods Save and SaveAs.
After checking that the active mdi child implements the interface, you can
typecast the Form to ISavable. After that you have a reference that's
type is ISavable. As you now have a reference to an object that implements
the interface, you can access the properties IsDirty and the methods Save
and SaveAs.
 
Well, I don't know that they all will have... That is one of the issues.

needed to check to be sure.

Need the Save icon and menu option to enable disable based on this....

So this is the best way to check if it implements the interface? Direct
cast with a Try Catch?

Right now the MDI form check MDIChildActivate event(or something like that)

This only works if the person clicks on a child for before trying to save.

What would be a better way to handle this? Somehow when the child form is
dirty the MDI form needs to know to enable save without any clicking...?

thanks,

Shane




If they implement ISave then the MDI
 
ok.

thanks for the typeof idea...

But why is directcast better than ctype?

Also as I asked Herfried, when the child becomes dirty the MDI parent needs
to respond by enabling the Save/Save As menu options and icons. Right now I
am checking this in MDIActivateChild(or whatever that was called) and this
works if the person clicks on one of the MDI children but needs to work
reguardless.. How should I best implement this?

Thanks,

Shane
 
SStory said:
ok.

thanks for the typeof idea...

But why is directcast better than ctype?

I didn't mention that it is better. ;-) I don't see a difference, also
concerning the performance, in both statements. I distinguish between type
casting and type conversion. Type casting only changes the type of the
reference. Type conversion creates a new object. CType can perform type
conversion and type casting whereas Directcast can only cast the type,
that's why I prefer DirectCast for type casting.
Also as I asked Herfried, when the child becomes dirty the MDI parent
needs to respond by enabling the Save/Save As menu options and icons.
Right now I am checking this in MDIActivateChild(or whatever that
was called) and this
works if the person clicks on one of the MDI children but needs to
work reguardless.. How should I best implement this?

I've already explained it in my previous post.

...... Here it is:

<quote>
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.
</quote>
 
Back
Top