本文介绍了如何防止Winforms Designer将Text属性设置为实例名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开始之前,在.
您可以为控件创建一个新的设计器并覆盖该方法,并在调用基本方法后,将 Text 属性设置为空字符串.

The designer sets Text property of control in InitializeNewComponent of ControlDesigner.
You can create a new designer for your control and override that method and after calling base method, set the Text property to empty string.

通过这种方式,您的控件以空的 Text 属性开始,并且还可以在设计时使用属性网格更改 Text 的值.

This way your control starts with an empty Text property and also you can change the value of Text using property grid at design-time.

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

[Designer(typeof(MyControlDesigner))]
public partial class MyControl: Control
{
}

public class MyControlDesigner : ControlDesigner
{
    public override void InitializeNewComponent(System.Collections.IDictionary defaultValues)
    {
        base.InitializeNewComponent(defaultValues);

        PropertyDescriptor descriptor = TypeDescriptor.GetProperties(base.Component)["Text"];
        if (((descriptor != null) && (descriptor.PropertyType == typeof(string))) && (!descriptor.IsReadOnly && descriptor.IsBrowsable))
        {
            descriptor.SetValue(base.Component, string.Empty);
        }
    }
}

这篇关于如何防止Winforms Designer将Text属性设置为实例名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 02:01
查看更多