OnClick event name

***************************************************
public class UserControl1 : System.Windows.Forms.UserControl {
    // ...

  public event EventHandler OnClick;

  // ...

  public void SomeFuncWhichFiresClickEvent() {
    // ...

    // fire event
    if (OnClick != null)
      OnClick(this, new EventArgs());
  }

  // ...

}public class UserControl1 : System.Windows.Forms.UserControl {

  // ...

  public event EventHandler OnClick;
   // ...

  public void SomeFuncWhichFiresClickEvent() {
    // ...

    // fire event
    if (OnClick != null)
      OnClick(this, new EventArgs());
  }
    // ...
 }
**************************************************

I am curious about the naming of EventHandlers.

Normally you see and event preceeded with the word "On" (OnClick, 
OnSelect,...)

In the above, OnClick could be anything, right?  It doesn't have to have the 
"On" in front of it.

Is this just a convention or is it required?

I would follow this, but I am curious if it has to be that way?

Thanks,

Tom 


0
tshad
3/6/2010 3:53:24 PM
dotnet.languages.csharp 1931 articles. 0 followers. Follow

6 Replies
1205 Views

Similar Articles

[PageSpeed] 4

tshad wrote:
> [...]
> I am curious about the naming of EventHandlers.
> 
> Normally you see and event preceeded with the word "On" (OnClick, 
> OnSelect,...)
> 
> In the above, OnClick could be anything, right?  It doesn't have to have the 
> "On" in front of it.
> 
> Is this just a convention or is it required?
> 
> I would follow this, but I am curious if it has to be that way?

Not only does it not have to be that way, I am skeptical that you've 
found any significant number of examples of events named that way.

Typically, the "OnXXX" name is used in the _method_ that raises the 
event, not the event itself.  In that convention, "XXX" is the event 
name, and "OnXXX" is the name of the method.

Even in that case, while that is a convention Microsoft follows and 
recommends, IMHO it's not a very good one.  I prefer "RaiseXXX" for the 
method name, as to me the word "on" implies something that happens as a 
consequence of the event being raised, not the other way around.

You are, of course, free to name your events, delegate types, and 
related methods whatever you want.  You don't even have to use the 
standard .NET method signature for your events (i.e. "(object sender, 
EventArgs e)")�any valid delegate type will do.  It's useful to follow 
the .NET convention IMHO when practical, but some events have no need 
for that structure and it's fine to do it some other way if so.

Pete
0
Peter
3/6/2010 5:06:44 PM
Hi!

"tshad" <tfs@dslextreme.com> wrote in message 
news:#VSsmUUvKHA.732@TK2MSFTNGP06.phx.gbl...

> I am curious about the naming of EventHandlers.

> Normally you see and event preceeded with the word "On" (OnClick, 
> OnSelect,...)

> In the above, OnClick could be anything, right?  It doesn't have to 
> have the "On" in front of it.

> Is this just a convention or is it required?

> I would follow this, but I am curious if it has to be that way?

It is just a convention and you can use all names you want. But 
Microsoft was thinking, what could be a good guideline for a developer 
and wrote that down.
http://msdn.microsoft.com/en-us/library/ms229042.aspx

You can use it or simply ignore it. Normaly it is a good idea to simply 
read it and try to go that way, too. But if you see any reason, why you 
want to do something a little different: Feel free to do it! But knowing 
the guidelines is always a good idea when you want to give a library 
away to other developers. They will be used to the nameing standards of 
Microsoft.

But maybe you have some reasons to do it in some other way (e.g. the 
whole team is used in giving the type first so you simply use stuff like 
pszNiceName for a pointer to a zero terminated string).

