473,385 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Easy question about events

In order to make a function handle an even such as a click, do you just
have to name the function [object_name]_EVENT where EVENT is the event
that you are trying to handle with the function?

I'm creating a compact framework app, and originally started in VB, but
ended up with C#. In the VB world I think you can explicitly define it
like:

function X() handles item.click

or something like that. Thanks in advance for the help.

Mar 4 '06 #1
3 1116
Hi Jonathon,
the function that handles an event can have any name, the main thing is
that the method that handles an event needs to have the correct signature for
the event. In the C# world setting up an event handler is something like:

object.TheEvent += new TheEventHandler(FunctionToHandleEvent);

where TheEvent is the event you want to handle, TheEventHandler is the
delegate used to point to the method that handles the event and
FunctionToHandleEvent is the name of the method that you have defined
somewhere which gets called when the event is raised.

The default code generated by Visual Studio when handling a button click is:

this.button1.Click += new System.EventHandler(this.button1_Click);

private void button1_Click(object sender, EventArgs e)
{

}

but it could just as well be:
this.button1.Click += new System.EventHandler(this.DoSomething);

private void DoSomething(object sender, EventArgs e)
{

}

Hope that helps
Mark Dawson
http://www.markdawson.org

"Jonathon" wrote:
In order to make a function handle an even such as a click, do you just
have to name the function [object_name]_EVENT where EVENT is the event
that you are trying to handle with the function?

I'm creating a compact framework app, and originally started in VB, but
ended up with C#. In the VB world I think you can explicitly define it
like:

function X() handles item.click

or something like that. Thanks in advance for the help.

Mar 4 '06 #2
You can use the "+=" and "-=" operators. See the "Subscribing to Events"
section at the link below.
http://msdn2.microsoft.com/en-us/library/ms173168.aspx

--
Tim Wilson
..NET Compact Framework MVP

"Jonathon" <js****@gmail.com> wrote in message
news:11*********************@j33g2000cwa.googlegro ups.com...
In order to make a function handle an even such as a click, do you just
have to name the function [object_name]_EVENT where EVENT is the event
that you are trying to handle with the function?

I'm creating a compact framework app, and originally started in VB, but
ended up with C#. In the VB world I think you can explicitly define it
like:

function X() handles item.click

or something like that. Thanks in advance for the help.

Mar 4 '06 #3

"Jonathon" <js****@gmail.com> wrote in message
news:11*********************@j33g2000cwa.googlegro ups.com...
In order to make a function handle an even such as a click, do you just
have to name the function [object_name]_EVENT where EVENT is the event
that you are trying to handle with the function?

I'm creating a compact framework app, and originally started in VB, but
ended up with C#. In the VB world I think you can explicitly define it
like:

function X() handles item.click

or something like that. Thanks in advance for the help.


It is interesting to see the difference between C# and VB:

In VB it is easy to see what events a handler is for but harder to find out
what, if anything, handles a given event whereas for C# the reverse is true.

The C# approach is closer to the underlying MSIL because it is procedural
rather than declarative.

Also in C# you now have anonymous delegates and I don't see how they can
translate well into VB.
Mar 5 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Steve | last post by:
I've run in to a problem with a query I'm trying to write. I have attached a sample SQL script at the end of this post to show an overview of what I'm working with. I want to be able to use...
2
by: jm | last post by:
I got the code below from the .NET SDK. I am still new to C# and I don't understand how the code works. And by that I mean I don't understand how the program knows to execute it. I can find no...
4
by: Markus Stoeger | last post by:
Hello! I'm using the FileSystemWatcher to get notified when new files are created in a directory. Once a file appears in the directory I want to read it's contents into memory and do some...
3
by: Rob Meade | last post by:
Hi all, I have a login page which has username and password fields, a login button, and 2 validation controls (one for each field) - currently I have controls to display to the summary if the...
6
by: Christian Blackburn | last post by:
Hi Gang, I can't seem to figure out how to create an object array in VB.NET 2003. In VB6 you would just select an object copy and paste it and that was that. I don't know why in the heck they...
1
by: hellohez | last post by:
We are currently looking into triggering printing of an HTML file to a designated printer in such a way that the print dialog box is not viewed. It is possible to use the print method of a default...
4
by: Sin Jeong-hun | last post by:
Hello. Event handlers for events from an object of another thread than the UI thread cannot access UI components, as you know. If the object has many events this precedure is painstaking. Plus...
20
by: raylopez99 | last post by:
Inspired by Chapter 8 of Albahari's excellent C#3.0 in a Nutshell (this book is amazing, you must get it if you have to buy but one C# book) as well as Appendix A of Jon Skeet's book, I am going...
1
by: Philth | last post by:
Hello folks, Hopefully this will be an easy one for most of you guys. I don't delve into PHP much so I'd appreciate some help. The aim of the script below is to display a collection of rows...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.