Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Need one of my classes to trigger an event in another class to update a text box

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
    }
    .
    .
    .

and I have my CommManager class

class CommManager 
{
     //here we manage the comms, recieve the data and parse the frame
}

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.

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...

Any ideas?!

like image 384
Matt Avatar asked Apr 01 '10 10:04

Matt


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


1 Answers

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();
    }
}

And here's a link to the .NET Event Naming Guidelines mentioned in the comments:

MSDN - Event Naming Guidelines

like image 167
Justin Niessner Avatar answered Oct 22 '22 01:10

Justin Niessner