Brandon
Brandon

Reputation: 161

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

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

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

The following code should "launch" form2. They are in the same project, and have the same namespace. I don't understand. I have tried changing the .NET framework from version 4 client to version 4.

Any other ideas?

Upvotes: 0

Views: 13081

Answers (4)

Samad
Samad

Reputation: 13

You can solve this issue in two wais:

  1. You remove the namespace outside your form's class code or
  2. Add a "using projectName" at the top of the code in your form.

Upvotes: 0

Rafik Bari
Rafik Bari

Reputation: 5037

Both forms should have the same namespace

Upvotes: -1

jrh_Engineer
jrh_Engineer

Reputation: 31

In my case I created form2, but renamed it later.

I found that although the name of the form changed, the name of the class remained as I originally created it.

For example: 1. Project->Add Windows Form (Setup.cs) 2. File->Save Setup.cs as... (frmSetup.cs) 3. Add code: frmSetup SetupForm = new frmSetup();

However, the actual name of the Setup class did not change with the file name, so changing my code to:

Setup SetupForm = new Setup();

So I was referencing the class name and not the file name solved the problem for me.

Upvotes: 3

Yaur
Yaur

Reputation: 7452

right click on Form2, chose "resolve" and pick one of the resolutions, alternatively move it to the WindowsFormsApplication3 namespace.

Upvotes: 3

Related Questions