从另一个 class 中的事件更新表单控件

Updating Form Controls from Events in another class

我是 C# 的初学者,这也是我的第一个 post 所以请对我好:)

好吧,我正在尝试编写一个小应用程序,这些应用程序仅在文件发生更改时才读取文件,并以 Windows 形式更新 richtextbox 控件中的新内容。

所以这是我的代码:

public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                string path = @"C:\MyPath\";
                Filecheck NewFileChecker = new Filecheck();
                NewFileChecker.WatchingFile(path, "myFile.txt");
            }

这是我的 Class FileCheck

class Filecheck
{
        public void WatchingFile (string FilePath, string Filter)
        {
            FileSystemWatcher fsw = new FileSystemWatcher();
            fsw.Path = FilePath;
            fsw.Filter = Filter;
            fsw.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size | NotifyFilters.LastWrite;
            fsw.Changed += OnFileChange;
            fsw.EnableRaisingEvents = true;
        }


        private void OnFileChange(object sender, FileSystemEventArgs e)
        {
            string line;
            try
            {
                using (FileStream file = new FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                using (StreamReader sr = new StreamReader(file, Encoding.Default))
                {
                    while ((line = sr.ReadLine()) != null)
                    {
                        MessageBox.Show(line);
                        // I WOULD LIKE TO UPDATE A FORM1 RichTextBox from here....
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

所以,我想从执行 MessageBox 方法的地方更新 Windows 表单控件。有人知道我该怎么做?因为当我尝试这样调用时:

Form1.richTextBoxName.Invoke(new MethodInvoker(delegate
{
Form1.richTextBoxName.Text(line);
}));

嗯,我收到这条消息:"CS0120: An object reference is required for the nonstatic field, method, or property"

有人知道我该如何解决? 谢谢

  1. Form1 class 在您尝试使用时需要是新的。
  2. 如果你想通过你的函数使用Form1,你可以将Form1设置为Static Type。

你的想法有很多解决方案,但我帮你一个案例:

Use delegate:

第 1 步:创建一个新委托,参数为 01 stringreturn typevoid,名称为 UpdateData:

public delegate void UpdateData(string data);

步骤 2: 在 Filecheck class (OnUpdateData) 中声明一个 eventdelegate创建:

public event UpdateData OnUpdateData;

第 3 步:随时发起事件:

this.OnUpdateData?.Invoke(line);

第 4 步:在您的 main 函数中,通过以下方式设置 OnUpdateData

Filecheck NewFileChecker = new Filecheck();
NewFileChecker.OnUpdateData += (d => MessageBox.Show(d));

完整代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public delegate void UpdateData(string data);
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            string path = @"C:\MyPath\";
            Filecheck NewFileChecker = new Filecheck();
            NewFileChecker.OnUpdateData += (d => MessageBox.Show(d));
            NewFileChecker.WatchingFile(path, "myFile.txt");
        }
    }
    class Filecheck
    {
        public event UpdateData OnUpdateData;
        public void WatchingFile(string FilePath, string Filter)
        {
            FileSystemWatcher fsw = new FileSystemWatcher();
            fsw.Path = FilePath;
            fsw.Filter = Filter;
            fsw.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size | NotifyFilters.LastWrite;
            fsw.Changed += OnFileChange;
            fsw.EnableRaisingEvents = true;
        }


        private void OnFileChange(object sender, FileSystemEventArgs e)
        {
            string line;
            try
            {
                using (FileStream file = new FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                using (StreamReader sr = new StreamReader(file, Encoding.Default))
                {
                    while ((line = sr.ReadLine()) != null)
                    {
                        //MessageBox.Show(line);
                        // I WOULD LIKE TO UPDATE A FORM1 RichTextBox from here....
                        this.OnUpdateData?.Invoke(line);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

是的!这在我的项目中对我的想法 Nguyen Van Thanh 非常有效。但是我做了一些修改,如果它可以帮助别人的话,让它工作。非常感谢你的输入。

主要Class:

public Form1()
{
    string path = @"C:\MyPath\";            
    Filecheck NewFileChecker = new Filecheck();
    NewFileChecker.OnUpdateData += (d => UpdateRTBoxJournal(d));
    NewFileChecker.WatchingFile(path, "myFile.txt");
}

public void UpdateRTBoxJournal(string line)
{
    richTextBoxName.Invoke(new MethodInvoker(delegate
    {
    richTextBoxName.Text = line;
    }));
}

最后在另一个 class 另一个文件中...

public delegate void UpdateData(string data);
class Filecheck
{
    public event UpdateData OnUpdateData;
    public void WatchingFile (string FilePath, string Filter)
    {
        FileSystemWatcher fsw = new FileSystemWatcher();
        fsw.Path = FilePath;
        fsw.Filter = Filter;
        fsw.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size | NotifyFilters.LastWrite;
        fsw.Changed += OnFileChange;
        fsw.EnableRaisingEvents = true;
    }

    private void OnFileChange(object sender, FileSystemEventArgs e)
    {
        string line;
        try
        {
            using (FileStream file = new FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            using (StreamReader sr = new StreamReader(file, Encoding.Default))
            {
                while ((line = sr.ReadLine()) != null)
                {
                    this.OnUpdateData?.Invoke(line);
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Une erreur s'est produite." + ex.Message);
        }
    }
}

再次感谢您的回答。