Show new form when button is clicked

I am new to C #. I am trying to show a new form (form2) when I click a 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(); } } } 

error shows

the type or namespace name 'Form2' cannot be found (did you miss the use directive or 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'm just doing an interface design.

all that I know when using Java, I only need to declare an object first. What should I do for this?

+6
source share
8 answers

I see no reason to refuse your code if you do not have a typo. I tried the same code as yours and it worked perfectly 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(); } } } 
+4
source

the name of the type or namespace 'Form2' cannot be found (are you missing the using directive or assembly references?)

This means that you forgot to add a namespace that points to the Form2 directory in your code

If you have Form2.cs inside a directory called UI and this directory is inside MyForms , then the whole tree will be ProjectName >> MyForms >> UI >> Form2.cs

So you should use this namespace in your code

 using ProjectName.MyForms.UI; 

Now I have to start showing it easily, because I added its location.

 new Form2().Show(); 

OR instead of all this and bothering to add a namespace, you can simply use:

 new ProjectName.MyForms.UI.Form2().Show(); 
+2
source

In form1, you use the constructor for Form2:

 public partial class Form1 : Form { public Form2() { InitializeComponent(); } 

if you change it to

 public partial class Form1 : Form { public Form1() { InitializeComponent(); } 

you should be fine.

+1
source

Your code claims that you do not have a constructor for Form1 .

  public partial class Form1 : Form { public Form2() { InitializeComponent(); } 

it should be:

  public partial class Form1 : Form { public Form1() { InitializeComponent(); } 
+1
source

I assume that below should be the reason your code is not working. You have both forms in Form1 and Form2, where the definition of Form2 is done in a different namespace directive that is not integrated into the namespace of Form1, also you cannot use the same namespace directive name for two namespaces, unless you redefine them.

+1
source

Try this code .....

 private void button1_Click(object sender, EventArgs e) { Form2 frm2 = new Form2(); { frm2.ShowDialog(); } } 
+1
source
 private void button5_Click(object sender, EventArgs e) { Form2.show() } 
0
source

My decision:

In the event of clicking form Form1, include your button:

 string foobar = "Hello world"; Form2 frm2 = new Form2(foobar); frm2.ShowDialog(); 

In form 2:

 public Form2(string foobar) { InitializeComponent(); textbox1.Text = foobar; } 
0
source

All Articles