8

我们实施了新的编码标准,要求我们的私人成员有一个领先的下划线。像这样:

private System.Windows.Forms.Label _label;

不幸的是,当您将新标签拖到表单上时,VS 会在下面显示默认值:

private System.Windows.Forms.Label label1;

有没有办法将默认更改为:

private System.Windows.Forms.Label _label1;

这适用于所有控件,而不仅仅是标签,是的,它们将在代码中使用。

干杯,

普拉门

4

7 回答 7

11

我个人认为自动生成的代码应该被排除在任何编码指南等之外。在大多数情况下忽略它们是安全的,除非生成器有错误。

请与编写该编码指南的人辩论,并要求 he.her 排除生成的代码。

于 2010-04-13T06:47:01.573 回答
3

除了创建您自己的 VS 插件来监视生成的代码并自动重命名它之外,没有办法完成您想要的。

于 2010-04-19T13:32:24.117 回答
3

我知道这是一个老问题,但我最近一直在与定制设计师合作,我有一个你想要的解决方案。将此类添加到您的项目中:

using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace WindowsFormsApplication1
{
    [Designer(typeof(BaseFormDesigner), typeof(IRootDesigner))]
    public class BaseForm : Form
    {
        public BaseForm()
        {
        }
    }

    public class BaseFormDesigner : DocumentDesigner
    {
        public override void Initialize(IComponent component)
        {
            base.Initialize(component);
            var service = GetService(typeof(IComponentChangeService)) as IComponentChangeService;
            service.ComponentAdded += service_ComponentAdded;
        }

        private void service_ComponentAdded(object sender, ComponentEventArgs e)
        {
            if (e.Component is Control && !(e.Component is Form)) {
                var control = (Control)e.Component;
                if (!control.Name.StartsWith("_")) {
                    var service = GetService(typeof(IComponentChangeService)) as IComponentChangeService;
                    PropertyDescriptor desc = TypeDescriptor.GetProperties(control)["Name"];
                    service.OnComponentChanging(e.Component, desc);
                    string oldValue = control.Name;
                    string newValue = "_" + oldValue;
                    desc.SetValue(control, newValue);
                    service.OnComponentChanged(e.Component, desc, oldValue, newValue);
                }
            }
        }
    }
}

然后将您的基本表单类从 Form 更改为 BaseForm:

using System;

namespace WindowsFormsApplication1
{
    public partial class Form1 : BaseForm
    {
        public Form1()
        {
            InitializeComponent();
        }
    }
}

在重新编译您的项目之前关闭任何表单设计器,然后重新打开它并尝试添加一个控件,其名称将以下划线开头。

于 2015-12-17T07:33:53.297 回答
1

这个答案可能不令人满意,但这是我能提供的最好的答案。

我认为您无法让 Visual Studio 设计器自动添加下划线。但是,您可以做的是使添加下划线的过程不那么痛苦。只需先创建没有下划线的对象;然后,使用重构功能重命名它们。只需将光标放在字段名称上(label1在您的情况下为 ),然后按 F2(或右键单击 ⇒ 重构 ⇒ 重命名)。无论您在代码中的哪个位置,这都有效,您只需要提及label1. 由于您可能会编写使用控件的代码,因此您可能会引用这些字段。您也可以按 F12 直接进入声明,您可以在其中有效地获得所有控件的完整列表;您可以轻松地使用 F2 一次性重命名其中的许多。

顺便说一句,我还使用 F2 重命名大多数自动生成的事件处理程序名称。例如,我讨厌看到像btnZoomIn_Click(). 我更喜欢调用该方法zoomIn(),因为它就是这样做的。而不是Mainform_KeyPress()我可能会调用它processKeyPress()

于 2010-04-09T13:38:08.293 回答
0

您可以将 GenerateMember 设置为 false。
这并不能解决您的问题,但是当您不手动使用控件时,请避免命名错误。

于 2010-04-13T00:57:14.750 回答
0

Resharper 可以强制执行命名约定。然后只需打开 .designer 文件并按 alt+enter,输入几次即可。

于 2010-04-13T04:08:35.240 回答
0

正如 BlueRaja 所说,您可以使用 Resharper。然后你可以使用它的代码清理(Ctrl-E,C),但是这对生成的文件不起作用,所以你必须暂时重命名它。我不在工作,所以我无法检查是否有选项可以在某处启用生成文件的选项。

请注意,每次重新生成文件时,您都必须重新运行它。

于 2010-04-16T18:01:54.840 回答