5

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?!

Matt
  • 367
  • 1
  • 5
  • 20
  • Also could you post the Comm manager event handling property/functions for framePopulated – Michal Ciechan Apr 01 '10 at 10:11
  • Could you paste the delegate definition of "framePopulated"? – Claudio Redi Apr 01 '10 at 10:12
  • 1
    As a helper Visual Studio will automatically create a method with the correct signature to consume the event when it is raised. A common approach is usaully to have two arguments, the first being the sender of the event and the second an implementation of EventArgs to carry with it descriptive data relating to the event. This is usually triggered with the += [TAB] [TAB] – REA_ANDREW Apr 01 '10 at 10:13
  • please paste the code of your delegate and I'm sure we'll figure it out – Claudio Redi Apr 01 '10 at 10:16
  • @LndCobra ok I must be completely missing something here because I don't have any more code pertaining to the event. From your post it would seem I need Comm manager event handling functions... I don't have this... from these – Matt Apr 01 '10 at 10:17
  • @Claudio again - I must be missing some brain functionality as I have no delegate code and in fact, do not know what a delegate is in this sense... perhaps I should get to reading lol – Matt Apr 01 '10 at 10:18
  • "framePopulated" is a property of "CommManager". Could you paste the definition? – Claudio Redi Apr 01 '10 at 10:19

4 Answers4

10

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

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • From MSDN Event Naming Guidelines: "Do not use a prefix or suffix on the event declaration on the type. For example, use Close instead of OnClose." – João Angelo Apr 01 '10 at 10:18
  • Sorry for another question but whenever I populate my frame do I just make a call to FramePopulated then to trigger the event? – Matt Apr 01 '10 at 10:33
2

Here you have "The Simplest C# Events Example Imaginable".

thelost
  • 6,638
  • 4
  • 28
  • 44
1

    public partial class Form1: Form
    {
        CommManager comm;

        public Form1()
        {
            InitializeComponent();
            comm = new CommManager();
            comm.OnFramePopulated += new EventHandler(updateTextBox);
        }

        private void updateTextBox(object sender, EventArgs ea)
        {
            //update Textbox
        }
    }

    class CommManager
    {
        public EventHandler OnFramePopulated;

        public void PopulateFrame()
        {
            OnFramePopulated(this, null);
        }
    }
Asher
  • 1,016
  • 1
  • 6
  • 20
0

Yes - change the signature of updateTextBox to :

private void updateTextBox(object sender, Eventargs ea)

Although that might not be the best design. Things would look a lot neater if you wrote a proper event handler, and then called updateTextBox from there...

Martin Milan
  • 6,346
  • 2
  • 32
  • 44