Tod said:
Schneider,
as I understood you, you suggest to write all logic on the mainform but
what if the types of documents too big than the mainform should contain
many IF/ELSE operators:
OnActivateMdiWindow
{
if (ActiveMdiWindow is ImageDocument)
{
toolbarZoom.Enabled = true;
toolbarZoom.SelectedIndex = (ImageDocument)ActiveMdiWindow;
}
else if (ActiveMdiWindow is OtherDocument)
{
toolbarSomeItem.SelectedIndex = ....
}
}
OnDeactiveMdiWindow
{
if (ActiveMdiWindow is ImageDocument)
{
toolbarZoom.Enabled = false;
}
}
It will be hard to understand and the main form will contain unnecessary
code. Are there any patterns for such problems?
No, I suggest keep as much of the Document logic out of the MDI form.
I was thinking more like this:
Public Class MyMdiForm
Inherits System.Windows.Forms.Form
Private m_ActiveDocumentForm As MyDocumentForm
Private m_Toolbar As ToolbarForm
Public Property ActiveDocumentForm() As MyDocumentForm
Get
Return m_ActiveDocumentForm
End Get
Set(ByVal value As MyDocumentForm)
m_ActiveDocumentForm = value
End Set
End Property
Public Property ToolBar() As ToolbarForm
Get
Return m_Toolbar
End Get
Set(ByVal value As ToolbarForm)
m_Toolbar = Value
End Set
End Property
Private Sub MyMdiForm_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
'create toolbar
Dim newToolbar As New ToolbarForm
newToolbar.MdiParent = Me
newToolbar.Visible = True
Me.ToolBar = newToolbar
'create a new document 1
Dim newDoc1 As New MyDocumentForm
newDoc1.Text = "Doc1"
newDoc1.MdiParent = Me
newDoc1.Visible = True
'create a new document 1
Dim newDoc2 As New MyDocumentForm
newDoc2.Text = "Doc2"
newDoc2.MdiParent = Me
newDoc2.Visible = True
End Sub
End Class
Public Class MyDocumentForm
Inherits System.Windows.Forms.Form
Private m_Zoom As Integer
Public Property Zoom() As Integer
Get
Return m_Zoom
End Get
Set(ByVal value As Integer)
m_Zoom = value
Me.Label1.Text = m_Zoom.ToString
End Set
End Property
Private Sub MyDocumentForm_Activated(ByVal sender As Object, ByVal
e As System.EventArgs) Handles MyBase.Activated
Dim mdiform As MyMdiForm = DirectCast(Me.MdiParent, MyMdiForm)
'set this document as active
mdiform.ActiveDocumentForm = Me
End Sub
End Class
Public Class ToolbarForm
Inherits System.Windows.Forms.Form
Private Sub btnZoomIn_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnZoomIn.Click
Dim mdiform As MyMdiForm = DirectCast(Me.MdiParent, MyMdiForm)
'increase zoom
mdiform.ActiveDocumentForm.Zoom =
diform.ActiveDocumentForm.Zoom + 10
End Sub
End Class
Anyone have a better approach?
I'm always looking for better ways...
Thanks,
Schneider