Sample code - MDI 'forms' collection


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Sample code - MDI 'forms' collection

  1. #1
    Larry Leach Guest

    Sample code - MDI 'forms' collection

    I thought some other folks might find this useful (or provide some
    feedback)...

    In VB 6, I used the forms collection to know if an MDI form was already
    loaded or not. That way, if a user clicked the menu item that displayed an
    MDI child, and the child was already being displayed (perhaps minimized or
    behind another form), I could simply 'reactivate' it. However, the forms
    collection has gone away in VB.Net, so here is the solution I came up with.

    First, I created the following class within the MDI form class:

    Private Class Forms
    Private Shared FormsArray As New ArrayList()
    Public Shared Function IsFormLoaded(ByVal frm As Form) As Boolean
    Dim f As Form
    For Each f In FormsArray
    If f Is frm Then Return True
    Next
    Return False
    End Function
    Public Shared Sub Add(ByVal frm As Form)
    FormsArray.Add(frm)
    End Sub
    Public Shared Sub Remove(ByVal frm As Form)
    FormsArray.Remove(frm)
    End Sub
    Public Shared Sub Activate(ByVal frm As Form)
    frm.WindowState = FormWindowState.Normal
    frm.BringToFront()
    End Sub
    End Class

    Next, I renamed the 'MdiClient1' variable (created by VB in the MDI form) to
    'ViperMDI' (a name which makes sense in my particular application). I then
    placed the following code in the 'ControlAdded' and 'ControlRemoved' events:

    Private Sub ViperMDI_ControlAdded(ByVal sender As Object, ByVal e As
    System.Windows.Forms.ControlEventArgs) Handles ViperMDI.ControlAdded
    If TypeOf (e.Control) Is Form Then
    Forms.Add(CType(e.Control, Form))
    End If
    End Sub

    Private Sub ViperMDI_ControlRemoved(ByVal sender As Object, ByVal e As
    System.Windows.Forms.ControlEventArgs) Handles ViperMDI.ControlRemoved
    If TypeOf (e.Control) Is Form Then
    Forms.Remove(CType(e.Control, Form))
    End If
    End Sub

    Now, I can use the following code to display a form (and always have just
    one instance of the form - my intention):

    Dim frmEmployerDetail As EmployerDetail 'at the module level

    Private Sub mnuEmployerDetail_Click(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles mnuEmployerDetail.Click
    If Forms.IsFormLoaded(frmEmployerDetail) Then
    Forms.Activate(frmEmployerDetail)
    Else
    frmEmployerDetail = New EmployerDetail()
    frmEmployerDetail.MdiParent = Me
    frmEmployerDetail.Show()
    End If
    End Sub

    If you have feedback on something that could have been done better (like
    'this is already built into .Net' and I just couldn't find it), or see some
    hidden problem in what I've done, please let me know.

    Thanks,

    -ldl-




  2. #2
    Bill McCarthy Guest

    Re: Sample code - MDI 'forms' collection

    Hi Larry,

    don't see why you needed the Forms collection you wrote, as you could have
    just used the MDIChildren() property of the main form. So that would make
    the code something like:

    ' inside the form's code
    ------------------------------
    ' this could be private. Cannot be Shared
    Public Function IsFormLoaded(ByVal frm As Form) As Boolean
    Dim f As Form
    For Each f In Me.MDIChildren
    If f Is frm Then Return True
    Next
    Return False
    End Function

    ' no need to bother with add or remove

    'renamed to prevent potential problems/confussion with Form.Activate

    ' this could be private/public, shared or not
    Public Shared Sub ActivateForm(ByVal frm As Form)
    frm.WindowState = FormWindowState.Normal
    frm.BringToFront()
    End Sub


    Private frmEmployerDetail As EmployerDetail 'at the module level

    Private Sub mnuEmployerDetail_Click(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles mnuEmployerDetail.Click
    If IsFormLoaded(frmEmployerDetail) Then
    ActivateForm(frmEmployerDetail)
    Else
    frmEmployerDetail = New EmployerDetail()
    frmEmployerDetail.MdiParent = Me
    frmEmployerDetail.Show()
    End If
    End Sub



    "Larry Leach" <larryl@asibpi.com> wrote in message
    news:3b621f7f$1@news.devx.com...
    > I thought some other folks might find this useful (or provide some
    > feedback)...
    >
    > In VB 6, I used the forms collection to know if an MDI form was already
    > loaded or not. That way, if a user clicked the menu item that displayed

    an
    > MDI child, and the child was already being displayed (perhaps minimized or
    > behind another form), I could simply 'reactivate' it. However, the forms
    > collection has gone away in VB.Net, so here is the solution I came up

    with.
    >
    > First, I created the following class within the MDI form class:
    >
    > Private Class Forms
    > Private Shared FormsArray As New ArrayList()
    > Public Shared Function IsFormLoaded(ByVal frm As Form) As Boolean
    > Dim f As Form
    > For Each f In FormsArray
    > If f Is frm Then Return True
    > Next
    > Return False
    > End Function
    > Public Shared Sub Add(ByVal frm As Form)
    > FormsArray.Add(frm)
    > End Sub
    > Public Shared Sub Remove(ByVal frm As Form)
    > FormsArray.Remove(frm)
    > End Sub
    > Public Shared Sub Activate(ByVal frm As Form)
    > frm.WindowState = FormWindowState.Normal
    > frm.BringToFront()
    > End Sub
    > End Class
    >
    > Next, I renamed the 'MdiClient1' variable (created by VB in the MDI form)

    to
    > 'ViperMDI' (a name which makes sense in my particular application). I

    then
    > placed the following code in the 'ControlAdded' and 'ControlRemoved'

    events:
    >
    > Private Sub ViperMDI_ControlAdded(ByVal sender As Object, ByVal e As
    > System.Windows.Forms.ControlEventArgs) Handles ViperMDI.ControlAdded
    > If TypeOf (e.Control) Is Form Then
    > Forms.Add(CType(e.Control, Form))
    > End If
    > End Sub
    >
    > Private Sub ViperMDI_ControlRemoved(ByVal sender As Object, ByVal e As
    > System.Windows.Forms.ControlEventArgs) Handles ViperMDI.ControlRemoved
    > If TypeOf (e.Control) Is Form Then
    > Forms.Remove(CType(e.Control, Form))
    > End If
    > End Sub
    >
    > Now, I can use the following code to display a form (and always have just
    > one instance of the form - my intention):
    >
    > Dim frmEmployerDetail As EmployerDetail 'at the module level
    >
    > Private Sub mnuEmployerDetail_Click(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles mnuEmployerDetail.Click
    > If Forms.IsFormLoaded(frmEmployerDetail) Then
    > Forms.Activate(frmEmployerDetail)
    > Else
    > frmEmployerDetail = New EmployerDetail()
    > frmEmployerDetail.MdiParent = Me
    > frmEmployerDetail.Show()
    > End If
    > End Sub
    >
    > If you have feedback on something that could have been done better (like
    > 'this is already built into .Net' and I just couldn't find it), or see

    some
    > hidden problem in what I've done, please let me know.
    >
    > Thanks,
    >
    > -ldl-
    >
    >
    >




  3. #3
    Bill McCarthy Guest

    Re: Sample code - MDI 'forms' collection

    Hi again,

    actually, can skip the iteration, and just use IndexOf:

    eg:

    Public Function IsFormLoaded(ByVal frm As Form) As Boolean
    Return Array.IndexOf(me.MDIChildren, frm) >= 0
    End Function



    "Bill McCarthy" <bill_mcc@iprimus.com.au> wrote in message
    news:3b6255e5@news.devx.com...
    > Hi Larry,
    >
    > don't see why you needed the Forms collection you wrote, as you could have
    > just used the MDIChildren() property of the main form. So that would make
    > the code something like:
    >
    > ' inside the form's code
    > ------------------------------
    > ' this could be private. Cannot be Shared
    > Public Function IsFormLoaded(ByVal frm As Form) As Boolean
    > Dim f As Form
    > For Each f In Me.MDIChildren
    > If f Is frm Then Return True
    > Next
    > Return False
    > End Function
    >
    > ' no need to bother with add or remove
    >





  4. #4
    Larry Leach Guest

    Re: Sample code - MDI 'forms' collection

    I can live with a little egg on my face.

    Great stuff. Thanks, Bill.

    -ldl-


    "Bill McCarthy" <bill_mcc@iprimus.com.au> wrote in message
    news:3b625738@news.devx.com...
    > Hi again,
    >
    > actually, can skip the iteration, and just use IndexOf:
    >
    > eg:
    >
    > Public Function IsFormLoaded(ByVal frm As Form) As Boolean
    > Return Array.IndexOf(me.MDIChildren, frm) >= 0
    > End Function
    >
    >
    >
    > "Bill McCarthy" <bill_mcc@iprimus.com.au> wrote in message
    > news:3b6255e5@news.devx.com...
    > > Hi Larry,
    > >
    > > don't see why you needed the Forms collection you wrote, as you could

    have
    > > just used the MDIChildren() property of the main form. So that would

    make
    > > the code something like:
    > >
    > > ' inside the form's code
    > > ------------------------------
    > > ' this could be private. Cannot be Shared
    > > Public Function IsFormLoaded(ByVal frm As Form) As Boolean
    > > Dim f As Form
    > > For Each f In Me.MDIChildren
    > > If f Is frm Then Return True
    > > Next
    > > Return False
    > > End Function
    > >
    > > ' no need to bother with add or remove
    > >

    >
    >
    >




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center