So in your case, we can simply have a look at the Events of 
System.Windows.Forms.Button:
- The Events itself are called: Click, DoubleClick, KeyUp, KeyDown, .... 
So they directly give in their name, what is happening.
- Functions that consume the event are called OnClick, OnDoubleClick, 
..... So you see directly when you read the code: "On" a "Click" this is 
done ....
(So it is not the Event that is named with "On" as you did in your Code 
example if I have it correctly in mind now... It would be "public event 
EventHandler Click")

I hope that helped a little bit.

With kind regards,

Konrad 

0
Konrad
3/6/2010 5:26:17 PM
Konrad Neitzel wrote:
> [...]
> So in your case, we can simply have a look at the Events of 
> System.Windows.Forms.Button:
> - The Events itself are called: Click, DoubleClick, KeyUp, KeyDown, .... 
> So they directly give in their name, what is happening.
> - Functions that consume the event are called OnClick, OnDoubleClick, 
> ..... So you see directly when you read the code: "On" a "Click" this is 
> done ....

Minor clarification: the above isn't quite correct.  Using Visual Studio 
with Forms events, the "functions that consume the event" are generally 
named in the form "xxxN_yyy" where "xxxN" is the name of the field 
referencing the object with the event itself (e.g. "textBox1") and "yyy" 
is the name of the event (e.g. "TextChanged").

Those are the functions (methods) that _consume_ (i.e. subscribe to) the 
event.  They are named by the Designer, and of course you can write any 
method from scratch to subscribe to the event.

The methods that _raise_ the event are the ones that start with "On�". 
They can also be used to react to the raising of the event from within 
the class where the event exists, but they aren't consuming the event 
per se.

As an extreme example, in the sub-class you could modify the "On�" 
override in your own class to not raise the event at all (e.g. by not 
calling the base implementation); obviously if the event isn't raised, 
it can't be consumed.  :)

Pete
0
Peter
3/6/2010 5:57:50 PM
Hi Peter!

"Peter Duniho" <no.peted.spam@no.nwlink.spam.com> wrote in message 
news:eCakKaVvKHA.4752@TK2MSFTNGP04.phx.gbl...
> Konrad Neitzel wrote:
>> [...]
>> So in your case, we can simply have a look at the Events of 
>> System.Windows.Forms.Button:
>> - The Events itself are called: Click, DoubleClick, KeyUp, KeyDown, 
>> .... So they directly give in their name, what is happening.
>> - Functions that consume the event are called OnClick, OnDoubleClick, 
>> ..... So you see directly when you read the code: "On" a "Click" this 
>> is done ....

> Minor clarification: the above isn't quite correct.  Using Visual 
> Studio with Forms events, the "functions that consume the event" are 
> generally named in the form "xxxN_yyy" where "xxxN" is the name of the 
> field referencing the object with the event itself (e.g. "textBox1") 
> and "yyy" is the name of the event (e.g. "TextChanged").

Thank you for that clarification. You are right that VisualStudio gives 
it this name. And we refactor it always in our team to a On.... Syntax.

> The methods that _raise_ the event are the ones that start with "On�". 
> They can also be used to react to the raising of the event from within 
> the class where the event exists, but they aren't consuming the event 
> per se.

> As an extreme example, in the sub-class you could modify the "On�" 
> override in your own class to not raise the event at all (e.g. by not 
> calling the base implementation); obviously if the event isn't raised, 
> it can't be consumed.  :)

Right. Good that you pointed that out. The full design pattern in our 
team is:

Inside the Class that wants to raise an Event (e.g. a Click to make it 
easier), we have:
- the event Click
- the function OnClick (in which you find something like if (Click != 
null) Click(this, ....) to raise the event)
- Whenever we want to raise the Click event, we call OnClick.
The big advantage of this design is, that you can derive from this class 
and simply modify what is done on all these events.
(So there is no need to subscribe your own events.)

When we want to consume some event, then we again use the On...Click 
Naming. But of course we have to add, what click we want to consume: 
e.g. OnCancelClick.
(This has also the advantage, that you have more or less a command 
related function. The nameing of visual studio has a control inside, but 
you can have multiple controls that give the same command e.g. a menu 
item and a button.)

It is interesting, what details can be forgotten, when you try to write 
something down quickly. We are only a very small team and we never wrote 
such stuff down so far (It more or less lives when we do code reviews 
and refactor stuff if required!)

Peter: Is that more complete or did I get something wrong / forgot 
something important? (It is always great to get these clarifications and 
deeper insights of you. I always learn a lot from that because you 
directly point me to stuff where I lack wisdom or didn't state something 
correct! When reading a book or self-paced training kit, you get a nice 
view into something but some details are always missing. I just hope 
that I am not to anoying and that my english is not to bad :) )

