Results 1 to 4 of 4

Thread: how to detect an already open form

  1. #1

    Thread Starter
    Registered User jkw119's Avatar
    Join Date
    Oct 2001
    Location
    Pittsburgh
    Posts
    256

    how to detect an already open form

    I have probably a relatively simple question... (at least one would think)

    IN my menu, there is an item that opens a form. I would like to put some code so that, if the user clicks it twice, instead of opening two forms, the already open form is just selected and brought to the front of everything. Does anyone have any suggestions? This is the menu code

    VB Code:
    1. Dim SegmentInfoForm As New frmSegmentInfo()
    2.         SegmentInfoForm.Show()
    3.         System.Windows.Forms.Application.DoEvents()

    Thanks,

    jkw119

  2. #2
    Addicted Member Fat_N_Furry's Avatar
    Join Date
    Oct 2002
    Posts
    171
    VB Code:
    1. 'In the general declarations
    2. Dim booFormLoaded as Boolean = False
    3.  
    4. 'In your sub
    5. If booFormLoaded = False then
    6.         Dim SegmentInfoForm As New frmSegmentInfo
    7.         SegmentInfoForm.Show()
    8.         System.Windows.Forms.Application.DoEvents
    9.         booFormLoaded = True
    10. Else
    11.         SegmentInfoForm.Focus()
    12. End If


    gL,
    Furry
    Eat long and prosper!
    If someone helps you, find someone you can help.
    If you still have time, click on the darn banner up there and help the forum!

  3. #3
    Lively Member
    Join Date
    Jan 2001
    Posts
    115
    Try this

    ' defined in a module
    public SegmentInfoForm As frmSegmentInfo()



    In the click menu event :

    if SegmentInfoForm is nothing or segmentinform.isdispose() then
    SegmentInfoForm = new frmSegmentInfo()
    endif
    SegmentInfoForm.show()
    SegmentInfoForm.topmost =true

  4. #4
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432
    [/code]
    Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click, CmnuDisplayChild.Click
    'Instantiate a child

    LoadForm("frmChild")
    End Sub

    Private Sub LoadForm(ByVal strFormName As String)
    'Load form or activate if it already exists
    Dim frmForms() As Form = Me.MdiChildren
    Dim intIndex As Integer
    Dim blnFound As Boolean

    'Does it exist?
    For intIndex = 0 To frmForms.GetLength(0) - 1
    If frmForms(intIndex).Name = strFormName Then
    blnFound = True
    frmForms(intIndex).Activate()
    End If
    Next

    If Not blnFound Then
    Select Case strFormName
    Case "frmChild"
    Dim frmChild As New ChildForm()
    With frmChild
    .MdiParent = Me
    .Name = "frmChild"
    .Show()
    End With
    End Select
    Me.LayoutMdi(MdiLayout.Cascade)
    End If
    End Sub
    [/code]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width