views:

51

answers:

3

Hello, 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), btnOK - 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

A: 

Yes, just rename them. VS will even rename all references in code to these newly renamed methods.

logicnp
+1  A: 

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.

Sylvestre Equy
A: 

2 general rules when I am naming variables.

  1. keep the most important information at first.

  2. make the whole name human friendly. So people will focus on code logic rather than get distracted by weird names.

Jerry Liu