Checking to see if a form exists

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

    #1

    Checking to see if a form exists


    I am using code to create a new modal form from a parent form. It is not an
    MDI form. I need to know if an instance of the form already exists so when
    the user clicks on the button I can close any previous version of the form.
    Here is the code I am using to create the form:

    frmFindReplace frmFind = new frmFindReplace( );

    frmFind.Show();

    frmFind.Prepare Form(this.dataG ridView1);





    Thanks!

    Ron


  • Lebesgue

    #2
    Re: Checking to see if a form exists

    The easiest solution would be to include a static variable in class frmFind
    (FindForm would be a better name ;-) ) which indicates if the form was
    already instantiated. Then read this property or implement a static method
    to show the form and eventually disallow instantiating it direcly by making
    the constructor private.

    class FindForm : Form
    {
    private static bool isShown;
    public static bool IsShown
    {
    get { return isShown; }
    }

    private FindForm()
    {
    }

    public static FindForm CreateAndShow()
    {
    FindForm res = new FindForm();
    res.Show();
    //eventually handle the closed event if you want to be absolutely
    correct and precise
    isShown = true;
    return res;
    }

    }

    "RSH" <way_beyond_oop s@yahoo.com> wrote in message
    news:uzNoV5HEGH A.2036@TK2MSFTN GP14.phx.gbl...[color=blue]
    >
    > I am using code to create a new modal form from a parent form. It is not
    > an MDI form. I need to know if an instance of the form already exists so
    > when the user clicks on the button I can close any previous version of the
    > form. Here is the code I am using to create the form:
    >
    > frmFindReplace frmFind = new frmFindReplace( );
    >
    > frmFind.Show();
    >
    > frmFind.Prepare Form(this.dataG ridView1);
    >
    >
    >
    >
    >
    > Thanks!
    >
    > Ron
    >
    >[/color]


    Comment

    • RSH

      #3
      Re: Checking to see if a form exists

      I realized after posting this that there are a couple more crucial elements
      involved.

      I have a parent form that basically has two buttons. Each button spawns a
      different child form. Basically I need to make sure that only one child
      form can be spawned at one time and when a child form is displayed the
      parent form is hidden, but when a child form is closed I need the parent
      form to be shown again.

      The problem I am running into is how to reference the parent form from the
      children when I want to unhide it, and how to find out if a child form of
      either type exists and close it before spawning a new instance.

      Thanks for any help you might be able to give me,
      Ron


      "Lebesgue" <nospam@spam.jp > wrote in message
      news:umjAVNIEGH A.3820@TK2MSFTN GP12.phx.gbl...[color=blue]
      > The easiest solution would be to include a static variable in class
      > frmFind (FindForm would be a better name ;-) ) which indicates if the form
      > was already instantiated. Then read this property or implement a static
      > method to show the form and eventually disallow instantiating it direcly
      > by making the constructor private.
      >
      > class FindForm : Form
      > {
      > private static bool isShown;
      > public static bool IsShown
      > {
      > get { return isShown; }
      > }
      >
      > private FindForm()
      > {
      > }
      >
      > public static FindForm CreateAndShow()
      > {
      > FindForm res = new FindForm();
      > res.Show();
      > //eventually handle the closed event if you want to be absolutely
      > correct and precise
      > isShown = true;
      > return res;
      > }
      >
      > }
      >
      > "RSH" <way_beyond_oop s@yahoo.com> wrote in message
      > news:uzNoV5HEGH A.2036@TK2MSFTN GP14.phx.gbl...[color=green]
      >>
      >> I am using code to create a new modal form from a parent form. It is not
      >> an MDI form. I need to know if an instance of the form already exists so
      >> when the user clicks on the button I can close any previous version of
      >> the form. Here is the code I am using to create the form:
      >>
      >> frmFindReplace frmFind = new frmFindReplace( );
      >>
      >> frmFind.Show();
      >>
      >> frmFind.Prepare Form(this.dataG ridView1);
      >>
      >>
      >>
      >>
      >>
      >> Thanks!
      >>
      >> Ron
      >>
      >>[/color]
      >
      >[/color]


      Comment

      • Lebesgue

        #4
        Re: Checking to see if a form exists

        You can hook onto the Closed event of the child form from the parent

        private void ShowChild()
        {
        ChildForm f = ChildForm.Show( ); //see me previous post, this is the
        "close existing form before spawning new instance"
        f.Closed += new EventHandler(Ch ildClosed);
        this.Visible = false;
        }

        private void ChildClosed(obj ect sender, EventArgs e)
        {
        this.Visible = true;
        }

        if we want to speak about "how to pass the reference of the parent to the
        child form, the solution is simple: pass it as argument to constructor (and
        modify the constructor, of course)

        ChildForm f = new ChildForm(this) ;

        "RSH" <way_beyond_oop s@yahoo.com> wrote in message
        news:O76dEXJEGH A.2708@TK2MSFTN GP11.phx.gbl...[color=blue]
        >I realized after posting this that there are a couple more crucial elements
        >involved.
        >
        > I have a parent form that basically has two buttons. Each button spawns a
        > different child form. Basically I need to make sure that only one child
        > form can be spawned at one time and when a child form is displayed the
        > parent form is hidden, but when a child form is closed I need the parent
        > form to be shown again.
        >
        > The problem I am running into is how to reference the parent form from the
        > children when I want to unhide it, and how to find out if a child form of
        > either type exists and close it before spawning a new instance.
        >
        > Thanks for any help you might be able to give me,
        > Ron
        >
        >
        > "Lebesgue" <nospam@spam.jp > wrote in message
        > news:umjAVNIEGH A.3820@TK2MSFTN GP12.phx.gbl...[color=green]
        >> The easiest solution would be to include a static variable in class
        >> frmFind (FindForm would be a better name ;-) ) which indicates if the
        >> form was already instantiated. Then read this property or implement a
        >> static method to show the form and eventually disallow instantiating it
        >> direcly by making the constructor private.
        >>
        >> class FindForm : Form
        >> {
        >> private static bool isShown;
        >> public static bool IsShown
        >> {
        >> get { return isShown; }
        >> }
        >>
        >> private FindForm()
        >> {
        >> }
        >>
        >> public static FindForm CreateAndShow()
        >> {
        >> FindForm res = new FindForm();
        >> res.Show();
        >> //eventually handle the closed event if you want to be absolutely
        >> correct and precise
        >> isShown = true;
        >> return res;
        >> }
        >>
        >> }
        >>
        >> "RSH" <way_beyond_oop s@yahoo.com> wrote in message
        >> news:uzNoV5HEGH A.2036@TK2MSFTN GP14.phx.gbl...[color=darkred]
        >>>
        >>> I am using code to create a new modal form from a parent form. It is
        >>> not an MDI form. I need to know if an instance of the form already
        >>> exists so when the user clicks on the button I can close any previous
        >>> version of the form. Here is the code I am using to create the form:
        >>>
        >>> frmFindReplace frmFind = new frmFindReplace( );
        >>>
        >>> frmFind.Show();
        >>>
        >>> frmFind.Prepare Form(this.dataG ridView1);
        >>>
        >>>
        >>>
        >>>
        >>>
        >>> Thanks!
        >>>
        >>> Ron
        >>>
        >>>[/color]
        >>
        >>[/color]
        >
        >[/color]


        Comment

        • Bruce Wood

          #5
          Re: Checking to see if a form exists

          In the case of the child forms, I suggest that what you want is not to
          "close any existing child form in order to open the new one," but
          rather "if there is an existing child form open, show it, otherwise
          create one."

          The latter is the Singleton pattern, which is easily applied to Forms,
          something like this:

          public class ChildForm
          {
          private static ChildForm _instance = null;

          private ChildForm()
          {
          ... do usual constructor stuff... note that constructor is
          "private" ...
          }

          public static ChildForm Instance
          {
          get
          {
          if (ChildForm._ins tance == null)
          {
          ChildForm._inst ance = new ChildForm();
          }
          return ChildForm._inst ance;
          }
          }
          }

          This way, whenever you want (the one and only) child form, you just
          say:

          ChildForm.Insta nce

          If there is no child form, then it will create one. If there already is
          one, it will give it back to you. So, in your parent form code, you can
          just say:

          ChildForm.Insta nce.Show()

          to show (or create and then show) the child form.

          Comment

          • RSH

            #6
            Re: Checking to see if a form exists

            Thanks!

            There is some really good stuff here. I see that forms really are nothing
            but objects...for some reason I was having a tough time with that paradigm.


            "RSH" <way_beyond_oop s@yahoo.com> wrote in message
            news:uzNoV5HEGH A.2036@TK2MSFTN GP14.phx.gbl...[color=blue]
            >
            > I am using code to create a new modal form from a parent form. It is not
            > an MDI form. I need to know if an instance of the form already exists so
            > when the user clicks on the button I can close any previous version of the
            > form. Here is the code I am using to create the form:
            >
            > frmFindReplace frmFind = new frmFindReplace( );
            >
            > frmFind.Show();
            >
            > frmFind.Prepare Form(this.dataG ridView1);
            >
            >
            >
            >
            >
            > Thanks!
            >
            > Ron
            >
            >[/color]


            Comment

            Working...