Adding a form to a C# program.

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

    Adding a form to a C# program.

    After adding a form to a C# program, I am unable to use it. I want to
    use form1 as a sort of menu, so that when the user clicks a button,
    form1 will close and form2 (or form3, etc) will open. What code must I
    write to make this happen? I am new to programming, so go easy on me
    with the explanation please.
    Thank you for any help you can offer.
  • Alberto Poblacion

    #2
    Re: Adding a form to a C# program.

    "Jonny Relentless" <JonnyRelentles s@gmail.comwrot e in message
    news:7dff6b63-28ee-49c0-a6e6-ba8a9f3415bc@x3 0g2000hsd.googl egroups.com...
    After adding a form to a C# program, I am unable to use it. I want to
    use form1 as a sort of menu, so that when the user clicks a button,
    form1 will close and form2 (or form3, etc) will open. What code must I
    write to make this happen? I am new to programming, so go easy on me
    with the explanation please.
    Thank you for any help you can offer.
    Add a Button to Form1. Double-click the button to open its "click" event
    handler in the code-behind. Enter the following:

    Form2 frm = new Form2();
    frm.Show();
    this.Close();

    The first instruction creates an instance of the form2. The second
    instruction shows it on screen (in a non-modal way, so your code continues
    executing the next line). The third instruction closes the form that is
    currently executing, which is the Form1 that contains the button.

    Do the same with additional buttons and additional forms. As you continue to
    learn, you will find that there are ways to simplify this, so that a single
    "click" handler can be used for multiple buttons, but you don't need to do
    this if you are just starting to experiment.

    Comment

    • Jonny Relentless

      #3
      Re: Adding a form to a C# program.

      On Feb 27, 12:25 pm, "Alberto Poblacion" <earthling-
      quitaestoparaco ntes...@poblaci on.orgwrote:
      "Jonny Relentless" <JonnyRelentl.. .@gmail.comwrot e in message
      >
      news:7dff6b63-28ee-49c0-a6e6-ba8a9f3415bc@x3 0g2000hsd.googl egroups.com...
      >
      After adding a form to a C# program, I am unable to use it. I want to
      use form1 as a sort of menu, so that when the user clicks a button,
      form1 will close and form2 (or form3, etc) will open. What code must I
      write to make this happen? I am new to programming, so go easy on me
      with the explanation please.
      Thank you for any help you can offer.
      >
      Add a Button to Form1. Double-click the button to open its "click" event
      handler in the code-behind. Enter the following:
      >
      Form2 frm = new Form2();
      frm.Show();
      this.Close();
      >
      The first instruction creates an instance of the form2. The second
      instruction shows it on screen (in a non-modal way, so your code continues
      executing the next line). The third instruction closes the form that is
      currently executing, which is the Form1 that contains the button.
      >
      Do the same with additional buttons and additional forms. As you continue to
      learn, you will find that there are ways to simplify this, so that a single
      "click" handler can be used for multiple buttons, but you don't need to do
      this if you are just starting to experiment.
      Thanks! That seems to be working, except that as soon as I click the
      button, form2 opens and then immediately closes, ending the debugging
      session. Do you know why? I added a button to form2 (trying to make it
      go back to form1), but it doesn't wait for input from the user, it
      just closes.

      Comment

      • Matt

        #4
        Re: Adding a form to a C# program.

        On Feb 27, 12:21 pm, Jonny Relentless <JonnyRelentl.. .@gmail.com>
        wrote:
        On Feb 27, 12:25 pm, "Alberto Poblacion" <earthling-
        >
        >
        >
        >
        >
        quitaestoparaco ntes...@poblaci on.orgwrote:
        "Jonny Relentless" <JonnyRelentl.. .@gmail.comwrot e in message
        >
        news:7dff6b63-28ee-49c0-a6e6-ba8a9f3415bc@x3 0g2000hsd.googl egroups.com...
        >
        After adding a form to a C# program, I am unable to use it. I want to
        use form1 as a sort of menu, so that when the user clicks a button,
        form1 will close and form2 (or form3, etc) will open. What code must I
        write to make this happen? I am new to programming, so go easy on me
        with the explanation please.
        Thank you for any help you can offer.
        >
        Add a Button to Form1. Double-click the button to open its "click" event
        handler in the code-behind. Enter the following:
        >
        Form2 frm = new Form2();
        frm.Show();
        this.Close();
        >
        The first instruction creates an instance of the form2. The second
        instruction shows it on screen (in a non-modal way, so your code continues
        executing the next line). The third instruction closes the form that is
        currently executing, which is the Form1 that contains the button.
        >
        Do the same with additional buttons and additional forms. As you continue to
        learn, you will find that there are ways to simplify this, so that a single
        "click" handler can be used for multiple buttons, but you don't need to do
        this if you are just starting to experiment.
        >
        Thanks! That seems to be working, except that as soon as I click the
        button, form2 opens and then immediately closes, ending the debugging
        session. Do you know why? I added a button to form2 (trying to make it
        go back to form1), but it doesn't wait for input from the user, it
        just closes.
        If you open the program.cs file for your project you will see
        something like this:

        Application.Ena bleVisualStyles ();
        Application.Set CompatibleTextR enderingDefault (false);
        Application.Run (new Form1());

        The new Form1() terminates the program when the form is closed. Thus,
        you can't
        "close" form1, but you can hide it.

        Matt

        Comment

        • Alberto Poblacion

          #5
          Re: Adding a form to a C# program.

          "Jonny Relentless" <JonnyRelentles s@gmail.comwrot e in message
          news:1b8627fa-45ad-42fc-aee6-6c215bfb5f71@c3 3g2000hsd.googl egroups.com...
          Thanks! That seems to be working, except that as soon as I click the
          button, form2 opens and then immediately closes, ending the debugging
          session. Do you know why?
          I suspect that Form1 is the first form that you open in your program, and
          you are opening it the "default" way, which is
          Application.Run (new Form1());
          (this code is automatically generated for you inside Program.cs).

          When you start the form like that, the application terminates when you
          close Form1. Since you are closing it after showing Form2, that is why Form2
          is closing.
          There are two possible solutions:
          (1) Hide Form1 instead of Closing it (just change this.Close() into
          this.Hide()).
          or (2) Modify Program.cs like this:
          (new Form1()).Show() ;
          Applicaton.Run( );


          Comment

          Working...