简体   繁体   中英

how to reference a Form in another folder (solution explorer)

I'm new to C#. I have 2 forms to open.

My solution explorer is like this:

  • NewFolder <-- here is form3.cs
  • Form2.cs
  • Form1.cs

Here is my code

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

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

    private void button2_Click(object sender, EventArgs e)
    {
    }
}

How to open form 3 in the folder when I click button2?

You can try one of two ways. One, type in this code:

form3 frm3 = new form3();

Right-click on "form3" in Visual Studio, and find "resolve". Click that to resolve the reference. (note you have to type the name exactly for "resolve" to appear, and it's case sensitive).

Alternatively, you can try and figure out the namespace. By default, I'd expect it to be "NewFolder.form3" (that's how Visual Studio sets the namespaces). You could also try wrapping your "form3" code in a namespace like "MyApp.Forms", then call "MyApp.Forms.form3" in your code. Wrap like this:

namespace MyApp.Forms
{
    public partial class form3 : Form
    {
        ...
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM