c# current form close problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dotnetnovice
    New Member
    • Aug 2009
    • 21

    c# current form close problem

    Hi everybody.

    I need some help in c# forms problem which is

    i have three forms naming main form,form1 & form2. main form is the mdiparent of both form1 and form2. i also have a toolstrip in my main form which includes option close current frm and close all. what i need is when i click the close current form option the form opened state(either form1 or form2) close down.

    suppose currently form 2 is currently open in the main form and i want to close that current open form how i do this.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    If you know which form is the "current" form, then you could either call the .Close() of the correct form to actually close the form, or maybe just Hide() if you only want it hidden, not closed.

    Comment

    • dotnetnovice
      New Member
      • Aug 2009
      • 21

      #3
      Hi Plater how would i decide which form is open it can be any one either Form1 or Form2 My question is how would i check which form is open.

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        I've never worked with an MDI interface before, but unless it explicitly tracks which form is active (and somehow I doubt it :P), you'll need to track it yourself.

        One way would be to have the form focus event (or is it activated? I'm not sure... whichever one gets raised when you activate a form) send an event to your controlling method with a reference to the most recently active form.

        Then your main form can close whichever that one is.

        Comment

        • dotnetnovice
          New Member
          • Aug 2009
          • 21

          #5
          Ok i will try this one

          Comment

          • fastestindian
            New Member
            • Aug 2009
            • 74

            #6
            U can declare a static global variable which will maintain the current instance of the form.

            Comment

            • ZokiManas
              New Member
              • Sep 2009
              • 2

              #7
              Try this code:

              void CloseChildForm( string formName)
              {
              foreach (Form childForm in this.MdiChildre n)
              {
              if (childForm.Name == "formName")
              {
              childForm.Close ();
              return;
              }
              }
              }

              Comment

              Working...