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?
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.
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).
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.
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();
}
}
}
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With