C# 从另一个类中的事件更新表单控件
我是一名C#初学者,这也是我在这里的第一篇帖子,所以请对我好一点:) 嗯,我正在尝试编写一个小应用程序,它只在文件发生更改时读取文件,并在Windows窗体中的richtextbox控件中更新新内容 这就是我的代码:C# 从另一个类中的事件更新表单控件,c#,multithreading,event-handling,C#,Multithreading,Event Handling,我是一名C#初学者,这也是我在这里的第一篇帖子,所以请对我好一点:) 嗯,我正在尝试编写一个小应用程序,它只在文件发生更改时读取文件,并在Windows窗体中的richtextbox控件中更新新内容 这就是我的代码: public partial class Form1 : Form { public Form1() { InitializeComponent(); s
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
string path = @"C:\MyPath\";
Filecheck NewFileChecker = new Filecheck();
NewFileChecker.WatchingFile(path, "myFile.txt");
}
这是我的班级档案
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);
}
}
}
因此,我想更新一个Windows窗体控件,在这里我使用MessageBox方法。有人知道我该怎么做?因为当我试图这样调用时:
Form1.richTextBoxName.Invoke(new MethodInvoker(delegate
{
Form1.richTextBoxName.Text(line);
}));
我得到一条消息:“CS0120:非静态字段、方法或属性需要对象引用”
有人知道我该怎么解决这个问题吗?
谢谢
你的想法有很多解决方案,但我可以帮你一个例子: 使用
委托
:
步骤1:使用参数is 01string
,返回类型
isvoid
,名称isUpdateData
:
public delegate void UpdateData(string data);
步骤2:在文件检查类(OnUpdateData
)中声明事件,并创建委托
:
public event UpdateData OnUpdateData;
步骤3:在需要时随时引发事件:
this.OnUpdateData?.Invoke(line);
步骤4:在主功能中,通过以下方式设置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做得很好。但是我做了一些修改,如果它能帮助其他人的话。。非常感谢您的投入
在主要类别中:
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;
}));
}
最后在另一个类的另一个文件中
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);
}
}
}
再次感谢您的回复。我想您是在帮助发帖人理解类实例(使用new Form1()创建)与类本身之间的区别。我认为你的回答会更有帮助,如果你说:你试图使用类,就好像它是类的一个实例一样。使用new创建实例,然后调用实例的方法。Form1.foo仅适用于Form类的静态成员。任何普通成员只能从表单实例访问。Form1
是一个类名。您需要对控件之类的对象使用实例对象引用。从开始您的研究,但几乎总是简单地添加一个方法或属性来向表单传递信息,让表单处理自己的内部控制,这是我的解决方案。如果你把它应用到你真正的项目中,也许你必须使用眼睛,这就是我的想法。但我做了一些修改,让它在我的项目中工作。。。非常感谢您的输入。您只能使用:NewFileChecker.OnUpdateData+=UpdaterBoxJournal代码>