question

GM-0106 avatar image
1 Vote"
GM-0106 asked JackJJun-MSFT commented

How can I fetch the form inside a file created in solution explorer in c#?

When calling a form in solution explorer I use:

Form1 go= new Form1();
go.ShowDialog();

But what code should I write to fetch the form in the file?

Exp:

Form2 go2= new NewFolder.Form2();
go2.ShowDialog();

dotnet-csharp
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


What error do you get in this case: Form2 go2= new Form2( ). Sometimes you can place the caret on this line, then press <Alt Enter> to solve it.


0 Votes 0 ·

@GM-0106, Welcome to Microsoft Q&A, based on my test, karen's answer is correct.
In my situation, the folder is Test.
I add using code in the left-top of the code then I can call the code successfully.

 using WindowsFormsApp1.Test;

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


Please check it again.

0 Votes 0 ·

1 Answer

karenpayneoregon avatar image
1 Vote"
karenpayneoregon answered karenpayneoregon commented

When a form resides in a folder within the project it's namespace will be different so you need to reference in this case Form2 to it's namespace.

Example, say the project name is WinFormDemo and Form2 is in a folder named WinFormDemo\Forms.

Form2 go2 = new WinFormDemo.Forms.Form2();

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you very much for your reply,

I tried the code you wrote but unfortunately it didn't work for me :(

0 Votes 0 ·

Best way to assist with this than is to place your project in a GitHub repository. If you never used GitHub see this page.

Also it didn't work for me tells us nothing.


0 Votes 0 ·