我是 C# 的新手。单击 form1 中的按钮时,我试图显示一个新表单 (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 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 type or namespace name 'Form2' could not be found (are you missing a using directive or an assembly reference?)
这是我的 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时,我只需要先声明对象。我应该为此做什么?
最佳答案
我看不出有任何理由让您的代码失败,除非您有任何拼写错误。我已经尝试过与您相同的代码,它在我的机器上运行良好。
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();
}
}
}
关于c# - 单击按钮时显示新表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11769890/