c# - 单击按钮时显示新表单

标签 c# winforms

我是 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/

相关文章:

c# - 在其他控件上方显示透明加载微调器

c# - WinForms App + MS SQL Server DB 迁移到 Azure 平台

c# - 为什么文本框点击事件不触发

c# - Cosmos DB 更改源监控与轮询

c# - X 是一个变量,但在尝试转换时像类型一样使用

c# - 如何在工厂中使用通用接口(interface)

c# - 按钮启用属性无法正常工作

c# - 像队列这样的数据结构可以访问最后一个元素

c# - regex.replace 中 $0 的问题 - c#

c# - ElementHost 内的 WPF 控件呈现问题