How to prevent multiple child forms opening

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tony K

    #1

    How to prevent multiple child forms opening

    I have a MDI application. On the menu toolstrip child forms are selected
    from one of the menus. I don't want to play the disable/enable menu item
    game. I have selected that open forms are added to the "Window" menu item.
    What I'm looking for is when the user clicks on the menu item to open the
    form, how can I check to see if it's open already and if so then set the
    focus (if possible).

    What happens now is if the menu item is clicked on 2, 3 or even 4 times, it
    opens new instances of the form each time.

    What can I change in this code that opens my child forms to prevent it from
    opening new instances?

    Dim frmCustomers As New Customers()
    frmCustomers.Md iParent = Me
    frmCustomers.Wi ndowState = FormWindowState .Maximized
    frmCustomers.Sh ow()


    Thanks,
    Tony

  • Cor Ligthert [MVP]

    #2
    Re: How to prevent multiple child forms opening

    Tony,

    I think that you are searching for the Isdisposed method from a control (and
    therefore a form).
    http://msdn.microsoft.com/library/de...posedtopic.asp

    Cor


    "Tony K" <king-tony2@NOSPAMcom cast.netschreef in bericht
    news:4D5FB25A-BB34-49F8-B13F-6F6816B1338F@mi crosoft.com...
    >I have a MDI application. On the menu toolstrip child forms are selected
    >from one of the menus. I don't want to play the disable/enable menu item
    >game. I have selected that open forms are added to the "Window" menu item.
    >What I'm looking for is when the user clicks on the menu item to open the
    >form, how can I check to see if it's open already and if so then set the
    >focus (if possible).
    >
    What happens now is if the menu item is clicked on 2, 3 or even 4 times,
    it opens new instances of the form each time.
    >
    What can I change in this code that opens my child forms to prevent it
    from opening new instances?
    >
    Dim frmCustomers As New Customers()
    frmCustomers.Md iParent = Me
    frmCustomers.Wi ndowState = FormWindowState .Maximized
    frmCustomers.Sh ow()
    >
    >
    Thanks,
    Tony

    Comment

    • Heikki Leivo

      #3
      Re: How to prevent multiple child forms opening

      What can I change in this code that opens my child forms to prevent it
      from opening new instances?
      >
      Dim frmCustomers As New Customers()
      frmCustomers.Md iParent = Me
      frmCustomers.Wi ndowState = FormWindowState .Maximized
      frmCustomers.Sh ow()
      You could, for example, use a variable to hold the form instance. For
      example,

      Private frmCustomers As Customers

      Private Sub mnuShowCustomer s_Click()
      If frmCustomers Is Nothing OrElse frmCustomers.Is Disposed Then
      frmCustomers = New Customers()
      frmCustomers.Md iParent = Me
      End If
      frmCustomers.Wi ndowState = FormWindowState .Maximized
      frmCustomers.Sh ow()
      End Sub




      Comment

      • Tony K

        #4
        Re: How to prevent multiple child forms opening

        Thank you Cor. You've helped me out several times and I really appreciate
        it. Thanks for the useful links.

        Tony


        "Cor Ligthert [MVP]" <notmyfirstname @planet.nlwrote in message
        news:%23IvNZh6Y HHA.4560@TK2MSF TNGP03.phx.gbl. ..
        Tony,
        >
        I think that you are searching for the Isdisposed method from a control
        (and therefore a form).
        http://msdn.microsoft.com/library/de...posedtopic.asp
        >
        Cor
        >
        >
        "Tony K" <king-tony2@NOSPAMcom cast.netschreef in bericht
        news:4D5FB25A-BB34-49F8-B13F-6F6816B1338F@mi crosoft.com...
        >>I have a MDI application. On the menu toolstrip child forms are selected
        >>from one of the menus. I don't want to play the disable/enable menu item
        >>game. I have selected that open forms are added to the "Window" menu
        >>item. What I'm looking for is when the user clicks on the menu item to
        >>open the form, how can I check to see if it's open already and if so then
        >>set the focus (if possible).
        >>
        >What happens now is if the menu item is clicked on 2, 3 or even 4 times,
        >it opens new instances of the form each time.
        >>
        >What can I change in this code that opens my child forms to prevent it
        >from opening new instances?
        >>
        > Dim frmCustomers As New Customers()
        > frmCustomers.Md iParent = Me
        > frmCustomers.Wi ndowState = FormWindowState .Maximized
        > frmCustomers.Sh ow()
        >>
        >>
        >Thanks,
        >Tony
        >
        >

        Comment

        • Tony K

          #5
          Re: How to prevent multiple child forms opening

          Thank you, I will try it today.

          Tony


          "Heikki Leivo" <heikkidotleivo @cadworksdotfi. removethiswrote in message
          news:pTUIh.1432 9$w55.8805@read er1.news.saunal ahti.fi...
          >What can I change in this code that opens my child forms to prevent it
          >from opening new instances?
          >>
          > Dim frmCustomers As New Customers()
          > frmCustomers.Md iParent = Me
          > frmCustomers.Wi ndowState = FormWindowState .Maximized
          > frmCustomers.Sh ow()
          >
          You could, for example, use a variable to hold the form instance. For
          example,
          >
          Private frmCustomers As Customers
          >
          Private Sub mnuShowCustomer s_Click()
          If frmCustomers Is Nothing OrElse frmCustomers.Is Disposed Then
          frmCustomers = New Customers()
          frmCustomers.Md iParent = Me
          End If
          frmCustomers.Wi ndowState = FormWindowState .Maximized
          frmCustomers.Sh ow()
          End Sub
          >
          >
          >
          >

          Comment

          • Stephen Flynn

            #6
            Re: How to prevent multiple child forms opening

            I use the following code. It will check each open forms type, if it finds
            an open form of that type, then it will bring it to the front. If not
            found, then it will open it.

            For Each OpenForm As Form In Me.MdiChildren
            If TypeOf OpenForm Is [FormName] Then
            OpenForm.Activa te()
            Exit Sub
            End If
            Next
            Dim NewForm As New [FormName]
            NewForm.Visible = True

            "Tony K" <king-tony2@NOSPAMcom cast.netwrote in message
            news:4D5FB25A-BB34-49F8-B13F-6F6816B1338F@mi crosoft.com...
            >I have a MDI application. On the menu toolstrip child forms are selected
            >from one of the menus. I don't want to play the disable/enable menu item
            >game. I have selected that open forms are added to the "Window" menu item.
            >What I'm looking for is when the user clicks on the menu item to open the
            >form, how can I check to see if it's open already and if so then set the
            >focus (if possible).
            >
            What happens now is if the menu item is clicked on 2, 3 or even 4 times,
            it opens new instances of the form each time.
            >
            What can I change in this code that opens my child forms to prevent it
            from opening new instances?
            >
            Dim frmCustomers As New Customers()
            frmCustomers.Md iParent = Me
            frmCustomers.Wi ndowState = FormWindowState .Maximized
            frmCustomers.Sh ow()
            >
            >
            Thanks,
            Tony

            Comment

            • Branco Medeiros

              #7
              Re: How to prevent multiple child forms opening

              Tony K wrote:
              I have a MDI application. On the menu toolstrip child forms are selected
              from one of the menus. I don't want to play the disable/enable menu item
              game. I have selected that open forms are added to the "Window" menu item.
              What I'm looking for is when the user clicks on the menu item to open the
              form, how can I check to see if it's open already and if so then set the
              focus (if possible).
              <snip>
              What can I change in this code that opens my child forms to prevent it from
              opening new instances?
              >
              Dim frmCustomers As New Customers()
              frmCustomers.Md iParent = Me
              frmCustomers.Wi ndowState = FormWindowState .Maximized
              frmCustomers.Sh ow()
              If you're using VB 2005 you can use the My.Forms collection:

              <aircode>
              Dim frmCustomers As Customers = My.Forms.Custom ers
              frmCustomers.Md iParent = Me
              frmCustomers.Wi ndowState = FormWindowState .Maximized
              frmCustomers.Sh ow()
              frmCustomers.Ac tivate
              </aircode>

              This will show the Customers form if it's not shown already (or was
              closed) and give it focus if it's already shown.

              HTH.

              Regards,

              Branco.

              Comment

              • lord.zoltar@gmail.com

                #8
                Re: How to prevent multiple child forms opening

                Y'know what else works? Open the form by the name of it's CLASS.

                Example: If the form's class's name is "MyForm1" and you do
                "MyForm1.Sh ow", no matter how many times you click the button that
                executes "MyForm1.Sh ow", the form will only open once. I don't like
                this one too much myself, but it works in a pinch, and is pretty easy
                to implement.

                Comment

                • Tony K

                  #9
                  Re: How to prevent multiple child forms opening

                  Wow! This worked perfectly. Thanks Branco.

                  Tony


                  "Branco Medeiros" <branco.medeiro s@gmail.comwrot e in message
                  news:1173706663 .870367.224360@ h3g2000cwc.goog legroups.com...
                  Tony K wrote:
                  >I have a MDI application. On the menu toolstrip child forms are selected
                  >from one of the menus. I don't want to play the disable/enable menu item
                  >game. I have selected that open forms are added to the "Window" menu
                  >item.
                  >What I'm looking for is when the user clicks on the menu item to open the
                  >form, how can I check to see if it's open already and if so then set the
                  >focus (if possible).
                  <snip>
                  >What can I change in this code that opens my child forms to prevent it
                  >from
                  >opening new instances?
                  >>
                  > Dim frmCustomers As New Customers()
                  > frmCustomers.Md iParent = Me
                  > frmCustomers.Wi ndowState = FormWindowState .Maximized
                  > frmCustomers.Sh ow()
                  >
                  If you're using VB 2005 you can use the My.Forms collection:
                  >
                  <aircode>
                  Dim frmCustomers As Customers = My.Forms.Custom ers
                  > frmCustomers.Md iParent = Me
                  > frmCustomers.Wi ndowState = FormWindowState .Maximized
                  > frmCustomers.Sh ow()
                  frmCustomers.Ac tivate
                  </aircode>
                  >
                  This will show the Customers form if it's not shown already (or was
                  closed) and give it focus if it's already shown.
                  >
                  HTH.
                  >
                  Regards,
                  >
                  Branco.
                  >

                  Comment

                  • Cyril Gupta

                    #10
                    Re: How to prevent multiple child forms opening

                    Hello Tony,

                    Here's an alternative method

                    If frmCustomers Is Nothing OrElse frmCustomers.Is Disposed Then
                    frmCustomers = New Customers()
                    End If
                    frmCustomers.Md iParent = Me
                    frmCustomers.Wi ndowState = FormWindowState .Maximized
                    frmCustomers.Sh ow()


                    -- You can do anything with a little bit of 'magination.

                    -- I've been programming so long, my brain is now software.
                    Wow! This worked perfectly. Thanks Branco.
                    >
                    Tony
                    >
                    "Branco Medeiros" <branco.medeiro s@gmail.comwrot e in message
                    news:1173706663 .870367.224360@ h3g2000cwc.goog legroups.com...
                    >
                    >Tony K wrote:
                    >>
                    >>I have a MDI application. On the menu toolstrip child forms are
                    >>selected
                    >>from one of the menus. I don't want to play the disable/enable menu
                    >>item
                    >>game. I have selected that open forms are added to the "Window"
                    >>menu
                    >>item.
                    >>What I'm looking for is when the user clicks on the menu item to
                    >>open the
                    >>form, how can I check to see if it's open already and if so then set
                    >>the
                    >>focus (if possible).
                    ><snip>
                    >>
                    >>What can I change in this code that opens my child forms to prevent
                    >>it
                    >>from
                    >>opening new instances?
                    >>Dim frmCustomers As New Customers()
                    >>frmCustomers. MdiParent = Me
                    >>frmCustomers. WindowState = FormWindowState .Maximized
                    >>frmCustomers. Show()
                    >If you're using VB 2005 you can use the My.Forms collection:
                    >>
                    ><aircode>
                    >Dim frmCustomers As Customers = My.Forms.Custom ers
                    >>frmCustomers. MdiParent = Me
                    >>frmCustomers. WindowState = FormWindowState .Maximized
                    >>frmCustomers. Show()
                    >frmCustomers.A ctivate
                    ></aircode>
                    >This will show the Customers form if it's not shown already (or was
                    >closed) and give it focus if it's already shown.
                    >>
                    >HTH.
                    >>
                    >Regards,
                    >>
                    >Branco.
                    >>

                    Comment

                    • Cor Ligthert [MVP]

                      #11
                      Re: How to prevent multiple child forms opening

                      Branco,

                      In all versions is the MDIChildren collection

                      http://msdn2.microsoft.com/en-us/lib...ichildren.aspx

                      Because of your message I got the impression that you did not know that.

                      Cor


                      "Branco Medeiros" <branco.medeiro s@gmail.comschr eef in bericht
                      news:1173706663 .870367.224360@ h3g2000cwc.goog legroups.com...
                      Tony K wrote:
                      >I have a MDI application. On the menu toolstrip child forms are selected
                      >from one of the menus. I don't want to play the disable/enable menu item
                      >game. I have selected that open forms are added to the "Window" menu
                      >item.
                      >What I'm looking for is when the user clicks on the menu item to open the
                      >form, how can I check to see if it's open already and if so then set the
                      >focus (if possible).
                      <snip>
                      >What can I change in this code that opens my child forms to prevent it
                      >from
                      >opening new instances?
                      >>
                      > Dim frmCustomers As New Customers()
                      > frmCustomers.Md iParent = Me
                      > frmCustomers.Wi ndowState = FormWindowState .Maximized
                      > frmCustomers.Sh ow()
                      >
                      If you're using VB 2005 you can use the My.Forms collection:
                      >
                      <aircode>
                      Dim frmCustomers As Customers = My.Forms.Custom ers
                      > frmCustomers.Md iParent = Me
                      > frmCustomers.Wi ndowState = FormWindowState .Maximized
                      > frmCustomers.Sh ow()
                      frmCustomers.Ac tivate
                      </aircode>
                      >
                      This will show the Customers form if it's not shown already (or was
                      closed) and give it focus if it's already shown.
                      >
                      HTH.
                      >
                      Regards,
                      >
                      Branco.
                      >

                      Comment

                      • Branco Medeiros

                        #12
                        Re: How to prevent multiple child forms opening

                        Cor Ligthert [MVP] wrote:
                        In all versions is the MDIChildren collection
                        >
                        http://msdn2.microsoft.com/en-us/lib....forms.form.md...
                        >
                        Because of your message I got the impression that you did not know that.
                        <snip>

                        Yes, but the MDIChildren collection lists all *open* child forms. That
                        would mean look up the child in the MDIChildren, and create a new
                        instance if it wasn't there... more trouble for what the OP was
                        asking, I guess.

                        Regards,

                        Branco.

                        Comment

                        Working...