winforms 在C#中写入格式文本框

z8dt9xmd  于 2022-11-25  发布在  C#
关注(0)|答案(1)|浏览(186)

新程序员来了。
我已经写了一个应用程序来过滤出密码保护的文件从一个大的集合。它是工作到目前为止...
我的问题是,我试图把结果路径+它们发生了什么写进一个文本框。也许我在这里错过了一些完全显而易见的东西,但我得到了错误:
CS0120非静态字段、方法或属性MainWindow.SearchRunOutputBox需要对象引用
当试图在我编写的updater方法中赋值时。
我很困惑,因为我以为一个格式文本框,已经存在于表单上,将是它自己的对象?再次,也许我只是错过了一些明显的这里...
代码如下:

using Microsoft.VisualStudio.Services.CircuitBreaker;
using Spire.Pdf.Exporting.XPS.Schema;
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace Find_Move_PWP_PDF_s
{
    public partial class MainWindow : Form
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        public static void OutputBoxUpdater(string message, Color color)
        {  
            SearchRunOutputBox.SuspendLayout();
            SearchRunOutputBox.SelectionColor = color;
            SearchRunOutputBox.AppendText(message);
            SearchRunOutputBox.AppendText(Environment.NewLine);
            SearchRunOutputBox.ScrollToCaret();
            SearchRunOutputBox.ResumeLayout();
        }

MainWindow启动时调用InitializeComonent()时,是否未创建文本框对象?
包含以下designer.cs:

partial class MainWindow
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.Windows.Forms.Label DirectoryPathLabel;
            System.Windows.Forms.Label CreateSubFoldersLabel;
            this.DirectoryPathInput = new System.Windows.Forms.TextBox();
            this.SearchStartButton = new System.Windows.Forms.Button();
            this.SearchProgressBar = new System.Windows.Forms.ProgressBar();
            this.MovePDFsButton = new System.Windows.Forms.Button();
            this.MovePDFsRunOutputBox = new System.Windows.Forms.RichTextBox();
            this.MovesProgressBar = new System.Windows.Forms.ProgressBar();
            this.SearchRunOutputBox = new System.Windows.Forms.RichTextBox();
            DirectoryPathLabel = new System.Windows.Forms.Label();
            CreateSubFoldersLabel = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // DirectoryPathLabel
            // 
            DirectoryPathLabel.AccessibleDescription = "DirectoryPathLabel";
            DirectoryPathLabel.AutoSize = true;
            DirectoryPathLabel.BackColor = System.Drawing.SystemColors.Window;
            DirectoryPathLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
            DirectoryPathLabel.Location = new System.Drawing.Point(14, 23);
            DirectoryPathLabel.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            DirectoryPathLabel.Name = "DirectoryPathLabel";
            DirectoryPathLabel.Size = new System.Drawing.Size(370, 25);
            DirectoryPathLabel.TabIndex = 0;
            DirectoryPathLabel.Text = "Please enter the desired search directory.";
            // 
            // CreateSubFoldersLabel
            // 
            CreateSubFoldersLabel.AccessibleDescription = "CreateSubFoldersLabel";
            CreateSubFoldersLabel.AccessibleName = "CreateSubFoldersLabel";
            CreateSubFoldersLabel.AutoSize = true;
            CreateSubFoldersLabel.BackColor = System.Drawing.SystemColors.Window;
            CreateSubFoldersLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
            CreateSubFoldersLabel.Location = new System.Drawing.Point(14, 453);
            CreateSubFoldersLabel.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            CreateSubFoldersLabel.Name = "CreateSubFoldersLabel";
            CreateSubFoldersLabel.Size = new System.Drawing.Size(725, 25);
            CreateSubFoldersLabel.TabIndex = 5;
            CreateSubFoldersLabel.Text = "Click here to move the password protected PDFs into subfolders labelled \"Locked\"." +
    "";
            // 
            // DirectoryPathInput
            // 
            this.DirectoryPathInput.AccessibleDescription = "DirectoryPathInput";
            this.DirectoryPathInput.Location = new System.Drawing.Point(424, 30);
            this.DirectoryPathInput.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
            this.DirectoryPathInput.Name = "DirectoryPathInput";
            this.DirectoryPathInput.Size = new System.Drawing.Size(822, 23);
            this.DirectoryPathInput.TabIndex = 1;
            // 
            // SearchStartButton
            // 
            this.SearchStartButton.AccessibleDescription = "SearchStartButton";
            this.SearchStartButton.Location = new System.Drawing.Point(1278, 23);
            this.SearchStartButton.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
            this.SearchStartButton.Name = "SearchStartButton";
            this.SearchStartButton.Size = new System.Drawing.Size(248, 30);
            this.SearchStartButton.TabIndex = 2;
            this.SearchStartButton.Text = "Start Search";
            this.SearchStartButton.UseVisualStyleBackColor = true;
            this.SearchStartButton.Click += new System.EventHandler(this.SearchStartButton_Click);
            // 
            // SearchProgressBar
            // 
            this.SearchProgressBar.AccessibleDescription = "SearchProgressBar";
            this.SearchProgressBar.AccessibleName = "SearchProgressBar";
            this.SearchProgressBar.Location = new System.Drawing.Point(14, 257);
            this.SearchProgressBar.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
            this.SearchProgressBar.Name = "SearchProgressBar";
            this.SearchProgressBar.Size = new System.Drawing.Size(1512, 15);
            this.SearchProgressBar.TabIndex = 3;
            // 
            // MovePDFsButton
            // 
            this.MovePDFsButton.AccessibleDescription = "MovePDFsButton";
            this.MovePDFsButton.AccessibleName = "MovePDFsButton";
            this.MovePDFsButton.Location = new System.Drawing.Point(767, 453);
            this.MovePDFsButton.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
            this.MovePDFsButton.Name = "MovePDFsButton";
            this.MovePDFsButton.Size = new System.Drawing.Size(237, 25);
            this.MovePDFsButton.TabIndex = 6;
            this.MovePDFsButton.Text = "Move PDFs";
            this.MovePDFsButton.UseVisualStyleBackColor = true;
            // 
            // MovePDFsRunOutputBox
            // 
            this.MovePDFsRunOutputBox.AccessibleDescription = "MovePDFsRunOutputBox";
            this.MovePDFsRunOutputBox.AccessibleName = "MovePDFsRunOutputBox";
            this.MovePDFsRunOutputBox.BackColor = System.Drawing.Color.WhiteSmoke;
            this.MovePDFsRunOutputBox.Location = new System.Drawing.Point(14, 489);
            this.MovePDFsRunOutputBox.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
            this.MovePDFsRunOutputBox.Name = "MovePDFsRunOutputBox";
            this.MovePDFsRunOutputBox.ReadOnly = true;
            this.MovePDFsRunOutputBox.Size = new System.Drawing.Size(1511, 190);
            this.MovePDFsRunOutputBox.TabIndex = 7;
            this.MovePDFsRunOutputBox.Text = "";
            // 
            // MovesProgressBar
            // 
            this.MovesProgressBar.AccessibleDescription = "MovesProgressBar";
            this.MovesProgressBar.AccessibleName = "MovesProgressBar";
            this.MovesProgressBar.Location = new System.Drawing.Point(14, 687);
            this.MovesProgressBar.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
            this.MovesProgressBar.Name = "MovesProgressBar";
            this.MovesProgressBar.Size = new System.Drawing.Size(1512, 15);
            this.MovesProgressBar.TabIndex = 8;
            // 
            // SearchRunOutputBox
            // 
            this.SearchRunOutputBox.AccessibleDescription = "SearchRunOutputBox";
            this.SearchRunOutputBox.AccessibleName = "SearchRunOutputBox";
            this.SearchRunOutputBox.BackColor = System.Drawing.Color.WhiteSmoke;
            this.SearchRunOutputBox.CausesValidation = false;
            this.SearchRunOutputBox.ImeMode = System.Windows.Forms.ImeMode.NoControl;
            this.SearchRunOutputBox.Location = new System.Drawing.Point(14, 60);
            this.SearchRunOutputBox.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
            this.SearchRunOutputBox.Name = "SearchRunOutputBox";
            this.SearchRunOutputBox.ReadOnly = true;
            this.SearchRunOutputBox.Size = new System.Drawing.Size(1511, 190);
            this.SearchRunOutputBox.TabIndex = 4;
            this.SearchRunOutputBox.Text = "";
            // 
            // MainWindow
            // 
            this.AccessibleDescription = "MainWindow";
            this.AccessibleName = "MainWindow";
            this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.ClientSize = new System.Drawing.Size(1540, 726);
            this.Controls.Add(this.MovesProgressBar);
            this.Controls.Add(this.MovePDFsRunOutputBox);
            this.Controls.Add(this.MovePDFsButton);
            this.Controls.Add(CreateSubFoldersLabel);
            this.Controls.Add(this.SearchRunOutputBox);
            this.Controls.Add(this.SearchProgressBar);
            this.Controls.Add(this.SearchStartButton);
            this.Controls.Add(this.DirectoryPathInput);
            this.Controls.Add(DirectoryPathLabel);
            this.DoubleBuffered = true;
            this.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
            this.Name = "MainWindow";
            this.Text = "Find and Move Password Protected PDF\'s";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox DirectoryPathInput;
        private System.Windows.Forms.Button SearchStartButton;
        private System.Windows.Forms.ProgressBar SearchProgressBar;
        private System.Windows.Forms.Button MovePDFsButton;
        private System.Windows.Forms.ProgressBar MovesProgressBar;
        private System.Windows.Forms.RichTextBox MovePDFsRunOutputBox;
        public System.Windows.Forms.RichTextBox SearchRunOutputBox;
    }
}

