简体   繁体   English

C#和Windows窗体控件命名准则

[英]C# and Windows Forms control naming guidelines

是否有人对表单和控件(例如事件,按钮)的命名约定有任何准则/最佳实践?

I usually use Systems Hungarian notation Example: 我通常使用系统匈牙利表示法示例:

btnSubmit : is button btnSubmit :是按钮

lblAccountNum : variable is a label lblAccountNum :变量是标签

In WinForms I usually use suffixes, so for example: 在WinForms中,我通常使用后缀,例如:

submitButton
accountNumberLabel

etc. 等等

But a lot of companies still use a prefix, like anthares said. 但是许多公司仍然使用前缀,如anthares所说。

I don't believe any special rules are needed for forms development; 我认为表单开发不需要任何特殊规则; the Microsoft .NET naming guidelines cover it. Microsoft .NET命名指南对此进行了介绍。

I name forms like any other class in the application: MainForm , SaveDialog , etc. 我像在应用程序中的任何其他类一样命名表单: MainFormSaveDialog等。

I name controls like any other field within a class: okButton , nameTextBox , etc. 我将控件命名为类中的其他任何字段: okButtonnameTextBox等。

The following example is most of the .net programmer is used 以下示例是大多数.net程序员使用的

Control: Label
Prefix : lbl
Example: lblFirstName

I usually prefix the full class name eg textBoxName. 我通常以完整的类名作为前缀,例如textBoxName。 I find it easier to read than three letter prefixes and it's consistent with the names that are generated by the IDE. 我发现它比三个字母前缀更易于阅读,并且与IDE生成的名称一致。 I only name controls that are referred to by code. 我只命名代码引用的控件。 Controls that are databound usually don't need a name. 数据绑定的控件通常不需要名称。

The reason that the prefixes are not the full class names in most of the companies but some abbreviations of the class name are as follows: 在大多数公司中,前缀不是完整的类名,而是某些类名的缩写,其原因如下:

  1. Most of the naming conventions are approved before Visual Studio 2010. 大多数命名约定都在Visual Studio 2010之前获得批准。
  2. All Visual Studio versions before 2010 have their inteli sense filter with something like "starts with" instead of contains. 2010年之前的所有Visual Studio版本都有其智能感知筛选器,例如“开始于”而不是包含。

That's why a lot of people / architects / leaders decided it will be a better idea to type "txt" and inteli sense will filter all textboxes for you, so then you just type "E" for example and you get txtEmail. 这就是为什么许多人/建筑师/领导者认为键入“ txt”是一个更好的主意,而inteli sense会为您过滤所有文本框,因此,例如,您只需键入“ E”,便会得到txtEmail。 If you have the full class name, you will need to type "textBoxE" to get the same result in inteli sense. 如果您具有完整的类名,则需要输入“ textBoxE”以在智能上获得相同的结果。 This adds a lot of overheat when you have complex UI. 当您使用复杂的UI时,这会增加很多过热。

Now with Visual Studio 2010 you get a better inteli sense so you can just type "em" and you can easilly see the "textBoxEmail" in the list (along with Email and some other things that contain "em"). 现在,借助Visual Studio 2010,您将获得更好的智能感,因此您只需键入“ em”,就可以轻松地在列表中看到“ textBoxEmail”(以及Email和其他包含“ em”的东西)。 Still I seem to prefer to have 2-3 or up ot 4 letters abbreviation that will allow me to filter in inteli sense by control type (specially for UI) than having to type textBox. 我似乎还是更喜欢使用2-3个或最多4个字母的缩写,这使我可以按控件类型(特别是针对UI)以智能方式进行过滤,而不必键入textBox。 The reason I prefer it is that even if you are puzzled for a while with some control type (eg "rg" for RadGrid) you will need 5 minutes 3-4 times to remember it and start typing without thinking about it. 我喜欢它的原因是,即使您对某种控件类型(例如RadGrid的“ rg”)感到困惑,您也将需要5分钟3-4次来记住它并开始输入而无需考虑它。 While if you have radGrid everywhere you will need to hit 7 strokes to get to the meaningful one that will filter for you (eg "radGridC" in "radGridCustomers"). 如果您到处都有radGrid,则需要打7招才能找到适合您的过滤条件(例如,“ radGridCustomers”中的“ radGridC”)。

I do agree that only naming controls that are referenced in the code is usually enough. 我确实同意,仅命名代码中引用的控件通常就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM