Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#使用program.cs参考表格1中的表格2有错误 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用System.Windows.Forms; 名称空间测试 { 静态类程序 { /// ///应用程序的主要入口点。 /// 公共静态表格1 f; 公共静态表格2 f2; [状态线程] 静态void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(新Form1()); f=新的Form1(); f2=新形式2(); } } } 私有无效按钮1\u单击(对象发送者,事件参数e) { Program.f2.Show();_C# - Fatal编程技术网

c#使用program.cs参考表格1中的表格2有错误 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用System.Windows.Forms; 名称空间测试 { 静态类程序 { /// ///应用程序的主要入口点。 /// 公共静态表格1 f; 公共静态表格2 f2; [状态线程] 静态void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(新Form1()); f=新的Form1(); f2=新形式2(); } } } 私有无效按钮1\u单击(对象发送者,事件参数e) { Program.f2.Show();

c#使用program.cs参考表格1中的表格2有错误 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用System.Windows.Forms; 名称空间测试 { 静态类程序 { /// ///应用程序的主要入口点。 /// 公共静态表格1 f; 公共静态表格2 f2; [状态线程] 静态void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(新Form1()); f=新的Form1(); f2=新形式2(); } } } 私有无效按钮1\u单击(对象发送者,事件参数e) { Program.f2.Show();,c#,C#,this.Hide(); } 该按钮给出“对象引用未设置为对象实例。”错误。我该如何解决这个问题?我在代码中没有看到任何错误。错误: “对象引用未设置为对象的实例。” 表示u试图调用的对象尚未初始化,您正在调用f2.Show(),但在调用它之前必须对其进行初始化 您应该初始化Form2,然后使用您指定的名称调用它 替换: using System; using System.Collections.Generic; using System.Linq; using System.Windows.

this.Hide(); }

该按钮给出“对象引用未设置为对象实例。”错误。我该如何解决这个问题?我在代码中没有看到任何错误。

错误:

“对象引用未设置为对象的实例。”

表示u试图调用的对象尚未初始化,您正在调用
f2.Show(),但在调用它之前必须对其进行初始化

您应该初始化Form2,然后使用您指定的名称调用它

替换:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Test
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        public static Form1 f;
        public static Form2 f2;
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
            f = new Form1();
            f2 = new Form2();
        }
    }
}
private void button1_Click(object sender, EventArgs e)
{
 Program.f2.Show();
与:


技术上的问题在于:

 private void button1_Click(object sender, EventArgs e)
 {
     var f2 = new Form2();
     f2.Show();
     this.Hide();
 }
实际上,Application.Run()会一直阻塞直到主窗体关闭,因此下面的两行(初始化窗体的地方)永远不会运行。要“修复”它,您需要将这些队列向上移动:

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
        f = new Form1();
        f2 = new Form2();
不过,看起来您希望“f”是对Form1的引用,所以应该将其传递给Application.Run():

但是,我会将这些实例包装在属性中,这样您就可以确保它们被正确实例化(例如当您关闭Form2并再次尝试重新打开它时)。可能看起来像这样:

        f = new Form1();
        f2 = new Form2();
        Application.Run(f);
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        f = new Form1();
        f2 = new Form2();
        Application.Run(new Form1());
        f = new Form1();
        f2 = new Form2();
        Application.Run(f);
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    private static Form1 f1;
    private static Form2 f2;

    public static Form1 F1
    {
        get
        {
            if (f1 == null || f1.IsDisposed)
            {
                f1 = new Form1();
            }
            return f1;
        }
    }

    public static Form2 F2
    {
        get
        {
            if (f2 == null || f2.IsDisposed)
            {
                f2 = new Form2();
            }
            return f2;
        }
    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(Program.F1);
    }

}
    private void button1_Click(object sender, EventArgs e)
    {
        Program.F2.Show();
    }