C# 单击按钮时显示新表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11769890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Show a new form when click a button
提问by sara brown
I am new in c#. I am trying to show a new form (form2) when click button in form1.
我是 C# 新手。当单击 form1 中的按钮时,我试图显示一个新表单(form2)。
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?)
找不到类型或命名空间名称“Form2”(您是否缺少 using 指令或程序集引用?)
this is my code for 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)
{
}
}
}
for form2, i just making the design interface.
对于form2,我只是制作设计界面。
all i know when using java, i only need to declare the object first. what should i do for this?
我只知道在使用 java 时,我只需要先声明对象。我该怎么做?
回答by competent_tech
In form1, you are using the constructor for Form2:
在 form1 中,您正在使用 Form2 的构造函数:
public partial class Form1 : Form
{
public Form2()
{
InitializeComponent();
}
if you change it to
如果你把它改成
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
you should be fine.
你应该没事。
回答by Chibueze Opata
Your code claimsyou don't have a constructor for Form1.
您的代码声称您没有Form1.
public partial class Form1 : Form
{
public Form2()
{
InitializeComponent();
}
should be:
应该:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
回答by Umesh
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();
}
}
}
回答by Murali Narayanam
I assume below must be the reason why your code is failing. You have both of your forms in Form1 and Form2, where Form2 definition is done in another namespace directive, which is not integrated in the namespace of Form1, also you can't use same namespace directive name for two namespaces unless you are overriding them.
我认为下面一定是您的代码失败的原因。您在 Form1 和 Form2 中都有两个表单,其中 Form2 定义是在另一个命名空间指令中完成的,该指令未集成在 Form1 的命名空间中,您也不能对两个命名空间使用相同的命名空间指令名称,除非您覆盖它们。
回答by Ramgy Borja
Try this code.....
试试这个代码.....
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
{
frm2.ShowDialog();
}
}
回答by Karuna Soni
private void button5_Click(object sender, EventArgs e)
{
Form2.show()
}
回答by Beyondo
the type or namespace name 'Form2' could not be found (are you missing a using directive or an assembly reference?)
找不到类型或命名空间名称“Form2”(您是否缺少 using 指令或程序集引用?)
It means that you forgot to add the namespace that points to the Form2directory to your code
这意味着您忘记将指向Form2目录的命名空间添加到您的代码中
If you have got a Form2.csinside a directory named UIand that directory is inside MyFormsdirectory, then the whole tree would be ProjectName>> MyForms>> UI>> Form2.cs
如果你有一个Form2.cs名为的内部目录UI并且该目录在MyForms目录中,那么整个树将是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();
ORinstead of all that and bother adding a namespace, you can just use:
或者,您可以使用:
new ProjectName.MyForms.UI.Form2().Show();
回答by IT Vlogs
My solution:
我的解决方案:
In click event of Form1 include your button:
在 Form1 的单击事件中包括您的按钮:
string foobar = "Hello world";
Form2 frm2 = new Form2(foobar);
frm2.ShowDialog();
In Form2:
在 Form2 中:
public Form2(string foobar)
{
InitializeComponent();
textbox1.Text = foobar;
}

