Open a Form C#

This is a discussion on Open a Form C# within the Windows Programming forums, part of the Platform Specific Boards category; I have just created "WindowsFormsApplication1". To this I have "Form1". I have Added a New Item to this project: "Form2" ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    383

    Open a Form C#

    I have just created "WindowsFormsApplication1".
    To this I have "Form1".
    I have Added a New Item to this project: "Form2"

    So I have 2 Forms.

    Now I have created a button on "Form1". With this button I want to open
    "Form2".

    I have never done this before so I have no idéa where to start.

    Code for "Form1"
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
    
            }
        }
    }
    "Form2"
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
    
            private void Form2_Load(object sender, EventArgs e)
            {
    
            }
        }
    }
    What will I do with this code now, the only thing I know is that this possible have something to do in order to open "Form2" ?.

    Code:
    Form2.Show();

  2. #2
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace CboardExample {
      
      public partial class Form1 : Form {
        private Form form2instance;
        public Form1() {
          InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e) {
          this.form2instance = new Form2();
          this.form2instance.Show();
        }
      }
    }
    And form 2 is just an empty form...

    So basically before applying Show to some object , you first need a form2 instance available.

    This should be in the C# board instead of the windows programming form.

    There is still something wrong though with the code above, just try to press the button multiple times and see what happens, up to you to fix that.
    Last edited by GanglyLamb; 01-30-2008 at 04:13 PM.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    Thank you. This did work but as you said it open a lot of window when clicking many times.. I will work with this and see what could be found out...

  4. #4
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    Code:
    On button click:
     - if form2 does not exists 
           create form2 instance
    
      show form2 instance

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Open a Form within a Form
    By Coding in forum Windows Programming
    Replies: 3
    Last Post: 01-29-2008, 07:13 PM
  2. Open Software License 3.0 Explained
    By laserlight in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 01-08-2008, 07:10 PM
  3. open empty file
    By mystic-d in forum C Programming
    Replies: 2
    Last Post: 11-16-2007, 09:27 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. To open or not to open ?
    By darfader in forum C Programming
    Replies: 7
    Last Post: 09-24-2003, 08:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21