I'm not sure if this is even possible. I've done some research, but haven't been able to find anything conclusive. There is a similar question here, but it's for WPF.
What I'd like to do is add a custom property to an existing WinForms GroupBox (or any control) on my form. For this example we'll use "Link". Say each GroupBox in my program contains a hyperlink, and then all I would need to do when I start my program is do this:
MyGroupBox.Link = "http:\\www.google.com\"
Later in my program I could set my hyperlink content to refer to MyGroupBox.Link
.
Is it possible to manipulate a Winforms control like this? I'd rather not make a custom control if I don't have to.
I saw from this question that I could extend my control, but how I would that look in my particular case? Is that the same as creating a custom control?
One way would be to use Extender Providers. They act like the ToolTip
component, when it's added to a form, it provides a property called ToolTip
to each control on that form. You could 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 it will provide the Link
property to all controls except the form. Now you create this class in your Windows Forms project and build it, after that you go to the designer of the form in the toolbox. You should see the ExtendControls
component, drag it on to the form and it will be placed in the component tray. Almost done...
Next you can use your new Link
property, either in the properties window on the desired control or in code like this (assuming you left the component to its default name):
// assuming of course 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);
I haven't tried this with GroupBox, but I'm thinking you could do something similar to the example with Button here.
http://msdn.microsoft.com/en-us/library/7h62478z(v=vs.90).aspx
Just create a new class, call it MyGroupBox or whatever you want:
public class MyGroupBox : GroupBox {
private string link;
public string Link {get {return link;} set{link=value;} }
}
This inherits all the behavior/properties from GroupBox and adds a new property for a Link.
Then you can just use it this way:
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 appropriately. :) Although tag might be easier if you are needing to do this for many controls and not just GroupBox.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With