C# 单击按钮时显示新表单

C# 单击按钮时显示新表单,c#,winforms,C#,Winforms,我是新来的。我试图在点击表单1中的按钮时显示一个新表单(表单2) 这是我的密码 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 SliceEn

我是新来的。我试图在点击表单1中的按钮时显示一个新表单(表单2)

这是我的密码

        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();            
         }
    }
}
错误显示

找不到类型或命名空间名称“Form2”(是否缺少) 使用指令或组件引用?)

这是我的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)
        {

        }
    }
}
对于form2,我只是制作设计界面


在使用java时,我只需要首先声明对象。对此我应该怎么做?

在form1中,您正在使用Form2的构造函数:

public partial class Form1 : Form
{
    public Form2()
    {
        InitializeComponent();
    }
如果你把它改成

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
你应该没事。

你的代码声称你没有
表单1的构造函数

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

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

我看不出有任何理由让你的代码失败,除非你有任何打字错误。我试过和你一样的代码,在我的机器上运行得很好

    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();
            }
        }
    }

我想下面一定是你的代码失败的原因。 您的两个表单都在Form1和Form2中,其中Form2定义是在另一个命名空间指令中完成的,该指令未集成在Form1的命名空间中,并且您不能对两个命名空间使用相同的命名空间指令名称,除非您正在重写它们。

请尝试此代码

private void button1_Click(object sender, EventArgs e)
{
       Form2 frm2 = new Form2();
       {
          frm2.ShowDialog();
       }
}
找不到类型或命名空间名称“Form2”(是否缺少) 使用指令或组件引用?)

这意味着您忘记将指向
Form2
目录的名称空间添加到代码中

using ProjectName.MyForms.UI;
如果您在名为
UI
的目录中有一个
Form2.cs
,并且该目录在
MyForms
目录中,那么整个树将是
ProjectName
MyForms
UI
Form2.cs

所以您应该在代码中使用这个名称空间

using ProjectName.MyForms.UI;
现在我应该能够很容易地开始显示它,因为我已经添加了它的位置

new Form2().Show();
您只需使用以下命令,即可轻松添加名称空间:

new ProjectName.MyForms.UI.Form2().Show();
我的解决方案:

在Form1的单击事件中,包括您的按钮:

string foobar = "Hello world";
Form2 frm2 = new Form2(foobar);
frm2.ShowDialog();
表格2:

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

对于仍在寻找答案的任何人:

在代码顶部,添加以下命名空间:

using YourProjectName;
然后,当您希望显示表单时,请键入以下内容:

var form = YourProjectName.YourFormName();
form.Show(); // Show form using new variable

您是否在项目中创建了
Form2
表单?请确认您的代码中存在
Form2
。请记住,文件名并不总是与其中的类相关,因此请务必仔细检查。仅供参考:您不“调用”窗体,而是显示或显示窗体。@sarabrown,请您发布两个代码示例,以便我们看到发生了什么事?Form2与Form1在同一个项目中(不仅仅是同一个解决方案)吗?你是真的复制并粘贴了这段代码,还是把它打印出来了?如果这不是你的全部代码,你可能已经隐藏了问题。哦,对不起。我在这篇文章中有点打字错误。我的真实代码和你的一样。你能帮我吗,请@competable\u tech对不起。我真正的代码是正确的。就像你说的那样。我在这篇文章中犯了一个打字错误。你能再帮我一次吗@蛋白石