So thank you for your time and answer.

With kind regards,

Konrad
 

0
Konrad
3/6/2010 8:01:31 PM
Konrad Neitzel wrote:
> [...]
> Inside the Class that wants to raise an Event (e.g. a Click to make it 
> easier), we have:
> - the event Click
> - the function OnClick (in which you find something like if (Click != 
> null) Click(this, ....) to raise the event)
> - Whenever we want to raise the Click event, we call OnClick.
> The big advantage of this design is, that you can derive from this class 
> and simply modify what is done on all these events.
> (So there is no need to subscribe your own events.)
> 
> When we want to consume some event, then we again use the On...Click 
> Naming. But of course we have to add, what click we want to consume: 
> e.g. OnCancelClick.
> [...]
> Peter: Is that more complete or did I get something wrong / forgot 
> something important?

In terms of naming conventions, it seems to me you've covered it, 
including sharing your own team's conventions (I agree, the 
Designer-provided names are poor, implying a connection to a specific 
instance of an object that may or may not exist).  It's always helpful 
to see what others have chosen as their convention when the language 
doesn't provide a specific requirement.

I will point out that your abbreviated description of what you'd find in 
the event-owning class's "On�" method is of a non-thread-safe 
implementation.  But of course, that's not really a problem with your 
post, since we aren't really discussing the specifics of the 
implementation for raising an event.  So my comment is really just that 
I tend to take any opportunity to make that point, rather than any sort 
of criticism of your post.  :)

Pete
0
Peter
3/6/2010 10:34:38 PM
Hi Peter!

"Peter Duniho" <no.peted.spam@no.nwlink.spam.com> wrote in message 
news:OZ7P20XvKHA.3536@TK2MSFTNGP06.phx.gbl...

> I will point out that your abbreviated description of what you'd find 
> in the event-owning class's "On�" method is of a non-thread-safe 
> implementation.  But of course, that's not really a problem with your 
> post, since we aren't really discussing the specifics of the 
> implementation for raising an event.  So my comment is really just 
> that I tend to take any opportunity to make that point, rather than 
> any sort of criticism of your post.  :)

Criticism must not be negative. It is better to learn something in here 
than learning through a bug that went through all tests to the customer 
:)

Thank you for all your input / clarification.

With kind regards,

Konrad 

0
Konrad
3/6/2010 11:09:50 PM
Reply:

Similar Artilces:

Outlook event reminders
Hello, I have a siuation where one of our users creates a reoccuring event and when the reminder comes up before the event the user will either dismiss or snooze the event. The event will initally dissappear but within a minute the reminder is back. We have deleted all of the users old and current events within the calendar and still have the same results. I have tried running a repair on the outlook client also, with no result. We are running Exchange 2003 and outlook 98 for the client. This is the only user that is having this problem. Has anyone had this same issue or know of a possible fix...

Naming Cell Range
Hi all, When naming a cell range - in the name what CANNOT be used...? Can I name a range "Mech-33Data" ? Without the "" of course..... Regards, DP "David Penney" <dpenney@no-spam.iinet.net.au> wrote in message news:3f1cfb9e$0$23595$5a62ac22@freenews.iinet.net.au... > Hi all, > > When naming a cell range - in the name what CANNOT be used...? > > Can I name a range "Mech-33Data" ? > Without the "" of course..... > > Regards, > > DP > > I searched Help for "naming cells" and got to this:...

