Naming convention for controls [duplicate]

2019-03-08 00:26发布

This question already has an answer here:

Microsoft has naming guidelines on their website (here). Also I have the Framework Design Guidelines book.

What I could not find was a guideline about naming controls.

For example, a button, when dropped to a form, gets the typename + number, camel-cased as default name, such as "button1".

This is what I do: I delete the number and add a meaningful description after. For example "buttonDelete" or "buttonSave".

This way you do not have to maintain a big list of controls and their abbreviated names in a guideline somewhere.

Do you agree?

12条回答
祖国的老花朵
2楼-- · 2019-03-08 01:05

I don't have a convention as such, but I do try to be very broad with the 'type' portion of the name. e.g. Button, Link Button, Image Button tend to be named 'somethingButton'. Combo boxes, radio button lists all end up as 'somethingSelector'. TextBoxes and Calendars are 'somethingInput'. That way I get a rough idea of what sort of control it is without the name being tied to the actual implementation. If I decide to replace an option button group with a dropdown then no need to rename!

查看更多
虎瘦雄心在
3楼-- · 2019-03-08 01:05

Yes, I agree totally (but I rename it to ButtonDelete), so lowercase names are for variables in my case :)

Personally, I think as long as you are consistent, you won't run into problems even if someone else is reading your code.

查看更多
何必那么认真
4楼-- · 2019-03-08 01:06

GUI programming gets the short stick when it comes to conventions of all sorts. See my answer to another question for the guidelines I use for naming.

查看更多
【Aperson】
5楼-- · 2019-03-08 01:09

I'm probably one of the last few people that still uses Hungarian notation. I know that argument that the IDE can tell you the variable type, but that doesn't help me when I'm coding in Notepad++ or looking at a printout.... anyway, I use the "btnSave", "cbOptions", "txtFirstName", "lblTitle", "ddlCardType", etc... I just like being able to glance at code and know what I'm looking at without looking for a declaration or hovering over a variable to get it's data type from the IDE.

查看更多
Emotional °昔
6楼-- · 2019-03-08 01:13

Here are some common ones:

frm  Form
mnu  Form menu
cmd  Command button
chk  Check button
opt  Radio button
lbl  Text label
txt  Text edit box
pb   Picture box
pic  Picture
lst  List box
cbo  Combo box
tmr  Timer

A longer list is at INFO: Object Hungarian Notation Naming Conventions for VB.

查看更多
小情绪 Triste *
7楼-- · 2019-03-08 01:15

Yes change those names

For me:

  • Button btnDescription

  • TextBox txtDescription

  • ComboBox cboDescription

etc...

查看更多
登录 后发表回答