Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show a new form when click a button

Tags:

c#

winforms

I am new in c#. I am trying to show a new form (form2) when click button in form1.

this is my 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 SliceEngine
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void button5_Click(object sender, EventArgs e)
        {   
            Form2 form2 = new Form2();
            form2.ShowDialog();            
         }
    }
}

the error show

the type or namespace name 'Form2' could not be found (are you missing a using directive or an assembly reference?)

this is my code for form2.

    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 SliceEngine
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }
    }
}

for form2, i just making the design interface.

all i know when using java, i only need to declare the object first. what should i do for this?

like image 972
sara brown Avatar asked Aug 02 '12 01:08

sara brown


People also ask

How do I open another form in Button click Visual Studio?

Now go to Solution Explorer and select your project and right-click on it and select a new Windows Forms form and provide the name for it as form2. Now click the submit button and write the code.

How do I show a new form in Visual Studio?

Add a new form with Visual Studio. In Visual Studio, find the Project Explorer pane. Right-click on the project and choose Add > Form (Windows Forms).

How do I display a form in C#?

Press F5 to build and run the application. Click on the button in the main form to display the sub form. Now, when you press the button in the sub form, the form will be hidden.


2 Answers

I don't see any reason to fail your code, unless you have any typo. I have tried the same code as yours and it worked well on my machine.

    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 winapp
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void button1_Click(object sender, EventArgs e)
            {
                Form2 frm2 = new Form2();
                frm2.ShowDialog();
            }
        }




    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 winapp
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
        }
    }
like image 77
Umesh Avatar answered Sep 23 '22 22:09

Umesh


the type or namespace name 'Form2' could not be found (are you missing a using directive or an assembly reference?)

It means that you forgot to add the namespace that points to the Form2 directory to your code

If you have got a Form2.cs inside a directory named UI and that directory is inside MyForms directory, then the whole tree would be ProjectName >> MyForms >> UI >> Form2.cs

So you should use this namespace in your code

using ProjectName.MyForms.UI;

Now I should be able to start showing it easily cause I've added its location.

new Form2().Show();

OR instead of all that and bother adding a namespace, you can just use:

new ProjectName.MyForms.UI.Form2().Show();
like image 38
Beyondo Avatar answered Sep 21 '22 22:09

Beyondo