c# - Form.ActiveForm 有时有效

标签 c# .net winforms user-interface active-form

我知道这是糟糕的设计,但我一直将对 GUI 表单类的控制权转移到 C# 程序中的其他类。

这已经工作很长时间了,使用:

var form = Form.ActiveForm as Form1;

随着我的程序变得越来越大,并且表单被更多的类打开,它的行为变得随机。有时我可以毫无错误地运行我的程序,但有时它会随机选择一个位置并抛出 NullReferenceException。

例如,以下两行代码来自被调用的八个函数。

var form = Form.ActiveForm as Form1;
form.richTextBox1.AppendText("Section ID: ");

既然程序到了这一步,我知道它之前能够传递表单并操作相同的字段。

当以前完全相同的事情发生时,你有什么想法为什么它选择抛出它?

据我所知,Form1 是唯一的形式。我对 GUI 开发和 C#/.NET 100% 陌生,所以我在这里可能是错的。

我的一个理论如下:有两个类,Reader 和Analyze,继承了Form 对象。 Reader首先继承它,没有问题。问题立即抛出到分析中。我不是放弃了Reader课上的表格吗?

编辑:我的第一预感是使用 ActiveForm 属性的多个类会导致问题。我将所有函数放入 Reader 类中。这并没有解决问题。

以下是 Reader 中的两种方法。首先,read() 在SectionHeaderMatch() 之前调用。 Read 使用 ActiveForm 属性并且永远不会出现问题,就像在它之前调用的另一个方法一样。 SectionHeaderMatch 是使用 ActiveForm 属性的第三种方法。在此方法中,form 设置为 null,而不是 Form1。我假设 read() 中的某些东西把事情搞砸了。

public static void read()
    {
        var form = Form.ActiveForm as Form1;

        StringBuilder outPut = new StringBuilder();
        outPut.Append(form.textBox2.Text);
        outPut.Append("\\out.txt");

        string cardDrive = String.Format("\\\\.\\{0}", form.textBox1.Text);
        cardDrive = cardDrive.Remove(6);
        SafeFileHandle fileHandle = CreateFile(cardDrive, 0x80000000, 0, IntPtr.Zero, 3, 0, IntPtr.Zero);
        FileStream readStream = new FileStream(fileHandle, FileAccess.Read);
        BinaryReader reader = new BinaryReader(readStream);
        FileStream writeStream = File.OpenWrite(outPut.ToString()); //Writing stream opened at the specified directory of output.

        long gigsRead; //Loop counter that specifies the number of gigabytes read thus far.
        long megsRead; //Loop counter that specifies the number of megabytes read thus far within the current gigabyte.

        for (gigsRead = 0; gigsRead < 0; gigsRead++)
        {
            for (megsRead = 0; megsRead < 1024; megsRead++)
            {
                byte[] buffer = new byte[1048576];
                long position = gigsRead * 1073741824 + megsRead * 1048576;
                reader.BaseStream.Position = position;
                reader.Read(buffer, 0, 1048576); //Read from SD card to buffer
                writeStream.Write(buffer, 0, 1048576); //Write from buffer to output text file.
                writeStream.Flush();
            }
        }

        for (megsRead = 0; megsRead < 432; megsRead++)
        {
            byte[] buffer = new byte[1048576];
            long gigSize = 1073741824;
            long position = 7 * gigSize + megsRead * 1048576;
            reader.BaseStream.Position = position;
            reader.Read(buffer, 0, 1048576); //Read from SD card to buffer
            writeStream.Write(buffer, 0, 1048576); //Write from buffer to output text file.
            writeStream.Flush();
        }
        writeStream.Close();
        readStream.Close();
        reader.Close();
        fileHandle.Close();
        outPut.Clear();
    }

    public static void SectionHeaderMatch()
    {
        var form = Form.ActiveForm as Form1;
        if (form == null)
        {

        }

        else
        {
            StringBuilder outPut = new StringBuilder();
            outPut.Append(form.textBox2.Text);
            outPut.Append("\\out.txt");

            FileStream readFile = new FileStream(outPut.ToString(), FileMode.Open, FileAccess.Read);
            BinaryReader readerFile = new BinaryReader(readFile);

            //Sector 1
            readerFile.BaseStream.Position = 1073741824;
            byte[] sectorOne = new byte[Form1.blockSize];
            readerFile.Read(sectorOne, 0, Form1.blockSize);

            //Sector 2
            readerFile.BaseStream.Position = 1073741824 + Form1.blockSize;
            byte[] sectorTwo = new byte[Form1.blockSize];
            readerFile.Read(sectorTwo, 0, Form1.blockSize);

            readerFile.Close();
            readFile.Close();

            string sector1 = UtilityClass.ByteArrayToString(sectorOne);
            string sector2 = UtilityClass.ByteArrayToString(sectorTwo);

            if (String.Compare(sector1, sector2) == 0) //0 if they match
            {
                if (headerMatchRunCount == 0) //If this is section 1
                {
                    form.richTextBox3.AppendText("Section 1 headers match.");
                }

                else //Section 2
                {
                    form.richTextBox4.AppendText("Section 2 headers match.");
                }
            }

            else //Headers did not match
            {
                if (headerMatchRunCount == 0) //Section 1
                {
                    form.richTextBox3.AppendText("Section 1 headers match.");
                }

                else //Section 2
                {
                    form.richTextBox4.AppendText("Section 2 headers match.");
                }
            }
        }
    }

最佳答案

经过几个小时的折腾,我终于找到了为什么 activateForm 有时为空......

activateForm 采用您的“IDE”表单,而不仅仅是生成您的 C# 表单。

我用一个例子来解释: 第一个循环,我在没有任何中断代码的情况下启动我的应用程序,并在执行“activateForm()”之后放置一个中断代码并且它可以工作。

在同一个循环中第二次,我设置了断点,并且 activateForm() 不再起作用。 为什么 ? 因为“ActivateForm”不再是我的 C# 表单了... 但这是我的“ Visual Studio Code 页”。

我不知道它是否对你的情况有帮助,但对我的情况有帮助。

关于c# - Form.ActiveForm 有时有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17117372/

相关文章:

c# - math.round 不适用于 double

c# - 更高效地编辑 XML 代码注释的工具?

c# - 如何在连接时从 WebClient.DownloadDataAsync() 获取更多信息?

c# - 字形未显示在 datagridview 上

c# - 在 DataGridView 中将 AutoSizeMode 设置为 AllCells 时出现 NullReferenceException

c# - Windows 窗体可以在没有关闭按钮的情况下显示最小和最大按钮吗?

c# - 优先级默认值或其他函数? (重载)

c# - 无法安装windows服务

.net - 在 Windows 中注册一个事件

c# - 如何在 Enum C# 中设置字符串?