我想知道是否有一种方法可以使文本框成为静态项?或者我应该打赌使用其他东西来显示日志?
我试过在OutBoxUpdater()中初始化一个新的文本框,它摆脱了错误,但似乎没有做任何事情。或者它做了一些事情,但不是我想要的,哈哈。

public static void OutputBoxUpdater(string message, Color color)
{
    RichTextBox SearchRunOutputBox = new();

    SearchRunOutputBox.SuspendLayout();
    SearchRunOutputBox.SelectionColor = color;
    SearchRunOutputBox.AppendText(message);
    SearchRunOutputBox.AppendText(Environment.NewLine);
    SearchRunOutputBox.ScrollToCaret();
    SearchRunOutputBox.ResumeLayout();
}

编辑:

根据Steve的第一条评论,很抱歉我没有提到我以前尝试过这个,当我从方法中删除static时,它会将错误推到我试图调用方法的位置。

using Spire.Pdf;
using System;
using System.Drawing;
using System.IO;

namespace Find_Move_PWP_PDF_s
{
    public class PDFOpenToCheck
    {
        /// <summary>
        /// Takes in a file path and attempts to open the file, throwing an error if it cannot, then saying it's password protected, and logging the filepath.
        /// Also checks that the file is not empty, and if it is, logs the filepath to a different log.
        /// </summary>
        /// <param name="filePath">Current file path from stack.</param>
        public void TryToOpenPDF(string filePath)
        {
            PdfDocument pdf = new();
            FileInfo fi = new(filePath);

            try
            {
                pdf.LoadFromFile(filePath);
                MainWindow.OutputBoxUpdater(filePath + " successfully opened!", Color.Black);
            }
            catch (Exception)
            {
                //Not empty and is a PDF
                if (fi.Length > 0 && filePath.EndsWith(".pdf"))
                {
                    MainWindow.OutputBoxUpdater(filePath + " is password protected!", Color.Red);
                    LogWriters.WritePWPPDFLog(filePath);
                }
                //Is an empty file
                else if (fi.Length <= 0)
                {
                    MainWindow.OutputBoxUpdater(filePath + " is an empty file!", Color.Blue);
                    LogWriters.WriteEmptyFileLog(filePath);
                }
                //Is not empty and is not a PDF
                else
                {
                    MainWindow.OutputBoxUpdater(filePath + " is not a PDF!", Color.Green);
                }
            }
        }
    }
 }

