9
votes

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?

3
Why not just subclass GroupBox and add the property?eddie_cat
@Savanna Unfortunately, I have no idea how to do that. Could you post an answer?gnarlybracket
Each control in WinForms has Tag property that basically just object, so you can set any data to it, and use it. You can set any url to it, and then get it and cast back to string.Sergey Litvinov
@SergeyLitvinov I knew that there's a Tag property, but I'm not sure if this would be a good use? I might use that if I don't get a satisfactory answer.gnarlybracket
If you don't want to use the Tag, then you'll have to create a derived custom control. See msdn.microsoft.com/en-us/library/7h62478z(v=vs.90).aspx for an example.Jim Mischel

3 Answers

12
votes

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);
6
votes

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.

-1
votes

In response to Savanna, you can just create your extended class using the method Savanna described, and then drag the actual class to the toolbox. Magically, you'll have it in your toolbox, ready to drag on to your form in designer mode, complete with whatever custom properties you've added.