C# 我不能从第一张表格打开第二张表格
我在互联网上找到了如何用C#打开另一个表单,但我有错误的问题C# 我不能从第一张表格打开第二张表格,c#,.net,visual-studio,C#,.net,Visual Studio,我在互联网上找到了如何用C#打开另一个表单,但我有错误的问题 `using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; nam
`using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void PlusProduct_Click(object sender, EventArgs e)
{
Form2 newForm = new Form2();
newForm.Show();
}
}
}
`
找不到类型或命名空间名称“Form2”(是否缺少using指令或程序集引用?)
我必须在点击btn时从Form1 Form2打开。您将需要
Form newForm=new Form()
除非您实际创建了一个名为Form2
的类,否则请转到您的ProjectName->右键单击->添加->“Windows窗体…”并将其命名为Form2。
您的代码现在应该可以工作了,您现在所要做的就是自定义它(第二个表单)。对于现有的主表单,您有以下内容
namespace WindowsFormsApp1
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void PlusProduct_Click(object sender, EventArgs e)
{
Form2 newForm = new Form2();
newForm.Show();
}
}
}
您要做的是使用创建Form2,确保它是与现有项目一起创建/添加的,并且它还应该使用相同/当前的命名空间windowsformsap1
从这里,您需要确保您已经设置了有关属性、方法等的Form2
从MainForm中,您希望这样编写代码
我创建了表格2。不知道你是如何设置项目的,很难说发生了什么。例如,如果Form2位于不同的命名空间中,则需要一个using语句。我推荐一些关于WinForms开发的入门教程来帮助您入门。我将Form2添加到项目中,但我有这个错误。
namespace WindowsFormsApp1
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void PlusProduct_Click(object sender, EventArgs e)
{
Form2 newForm = new Form2();
newForm.ShowDialog();
newForm.Dispose();
}
}
}