C#: Need one of my classes to trigger an event in another class to update a text box(C#:需要我的一个类来触发另一个类中的事件以更新文本框)
问题描述
尽管我已经编程了一段时间,但 C# 和事件的总 n00b.
Total n00b to C# and events although I have been programming for a while.
我有一个包含文本框的类.此类创建从串行端口接收帧的通信管理器类的实例.我有这一切工作正常.
I have a class containing a text box. This class creates an instance of a communication manager class that is receiving frames from the Serial Port. I have this all working fine.
每次接收到帧并提取其数据时,我都希望在我的类中使用文本框运行一个方法,以便将此帧数据附加到文本框.
Every time a frame is received and its data extracted, I want a method to run in my class with the text box in order to append this frame data to the text box.
因此,无需发布我的所有代码,我就有了我的表单类...
So, without posting all of my code I have my form class...
public partial class Form1 : Form
{
    CommManager comm;
    public Form1()
    {
        InitializeComponent();
        comm = new CommManager();
    }
    private void updateTextBox()
    {
        //get new values and update textbox
    }
    .
    .
    .
我有我的 CommManager 课程
and I have my CommManager class
class CommManager 
{
     //here we manage the comms, recieve the data and parse the frame
}
所以...本质上,当我解析该框架时,我需要运行表单类中的 updateTextBox 方法.我猜这在事件中是可能的,但我似乎无法让它发挥作用.
SO... essentially, when I parse that frame, I need the updateTextBox method from the form class to run. I'm guessing this is possible with events but I can't seem to get it to work.
在创建 CommManager 的实例后,我尝试在表单类中添加一个事件处理程序,如下所示...
I tried adding an event handler in the form class after creating the instance of CommManager as below...
 comm = new CommManager();
 comm.framePopulated += new EventHandler(updateTextBox);
...但是我一定做错了,因为编译器不喜欢它...
...but I must be doing this wrong as the compiler doesn't like it...
有什么想法吗?!
推荐答案
你的代码应该是这样的:
Your code should look something like:
public class CommManager()
{
    delegate void FramePopulatedHandler(object sender, EventArgs e);
    public event FramePopulatedHandler FramePopulated;
    public void MethodThatPopulatesTheFrame()
    {
        FramePopulated();
    }
    // The rest of your code here.
}
public partial class Form1 : Form      
{      
    CommManager comm;      
    public Form1()      
    {      
        InitializeComponent();      
        comm = new CommManager();      
        comm.FramePopulated += comm_FramePopulatedHander;
    }      
    private void updateTextBox()      
    {      
        //get new values and update textbox      
    }
    private void comm_FramePopulatedHandler(object sender, EventArgs e)
    {
        updateTextBox();
    }
}
这里是评论中提到的 .NET 事件命名指南的链接:
And here's a link to the .NET Event Naming Guidelines mentioned in the comments:
MSDN - 事件命名指南
这篇关于C#:需要我的一个类来触发另一个类中的事件以更新文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C#:需要我的一个类来触发另一个类中的事件以更新文本框
				
        
 
            
        - Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
 - 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
 - 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
 - C# 中多线程网络服务器的模式 2022-01-01
 - C#MongoDB使用Builders查找派生对象 2022-09-04
 - 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
 - 输入按键事件处理程序 2022-01-01
 - WebMatrix WebSecurity PasswordSalt 2022-01-01
 - 如何用自己压缩一个 IEnumerable 2022-01-01
 - MoreLinq maxBy vs LINQ max + where 2022-01-01
 
						
						
						
						
						
				
				
				
				