编辑:显示从何处调用TryToOpenPDF()。

namespace Find_Move_PWP_PDF_s
{
    public static class TraverseDirectoryTree
    {
        /// <summary>
        /// Recursive method to iterate through the directory structure below the provided start point.
        /// In this application, will also perform PDF checking with Spire as it iterates (Line 74).
        /// Uses stack based iteration.
        /// </summary>
        /// <param name="root">Starting parent directory.</param>
        public static void TraverseTree(string root)
        {
            // Data structure to hold names of subfolders to be examined for files
            // "Stack<T>" works by "first in last out" and could take a long time to build the initial stack before iteration begins
            Stack<string> dirs = new();

            if (!System.IO.Directory.Exists(root))
            {
                ArgumentException argumentException = new();
                throw argumentException;
            }
            dirs.Push(root);

            while (dirs.Count > 0)
            {
                string currentDir = dirs.Pop();
                string[] subDirs;
                try
                {
                    subDirs = System.IO.Directory.GetDirectories(currentDir);
                }
                // "Unauthorized Access Exception" will be thrown if we do not have discovery permission on a folder or file. 
                catch (UnauthorizedAccessException e)
                {
                    System.Diagnostics.Trace.WriteLine(e.Message);
                    //Console.WriteLine(e.Message);
                    continue;
                }
                //"Directory Not Found Exception" will be thrown if currentDir has been deleted by another application or thread after our call to Directory
                catch (System.IO.DirectoryNotFoundException e)
                {
                    System.Diagnostics.Trace.WriteLine(e.Message);
                    //Console.WriteLine(e.Message);
                    continue;
                }

                string[]? files;
                try
                {
                    files = System.IO.Directory.GetFiles(currentDir);
                }

                catch (UnauthorizedAccessException e)
                {
                    System.Diagnostics.Trace.WriteLine(e.Message);
                    //Console.WriteLine(e.Message);
                    continue;
                }

                catch (System.IO.DirectoryNotFoundException e)
                {
                    System.Diagnostics.Trace.WriteLine(e.Message);
                    //Console.WriteLine(e.Message);
                    continue;
                }
                // Perform the required action on each file here
                foreach (string file in files)
                {
                    try
                    {
                        PDFOpenToCheck.TryToOpenPDF(file);
                    }
                    catch (System.IO.FileNotFoundException e)
                    {
                        // If file was deleted by a separate application or thread since the call to "TraverseTree()", then just continue.
                        System.Diagnostics.Trace.WriteLine(e.Message);
                        //Console.WriteLine(e.Message);
                        continue;
                    }
                    catch (Exception e)
                    {
                        System.Diagnostics.Trace.WriteLine(e.Message);
                        //Console.WriteLine(e.Message);
                    }
                }

                // Push the subdirectories onto the stack for traversal
                foreach (string str in subDirs)
                    dirs.Push(str);
            }
        }
    }
}
e5nszbig

