locked
Bring a Form to the Front as Active Form

    Question

  • I am trying to have any only one form type eg Countries, Projects, or Vendors etc. open at the same time.  So, if a user has the Country form open and goes to open it again from the menu I would like the applaication to bring the form to the Front of the MDI container.  I have tried a number of things like BringToFront, ActivateMdiChild, etc.

     

    Any idea how I can do that? 

     

    Here is some code have tried at different times.

     

        Private Sub CountryListToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CountryListToolStripMenuItem.Click
    
            For Each f As Form In Me.MdiChildren
    
                If f.Name = "frmCountryList" Then
    
                    'ActivateMdiChild(frmCountryList)
    
                    'frmCountryList.WindowState = FormWindowState.Maximized
    
                    MessageBox.Show("Country List already exists!")
    
                    Exit Sub
    
                End If
    
            Next
    
    
    
            Dim CountryListChildForm As New frmCountryList
    
            ' Make it a child of this MDI form before showing it.
    
            CountryListChildForm.MdiParent = Me
    
            CountryListChildForm.Show()
    
        End Sub
    
    

     Thanks for your Help!

    Rich 

    • Edited by RichLemu Tuesday, July 14, 2009 2:45 PM
    Tuesday, July 14, 2009 2:31 PM

Answers

  • That did it!  Thanks for the help

     

    Rich

    • Marked as answer by RichLemu Tuesday, July 14, 2009 4:24 PM
    Tuesday, July 14, 2009 4:24 PM

All replies

  • Private Sub tvwConfig_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvwConfig.AfterSelect
            Select Case tvwConfig.SelectedNode.Name
                Case "nodeBasis"
                    showForm(frmConfigGeneral)
                Case "nodeDatabase"
                    showForm(frmConfigDatabase)
                Case "nodeBackup"
                    showForm(frmConfigBackup)
            End Select
        End Sub
    
        Private Sub showForm(Of formType As {Form, New})(ByRef formName As formType)
            If formIsOpen(formName) = False Then
                Dim frm As New formType
    
                frm.MdiParent = Me
                frm.FormBorderStyle = Windows.Forms.FormBorderStyle.None
                frm.WindowState = FormWindowState.Normal
                frm.ControlBox = False
                frm.MinimizeBox = False
                frm.MaximizeBox = False
    
                frm.Show()
            Else
                For Each frm In Me.MdiChildren
                    If UCase(formName.Name) = UCase(frm.Name) Then
                        frm.BringToFront()
                        Exit For
                    End If
                Next
            End If
        End Sub
    
        Private Function formIsOpen(ByVal formName As Form) As Boolean
            Dim bExists As Boolean
            For Each frm In Me.MdiChildren
                If UCase(formName.Name) = UCase(frm.Name) Then
                    bExists = True
                    Exit For
                End If
            Next
            FormIsOpen = bExists
        End Function
    I've got this working.
    Tuesday, July 14, 2009 3:21 PM
  • I just added f.BringtoFront before the messagebox and this appears to be working for me. This is the code I used (note: my form was form2)

            For Each f As Form In Me.MdiChildren
    
                If f.Name = "Form2" Then
                    f.BringToFront()
                    MessageBox.Show("Country List already exists!")
    
                    Exit Sub
    
                End If
    
            Next
    
            Dim CountryListChildForm As New Form2
            ' Make it a child of this MDI form before showing it.
            CountryListChildForm.MdiParent = Me
            CountryListChildForm.Show()
    Hope this helps.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Proposed as answer by DeborahKMVP Tuesday, July 14, 2009 4:30 PM
    Tuesday, July 14, 2009 4:03 PM
  • That did it!  Thanks for the help

     

    Rich

    • Marked as answer by RichLemu Tuesday, July 14, 2009 4:24 PM
    Tuesday, July 14, 2009 4:24 PM