简体   繁体   English

C#WinForms自定义控件默认属性

[英]C# WinForms custom control default properties

How to you set the default properties for custom controls, ie when they are first dragged onto the form in the Designer? 如何设置自定义控件的默认属性,即首次将它们拖动到Designer中的表单上?

Can't find an answer here or via Google; 无法在此处或通过Google找到答案; all I get is how to constrain the values. 我得到的是如何约束价值观。

Using Width & Height as examples, if I set them in the constructor they get applied to the control everytime the Designer is opened. 使用Width&Height作为示例,如果我在构造函数中设置它们,则每次打开Designer时它们都会应用于控件。 How do I set them to a default which is never applied again after the user changes the properties? 如何将它们设置为默认值,在用户更改属性后永远不会再次应用它?

Try using the DefaultValue attribute. 尝试使用DefaultValue属性。

private int height;

[DefaultValue(50)]
public int Height
{
    get 
    {
       return height;
    }
    set 
    {
       height=value;
    }
 }

What worked for me for properties that I can't override is using the new operator. 对于我无法覆盖的属性,对我有用的是使用new运算符。 For example, the MultiSelect property on a ListView control. 例如, ListView控件上的MultiSelect属性。 I want MultiSelect to default to false , but I still want to be able to change it. 我希望MultiSelect默认为false ,但我仍然希望能够更改它。

If I just set it to false in the constructor, or in InitializeComponent , the problem (I think) is that the default value is still true , so if I set the value to true in the designer, the designer notices that true is the default, and so just doesn't set the property at all rather than explicitly setting it to what it thinks is already the default. 如果我只是在构造函数或InitializeComponent中将其设置为false ,问题(我认为)是默认值仍为true ,因此如果我在设计器中将值设置为true ,则设计器会注意到true是默认值,所以根本不设置属性,而不是明确地将其设置为它认为已经是默认值。 But then the value ends up being false instead, because that is what is set in the constructor. 但是这个值最终会false ,因为这是构造函数中设置的内容。

To get around this issue I used the following code: 为了解决这个问题,我使用了以下代码:

/// <summary>Custom ListView.</summary>
public sealed partial class DetailsListView : ListView
{
   ...

   [DefaultValue(false)]
   public new bool MultiSelect {
      get { return base.MultiSelect; }
      set { base.MultiSelect = value; }
   }

This allows the control to still have a functioning MultiSelect property that defaults to false rather than true , and the property can still be toggled on the new control. 这允许控件仍具有正常运行的MultiSelect属性,默认为false而不是true ,并且仍可以在新控件上切换属性。

EDIT: I encountered an issue having to do with using abstract forms. 编辑:我遇到了与使用抽象表单有关的问题。 I've been using abstract form classes, with a concrete implementation that I switch to when I need to use the designer. 我一直在使用抽象表单类,我需要使用设计器时切换到具体实现。 I found that when I switched the class that I was inheriting from that the properties on my custom control would reset to the old defaults. 我发现当我切换继承的类时,我的自定义控件上的属性将重置为旧的默认值。 I seem to have corrected this behaviour by setting properties to their defaults in the constructor of the custom control. 我似乎已通过在自定义控件的构造函数中将属性设置为其默认值来更正此行为。

In the constructor, set the values of your properties you want to appear when you drag it onto the canvas. 在构造函数中,设置将属性拖动到画布上时要显示的属性的值。 Or if they are the built in properties of the base control then set them in the designer code class. 或者,如果它们是基本控件的内置属性,则将它们设置在设计器代码类中。

The below will allow you to add the value when you display the form, after that you can set it as you want. 下面将允许您在显示表单时添加值,之后您可以根据需要进行设置。

private int widthLength = 5;  

public int Width {      

     get { return widthLength ; } 
     set { widthLength = value;   
} 

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

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