e5nszbig1#

OutputBoxUpdater()更改为NOTstatic:

public void OutputBoxUpdater(string message, Color color)
{  
    SearchRunOutputBox.SuspendLayout();
    SearchRunOutputBox.SelectionColor = color;
    SearchRunOutputBox.AppendText(message);
    SearchRunOutputBox.AppendText(Environment.NewLine);
    SearchRunOutputBox.ScrollToCaret();
    SearchRunOutputBox.ResumeLayout();
}

然后更改TryToOpenPDF()的签名以将MainForm作为参数接收,以便可以使用它:

public void TryToOpenPDF(string filePath, MainForm mf) {

    // ... code ...

    mf.OutputBoxUpdater(filePath + " successfully opened!", Color.Black);

    // ... code ...
}

请注意,我使用的是参数名mf,而不是窗体本身的名称。
MainForm中的某个地方,您可能调用了TryToOpenPDF(),但尚未显示。
它看起来像这样:

// ... within MainForm code ...

PDFOpenToCheck pdf = new PDFOpenToCheck();
pdf.TryToOpenPDF("some file path", this);

请注意,我们正在通过this传递MainForm的当前示例,即已经可见的示例!
---编辑---
因此,现在您必须做出选择,首先将引用传递给TraverseTree(),然后TraverseTree()再将其传递给TryToOpenPDF(),或者使用以下命令进行CHEAT:

MainForm mf = Application.OpenForms.Cast<MainForm>().Where(x => (x is MainForm)).FirstOrDefault();
if (mf != null)
{
    // ... do something with "mf" ...
}

请注意,这是假设只有一个MainForm示例。

相关问题