Event id 5891
Got this event id 300 times a day. Anyone know what it is? An error occurred : <description>An unexpected error occurred.</description><details>Failed to open registry key HKLM\software\microsoft\mscrm\platformurl Error : 2</details><file>d:\crm\build\3297\src\platform\communications\exchange\service\exchangequeue.cpp</file><line>230</line> ...

Input parameter in OnOpen event
Hello All I need an OnOpen event for a form that will carry out one of 5 actions - depending on a selection made by the user. i.e. The user should first be presented with a messagebox along the lines of "Enter X to do action1, or enter Y to do action2 or enter Z to do action3" Then if the user enters "X" the OnOpen event must do action1 (which would be to set the value of txtName to "Bricks and bats" and then open rpt1) if the user enters "Y" the OnOpen event must do action2 (which would be to set the value of txtName to "Trains, Boats and Rock...

Bypass workbook open or close events
Is it possible to write into code something that would bypass The Workbook Open and Close events altogether or even certain lines of code therein? Could someone provide an example to start me off please? Rob rob, If you're opening a workbook via code, then Application.EnableEvents = False before opening the workbook, will stop the "Events" (of which, Workbook_Open is an event), from firing. This would keep "Events" disabled (including the Workbook_Close) until you re-enabled them with Application.EnableEvents = True John "rob nobel" <robnobel@dodo.c...

Event ID: 2012 / 2013 SMTPSVC [ANNOYING ANNOYING ANNOYING]
I get these errors every hour on the hour for the past week. My logs look nasty with all these reds and yellows...me no likey, MS plz fix. thank you. Ode to Event ID: 2012 and 2013 Stop errors are Red Warning are yellow If I get more of these errors my server will not say HELO Event Type: Warning Event Source: smtpsvc Event Category: None Event ID: 2012 Date: 4/27/2004 Time: 6:57:22 AM User: N/A Computer: CASEYEXCH Description: SMTP could not connect to the DNS server '10.0.0.25'. The protocol used was 'UDP'. It may be down or inaccessible. For more information, see Help...

Which event?
Hi, I have a tab control on my main form, and each tab has a separate subform inside it. Each of those subforms contains a its own subforms. I have a combo box in the main form that selects a record. All of the subforms are set to filter based on that choice. Each subform has some formatting code that needs to run each time the user changes the selection from the combo box. I only want it to run once to reformat the subform based on its new filter. Which event should I use to place the code? I am currently using the On Current event for the subforms. But I think it runs...

button onclick event
Hi I have a spreadsheet with 6 worksheets, each of which has a button with corresponding onClick event code. In my final worksheet I have a button that when I click I want it to cause each of the onClick events of the other worksheets buttons to fire. Anyone any clues or sites I can view to help me do this? Thanks in advance Brendan ______________________________________________ Rate your experiences with your UK and Ireland builders at http://www.ratethebuilder.co.uk ========================================= If what I want cannot be done, is it possible to call a function in one s...

Event ID 9646 #2
This might be entirely coincidental, but after we upgraded to 2003 SP1 we started seeing the following errors crop up on the back end server. At some point, clients are not able to connect to the Exchange server, as they get an error message stating that the default folders cannot be opened. If we were to restart the ExchangeIS service, the users are able to connect normally. Any thoughts? Event Type: Error Event Source: MSExchangeIS Event Category: General Event ID: 9646 Date: 6/25/2004 Time: 5:16:38 PM User: N/A Computer: CSU-EXCH1 Description: Mapi session "27cd72e4-0174-...

Display names in Outlook 2003 address book
Moved from Office XP Pro to Office 2003. Mail sent from my computer will show the following in Sent to:, CC:, and BCC: FirstnameLastname instead of Firstname Lastname ie..there are no spaces between the first and last name. This was not a problem with the same address book in Office XP Pro (Outlook). Can someone explain why and what's the fix ? Mike E. <anonymous@discussions.microsoft.com> wrote: > Moved from Office XP Pro to Office 2003. How? What exact steps did you use? > Mail sent from > my computer will show the following in Sent to:, CC:, and > BCC:...

template with name-inputfield. Outlook 2000
I am sending the same message for many customers, and the only difference is the name. But the name is written several times in the message. Is it possible to make a template or anything like that, so I only have to change the name once, and then it changes for the whole document? This sounds like a good task for a Word mail merge. -- Sue Mosher, Outlook MVP Author of Microsoft Outlook Programming - Jumpstart for Administrators, Power Users, and Developers http://www.outlookcode.com/jumpstart.aspx "alko" <anonymous@discussions.microsoft.com> wrote in mes...

Catch the Opportunity Close event from OnSave event
Hi, Is anybody aware of a way to catch the event to 'close opportunity' inside the OnSave event. Obviously OnSave is called one last time when an opportunity is closed but since I can not catch it, I can not stop the execution of the section of the javascript code inside OnSave. If not is there a way to do it using callouts or workflow rules etc? Thanks in advance Isil OnSave is only triggered by the 'save' and 'save and close' buttons. I dont think the items in the actions menu trigger it. Callouts and workflow rules can capture the 'close opportunity...

mouseover event pop-up description question
Hi, I'm rather new to CRM, so I hope this is not a newbe question. I have looked for the answer online and in the "Working with Dynamics CRM 3.0 book" but haven't found it. In Dynamics CRM 4.0, when a user hovers the mouse over a form element name, the displayed field name reappears as a mouseover tooltip. That is not helpful. I'd rather have it display the field attribute's description. I did find an unsupported solution to customizing the mouseover event help window here, but that involves customizing each field, which I'd rather not do. http://bl...

Form not closing for event "leaving field"
Within a form the input-data (number) is checked wether it is already in the database. If it is already in the database, I want to open a new form and close the first form. But, when trying this, I always get the errorr "can't close form while event still running" (sorry, it is a translation from german). Any helpful hints how to solve the problem Thanks in advance Jokobe jokobe, Does that mean that when the input data is already in the db the record is not saved or shouldn't be saved? My guess is that your form state is still 'dirty' and therfore doesn't...

ESE Backup Event 904
I have a two node cluster I get the same errors when I run a backup job using Backup exec v.8.6 The infomation stores fail to apear after 5-6 successful backup jobs. After the job fails, I run a NTBackup and get an error message saying the the Exchange service is not started. I then reboot the node and try an NTBackup again, then the stores apeared. Again, this happens everytime after 6-5 successful backup. eventlog message: I first get this: Information Store (2148) Callback function call ErrESECBGetDatabasesInfo ended with error 0x8004010F . Two minutes later I get this: Information...

Canceling an OnClick Event
Hi all, Is it possible for a BeforeUpdate event of one control to cancel the OnClick event of another? Details: I have an unbound form that allows for editing and creating sites. txtSiteNo has a BeforeUpdate event that checks if the site already exists, and if it does, asks if you want to edit it. If so, the rest of the unbound text boxes update to what's already in the database. ctlAddSite has an OnClick Event which adds/edits the table info to the values of the rest of the unbound text boxes. Everything works fine, except that if someone enters an existing site int...

what is the name of a sub form download from microsoft website
i need to add a sub form to a form created in access. i tried to start the wizard but the feature is not currently installed. i try to install but it is looking for a disk for me to insert. i do not have a disk to insert. can i go to microsoft website and download the sub form feature for access? if yes, what is the name of the download. Are you sure it is not installed? Open your main form in design view. Click the Toolbox button in the menu at the top. There are two buttons at the top of the Toolbox, an Arrow and a wand. Is the Wand highlighted. If not, click on it then click on th...

Event ID: 5719
We recently upgraded our DCs from Win2003 to Win2008 R2. After the upgrade, I started seeing the below errors on our windows server running 2003 SP2. There is a firewall between the DCs and this server but it was working fine before the upgrade. Are there additional ports that need to be opened for Netlogon to function correctly in Win2008 R2? Event Type: Error Event Source: Userenv Event Category: None Event ID: 1053 Date: 2/3/2010 Time: 10:18:15 PM User: NT AUTHORITY\SYSTEM Computer: SERVER1 Description: Windows cannot determine the user or computer name. (There are...

user name
I have Microsoft Money 2004, and just moved. My e-mail address changed, so I have to change my user name on Money. It was sssss@comcast.com, now I want it to be xxxx@hotmail.com. How do I do that? In microsoft.public.money, megan wrote: >I have Microsoft Money 2004, and just moved. My e-mail >address changed, so I have to change my user name on >Money. It was sssss@comcast.com, now I want it to be >xxxx@hotmail.com. How do I do that? See http://support.microsoft.com/default.aspx?scid=kb;en-us;324001&Product=mny ...

Event Log Error
I'm running Exchange 5.5 on windows 2000 server. everyday i got error in my event log.I can't find any solution in Microsoft knowledgebase. please help me out. Thanks Event Log Details: Log Name : System Type : Error Date/Time: Tuesday, November 11, 2003 at 10:37:15 Source : DCOM Category : None ID : 10005 Desc : DCOM got error "The dependency service or group failed to start."attempting to start the service BITS with arguments ""in order to run the server: {4991D34B-8OA1-4291-83B6-3328366B9097} ...

Change Email from OnClick to On Deactivate
My Report is emailed from an On Click control, I am want to change it to On Deactivate of my Report is this possible, here is my code from a control Thanks if you can help me...................Bob Private Sub SendMailButton_Click() On Error GoTo ErrorHandler Dim lngID As Long, strMail As String, strBodyMsg As String, _ blEditMail As Boolean, sndReport As String, strCompany As String Dim msgPmt As String, msgBtns As Integer, msgTitle As String, msgResp As Integer Select Case Me.OpenArgs Case "ownerStatement" sndReport = "rptOwner...

MS Exchangeal Errors, Event:8331
And I have this problem too. More details needed. zehner arnold wrote: > And I have this problem too. ...

what is correct way to assign a custom event handler?
I have a (test) form with 3 panels and a button on each panel. I want to assign a single event handler to the 3 buttons using an anonymous method (just experimenting here) - this based on an example in a prior post (of mine). The compiler is complaining as noted below. What is the correct way to do this? using... ... namespace CsharpProj2008 { public partial class Form1 : Form { string[] s1 = new string[] { "test1", "test2", "test3 }; public frmMultiBtns() { InitializeComponent(); } //>>---complaining in this block bu...

How to access a category name and code from vbscript
I am modifying the lineitem.html for the status bar in Store operation. I want to ask how can i access the category name and code for each item, i only can find a categoryid function, which is in the TransactionEntry.Item.CategoryID, but can not access the name and code for the category Raemond, You will need to retrieve the name and code via SQL using the ID: resultSet=QSRules.OpenRecordset("SELECT Name, Code FROM Category WHERE ID=" & TransactionEntry.Item.CategoryID, true) - Evan Culver New West Technologies Raemond wrote: > I am modifying the lineitem.html for t...

Additional mailboxes not displaying "Mailbox -" before the name
I have seen this with all different versions of Outlook. When you add an additional mailbox to be displayed, it does not always display as "Mailbox - Smith, John" Something it will just display as "Smith, John" Why is this? Mailboxes are both running off E2k or E2k3 and on the same exact server. I have seen it originally being displayed as "Mailbox - Smith, John" but then I recreate the profile and "Mailbox -" disappears. How do I get it back. Is this an application problem? Is there a registry hack to fix it? Thanks, Keith Anybody kno...