• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Basic Event Creation C# ?

elkinm

Platinum Member
Hi I am trying to create some events and event handlers in my code.
There are plenty sites listing events, but I can't get a handle off them as I can't find one that has a complete A-Z list of everything that is needed and has it work.

To put it simply I have a forms application in VS C#, I have a text box where I enter text, text box to store text, a button that copies the entered text and a 3rd text box which should be event triggered to hold the new text.

I enter text in the first text box and press the button which runs the VS native event to copy the text to the second text box then I want an event triggered that text box 2 has changed that would update text box 3. This is just a basic example I am trying to do to learn events.

I also don’t know how to use EventArgs. I would like to send the old value and the new value as part of the event so the handler checks if the value has changed and only updates the text box if it has.

Thanks for your help.
 
Are you specifically looking to learn about events or are you trying to implement something and concluded using events was the best solution? I think you are might be mixing some terms up.

Also, MSDN has a nice list of C# events.

Finally, you can use the "CLICK" event of the first button that simply copies text, for example, you won't even need to reference eventargs.

Code:
private void Copy_Click(object sender, EventArgs e)
{
  // copy textbox1 contents to textbox2
  TextBox2.Text = TextBox1.Text;
}

Then you can reference the TextChanged Event for the third textbox, still without referencing anything inside eventargs.
Code:
private void TextBox2_TextChanged(object sender, EventArgs e)
{
  // copy to textbox3 only if different from textbox2
  if (TextBox3.Text != TextBox2.Text)
    TextBox3.Text = TextBox2.Text;
}

Visual Studios will automatically attach the handler to your controls but if not, put the following in your constructor or load
Code:
TextBox2.TextChanged += new EventHandler(TextBox2_TextChanged);
 
for buttons on a form in the design viewer, you can just double click the button to make a click event.
 
Use intellisense to find out the members of each class. Its a good way to see what events it supports. Same thing with EventArgs - look at what its data members are.

In this case, your Event Args wont have a new value and an old value data member. However, if you can write your own EventArgs class, and use that to send the value. You would then need to create your own event to use this new event args, as well as a new event handler. The problem you would have is that by the time your new event is fired, the old text would likely have changed. Way to get round that of course is to have a string that stores the actual value, behind the scenes, and the textbox text changed event simply updates that.

Hope that all makes sense?
 
Create a new event arguments class...

Code:
public class MyEventArgs : EventArgs
{
// your properties here
}

Create a delegate type using the generic event type...

Code:
// ... inside the class that "publishes" the event
public event EventHandler<MyEventArgs> MyEvent;

Create a restricted access method within the same class to "raise" the event when something happens...

Code:
private void OnSomethingHappened(MyEventArgs myArgs)
{
    EventHandler<MyEventArgs> ev = MyEvent;
    if (null != ev)
        ev(myArgs);
}

Now clients that hold an instance of your class can subscribe to the events...

Code:
void SomethingHappened( MyEventArgs args)
{
    // something happened so do something
}

void SomeOtherMethod()
{
    YourClass yc = new YourClass();
    yc.MyEvent += new EventHandler<MyEventArgs>(SomethingHappened);
}

Lastly, when the class that raises the event detects that the event needs to be raised, it does so like this ...

Code:
MyEventArgs ea = new MyEventArgs(whateverGoesHere);
OnSomethingHappened(ea);

That's the basic mechanism. There are some other available syntactic shortcuts but I think this format makes it clearer what's going on. So in summary: to pass your own data around with events you need to create your own event arguments type and define your own events. Where this typically comes into play is when you are designing a control that is a composite of a number of system controls. The "outer control" handles the events of its child controls, and then raises its own events to its clients when appropriate.
 
I've seen people assign an empty event handler by default to the private event object so you don't need the null check in your private method to raise the event.

I use the technique that Mark has described in my code though.
 
Back
Top