C# 控件的命名约定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/642759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Naming convention for controls
提问by Patrick Peters
Microsoft has naming guidelines on their website (here). Also I have the Framework Design Guidelines book.
Microsoft 在其网站(此处)上有命名指南。我还有框架设计指南书。
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".
例如,一个按钮,当放到表单上时,会得到 typename + number,驼峰式大小写作为默认名称,例如“button1”。
This is what I do: I delete the number and add a meaningful description after. For example "buttonDelete" or "buttonSave".
这就是我所做的:我删除了数字并在之后添加了一个有意义的描述。例如“buttonDelete”或“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?
你同意?
采纳答案by Jon M
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!
我没有这样的约定,但我确实尝试对名称的“类型”部分进行非常广泛的定义。例如,Button、Link Button、Image Button 往往被命名为“somethingButton”。组合框、单选按钮列表都以“somethingSelector”结尾。文本框和日历是“somethingInput”。这样我就可以粗略地了解它是什么类型的控件,而无需将名称与实际实现联系起来。如果我决定用下拉列表替换选项按钮组,则无需重命名!
回答by Gambrinus
Yes, I agree totally (but I rename it to ButtonDelete), so lowercase names are for variables in my case :)
是的,我完全同意(但我将其重命名为 ButtonDelete),所以在我的情况下,小写名称用于变量:)
Personally, I think as long as you are consistent, you won't run into problems even if someone else is reading your code.
就个人而言,我认为只要您保持一致,即使其他人正在阅读您的代码,您也不会遇到问题。
回答by Jason Punyon
I'm not sure, but I think that control naming in Windows Formsis one of the only places I can see a use for Hungarian notation. So I think you're good.
我不确定,但我认为Windows Forms中的控件命名是我能看到匈牙利表示法使用的唯一地方之一。所以我觉得你很好。
回答by Mark Pim
Yes, you need meaningful identifiers for any variable - control or not - the default names are only because your IDE knows nothing about your problem domain and so can't always 'guess' a better name.
是的,您需要任何变量的有意义的标识符 - 控制与否 - 默认名称只是因为您的 IDE 对您的问题域一无所知,因此不能总是“猜测”更好的名称。
回答by user68610
Yes change those names
是的,更改这些名称
For me:
为了我:
Button btnDescription
TextBox txtDescription
ComboBox cboDescription
Button btnDescription
TextBox txtDescription
ComboBox cboDescription
etc...
等等...
回答by Lazarus
I believe that current thinking frowns upon including the control type in the name. I'd be inclined to treat them as another other object I'm using and follow the same naming convention.
我相信当前的想法不赞成在名称中包含控件类型。我倾向于将它们视为我正在使用的另一个对象并遵循相同的命名约定。
Certainly use meaningful naming, that goes without saying :) However, at the end of the day, if your naming convention still makes sense to you when you revisit your code months later then I'd probably stick with it.
当然使用有意义的命名,这是不言而喻的:) 然而,在一天结束时,如果你几个月后重新访问你的代码时你的命名约定仍然对你有意义,那么我可能会坚持下去。
回答by Krzysztof Kozmic
I don't do WinForms for quite some time but what I did was two things.
我很长一段时间不做 WinForms,但我做了两件事。
- uncheck 'generate member' (or however it is called) for things like labels, etc. Basically ensuring I keep as fields only things I need.
- for those I need, set descriptive name. It if is necessary, appendthe name of the control (ie saveButton). If I don't feel like adding a control name adds any value I would not append the 'Button' and leave the name simply as 'save'.
- 对于标签等内容,取消选中“生成成员”(或无论如何调用)。基本上确保我只保留我需要的字段。
- 对于我需要的人,请设置描述性名称。如有必要,附加控件的名称(即 saveButton)。如果我不想添加控件名称会增加任何值,我不会附加“按钮”并将名称简单地保留为“保存”。
Basically most of the time I would not create a member for save button at all. (If you have some save logic you still can have only OnSaving event handler subscribed to the button's Click event).
基本上大部分时间我根本不会为保存按钮创建成员。(如果您有一些保存逻辑,您仍然只能让 OnSaving 事件处理程序订阅按钮的 Click 事件)。
https://msdn.microsoft.com/en-us/library/ms233630(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/ms233630(v=vs.110).aspx
回答by Michael Meadows
GUI programming gets the short stick when it comes to conventions of all sorts. See my answer to another questionfor the guidelines I use for naming.
当涉及到各种约定时,GUI 编程是捷径。有关我用于命名的准则,请参阅我对另一个问题的回答。
回答by mlindegarde
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.
我可能是最后几个仍然使用匈牙利符号的人之一。我知道 IDE 可以告诉您变量类型的论点,但是当我在 Notepad++ 中编码或查看打印输出时,这对我没有帮助......无论如何,我使用“btnSave”,“cbOptions”, “txtFirstName”、“lblTitle”、“ddlCardType”等......我只是喜欢能够浏览代码并知道我在看什么,而无需寻找声明或将鼠标悬停在变量上以从集成开发环境。
回答by Konstantinos
Thisis what we are using
这就是我们正在使用的
In short, we prefix the controls with an abbreviation of the control. ie
简而言之,我们用控件的缩写作为控件的前缀。IE
Buttons = btnDelete, btnSubmit, btnReturn
按钮 = btnDelete, btnSubmit, btnReturn
Textboxes = txtUsername, txtPassword etc
文本框 = txtUsername、txtPassword 等
that way, by typing the abbreviation you get all the similar controls by the time you finish typing the abbreviation ie type btn and intellisense will list all the buttons you have added so far.
这样,通过键入缩写,您将在完成键入缩写时获得所有类似的控件,即键入 btn,intellisense 将列出您到目前为止添加的所有按钮。