How to add a property to an existing Winforms control

I am not sure if this is possible. I did some research, but could not find anything convincing. There is a similar question here , but this is for WPF.

What I would like to do is add a custom property to an existing WinForms GroupBox (or any control) in my form. In this example, we will use the "Link". Say that each GroupBox in my program contains a hyperlink, and then all I need to do when I start my program is to do this:

MyGroupBox.Link = "http:\\www.google.com\" 

Later in my program, I could set the contents of the hyperlink to MyGroupBox.Link .

Can I manipulate a Winforms control as follows? I would prefer not to create a custom control if I don't need it.

I saw from this question that I could expand my control, but what would it look like in my particular case? Is it the same as creating a custom control?

+6
source share
3 answers

I have not tried this with GroupBox, but I think you can do something similar to the Button example here.

http://msdn.microsoft.com/en-us/library/7h62478z(v=vs.90).aspx

Just create a new class, name it MyGroupBox or whatever:

 public class MyGroupBox : GroupBox { private string link; public string Link {get {return link;} set{link=value;} } } 

Inherits all GroupBox behavior / properties and adds a new property for Link.

Then you can simply use it as follows:

 MyGroupBox groupBox = new MyGroupBox(); groupBox.Link = "www.google.com"; 

I think this is cleaner than using the tag property, honestly. Mostly because it’s not a tag, it’s a link, and I like being able to name the property correctly. :) Although the tag may be easier if you need to do this for many controls, not just GroupBox.

+5
source

One way is to use Extender Providers. They act as a ToolTip component when added to a form; it provides a property called ToolTip for each control in that form. You can create this class:

  [ProvideProperty("Link", typeof(Control))] public class ExtendControls : Component, IExtenderProvider { private Dictionary<Control, string> links = new Dictionary<Control, string>(); public bool CanExtend(object extendee) { return !(extendee is Form); } public void SetLink(Control extendee, string value) { if (value.Length == 0) { links.Remove(extendee); } else { links[extendee] = value; } } [DisplayName("Link")] [ExtenderProvidedProperty()] public string GetLink(Control extendee) { if (links.ContainsKey(extendee)) { return links[extendee]; } else { return string.Empty; } } } 

What it does is provide the Link property to all controls except the form. Now you create this class in your windowsforms project and build it, after you go to the form designer in the toolbar, you should see the extension controlcontrols the component will drag it into the form and be placed in the component tray. Almost everything ... then you can use your new Link property either in the properties window on the desired control or in code similar to this (provided that you left the component to its default name):

 //assuming ofcourse you have a button called button1 //i used button as the example you can use panel,datagridview,label,etc... //to set it... extendControls1.SetLink(button1, "sometest"); //to get it back... string myLink = extendControls1.GetLink(button1); 
+3
source

In response to Savanna, you can simply create your extended class using the Savanna method described above, and then drag the actual class into the toolbar. Magically, you will get it in your toolbox, ready to drag it into the form in design mode, complete with any custom properties that you added.

0
source

Source: https://habr.com/ru/post/972203/


All Articles