How should I name form controls variables and their event handlers in WinForms?
I have a little coding style problem - what names should I choose for controls in Windows Forms in my application? I think that best approach is to not generate the field at all (GenerateMember
property), this can work at buttons, separators, menu items etc.
But there are for example some labels and controls that I need to reference, modify, disable, etc. from the code. Currently I name them this way: lblInfo
(label) , cbPerson
(combobox), bt开发者_StackOverflownOK - I think this is useful use of Hungarian notation. Their names start in lowercase because they are form's fields.
I choose names for the menu items depending on their hierarchy - so it's menuFileExit, menuEditCopy, etc.
The problem comes when I create event handler for one of controls - Visual Studio autogenerates handler such as:
btnOk_Click
, menuEditCopy_Click
- and StyleCop and I don't like no methods that start with lowercase. I also wouldn't like the event handlers to have different prefix than the name of the control that raises the event. Should I just name the event handlers like BtnOk_Click
with first letter in uppercase to satisfy the coding standard? Thanks
I personally prefer to name my controls variable OkButton
rather than btnOk
in order to focus on functionality rather than type. For event handlers, I'd say OnOkButtonClick
, the formula being On + ControlName + Event.
Yes, just rename them. VS will even rename all references in code to these newly renamed methods.
2 general rules when I am naming variables.
keep the most important information at first.
make the whole name human friendly. So people will focus on code logic rather than get distracted by weird names.
精彩评论