简体   繁体   English

如何在WinForms中扩展控件?

[英]How do I extend a Control in WinForms?

Any control. 任何控制。 But preferably all of them (TextBox, Panel, Button, LinkLabel, TabControl, etc). 但最好都是它们(TextBox,Panel,Button,LinkLabel,TabControl等)。 What I would like to do is: 我想做的是:

public class Something
{
   public String isBetterThan { get; set; }
   public String Author { get; set; }
}

public void button1_Click(object sender, EventArgs e)
{
   panelControl1.ClassObject = new Something()
      {
         isBetterThan = "nothing.",
         Author = "Unknown"
      };
}

So from the code above, you can see that it acts similarly to the .Location property, where you assign it a new value. 因此,从上面的代码中,您可以看到它的行为类似于.Location属性,在其中您为其分配了一个新值。 I would like to store this information, so that later on, I can simply do this: 我想存储此信息,以便以后可以简单地执行以下操作:

public void getClassDetailsButton_Click(object sender, EventArgs e)
{
   Something something = (Something)panelControl1.ClassObject;
   MessageBox.Show("Something is better than " + something.isBetterThan);
}

You can create a Custom control by inheriting from the control you are trying to add the function to. 您可以通过继承要向其添加功能的控件来创建Custom控件。 Something like this should work ( using a button as an example ) 这样的事情应该可以工作( 以按钮为例

using System;
using System.Collections.Generic;
using System.Linq; 
using System.Text;
using System.Windows.Forms; 

namespace WindowsFormsApplication1
{
    class SomethingButton : Button
    {
        public Something mySomething
        { get; set; }
    }

    public class Something
    {
        public String isBetterThan { get; set; }
        public String Author { get; set; }
    }

}

Usuage 使用方式

somethingButton1.mySomething = new Something() { isBetterThan = "nothing", 
                                                 Author = "Unknown" 
                                               };

I think the answer you are looking for is the DefaultValue attribute. 我认为您正在寻找的答案是DefaultValue属性。 You set it once in the class and then whenever the object is created it gets assigned the value. 您可以在类中设置一次,然后在创建对象时为其分配值。 There's a couple of pitfalls with saving the object data using this attribute so use caution and do regression testing. 使用此属性保存对象数据有两个陷阱,因此请谨慎使用并进行回归测试。

http://msdn.microsoft.com/en-us/library/system.componentmodel.defaultvalueattribute(v=VS.71).aspx http://msdn.microsoft.com/zh-CN/library/system.componentmodel.defaultvalueattribute(v=VS.71).aspx

[DefaultValue("Author Name")] [DefaultValue(“作者名称”)]

You can always create a class that extends off of the control, and override the methods that you'd like to add functionality to. 您始终可以创建一个扩展到控件之外的类,并覆盖要向其添加功能的方法。

public class MyTextBox : TextBox {

public String isBetterThan { get; set;}
public String author {get; set;}

protected override void OnMouseLeave(MouseEventArgs e)        
{            
     base.OnMouseLeave(e);
     // do something
     isBetterThan = this.Text;
}    
}

Then, add the control to your Form. 然后,将该控件添加到您的窗体。 You can treat it like a regular TextBlock, but also ask it for isBetterThan and Author. 您可以像对待常规TextBlock一样对待它,也可以要求它提供isBetterThan和Author。

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

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