views:

417

answers:

2

I would like to know how to add a property from one of my user controls to the base controls property list. Is this possible?

So what I am doing is this:

private static List<LabelType> ConvertControlCollectionToList(Control customContainer)
    {
        LabelTypeList.LabelProps.Clear();

        foreach (Control c in customContainer.Controls)
        {
            LabelTypeList.LabelProps.Add(new LabelType());
            LabelTypeList.LabelProps.Last().Name = c.Name;
            LabelTypeList.LabelProps.Last().Top = c.Top;
            LabelTypeList.LabelProps.Last().Left = c.Left;
            LabelTypeList.LabelProps.Last().Height = c.Height;
            LabelTypeList.LabelProps.Last().Width = c.Width;
            LabelTypeList.LabelProps.Last().Font = c.Font;
            LabelTypeList.LabelProps.Last().Text = c.Text;
            LabelTypeList.LabelProps.Last().DataColumn = c.?????
            LabelTypeList.LabelProps.Last().Rotation = c.?????
        }

        return LabelTypeList.LabelProps;
    }

I have two properties at the end of the list which are custom to my usercontrol but I need to add them to the base control class so when I load the collection back into a form my settings for DataColumn and Rotation are accessible. Does that make sense?

Thanks.

+2  A: 

Say, for example, that your control is a TextBox named txtFirstName and you want a property representing the text of that TextBox on your UserControl:

public string FirstName
{
    get { return txtFirstName.Text; }
    set { txtFirstName.Text = value; }
}

Edit

Given the edit to the question, no you can't do exactly what you're asking for (add your property to those of the base class). You can, however, check to see if a particular control is an instance of your UserControl, and gather those properties if it is. To do so, use this:

private static List<LabelType> ConvertControlCollectionToList(Control customContainer)
{
    LabelTypeList.LabelProps.Clear();

    foreach (Control c in customContainer.Controls)
    {
        LabelType lt = new LabelType();

        LabelTypeList.LabelProps.Add(lt);

        lt.Name = c.Name;
        lt.Top = c.Top;
        lt.Left = c.Left;
        lt.Height = c.Height;
        lt.Width = c.Width;
        lt.Font = c.Font;
        lt.Text = c.Text;

        YourUserControlType uc = c as YourUserControlType;

        if(uc != null)
        {
            lt.DataColumn = uc.DataColumn;
            lt.Rotation = uc.Rotation;
        }
    }

    return LabelTypeList.LabelProps;
}

(I removed the call to Last(), as that is a LINQ extension method and will cause the entire list to be traversed every time you set a property).

Adam Robinson
Downvoter care to elaborate as to why? This appears to be exactly what he's asking for.
Adam Robinson
Adam, in your answer is YourUserControlType a type class or the actual UserControl? If it is the usercontrol how can I access it from my biz tier without causing a circular reference?
Nathan
It's a type, hence the suffix of `Type` ;)
Adam Robinson
It's been a long day.
Nathan
I am getting this error now when when I use MyType t = c as MyTypeCannot convert type 'System.Windows.Forms.Control' to 'ScanProTypes.UserLabelType' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion
Nathan
+1 for removing those horrifying repeated calls to Last(), although I might have been inclined to use an object initializer.
Joel Mueller
Nathan, my first thought on seeing that error would be to check whether ScanProTypes.UserLabelType actually inherits from System.Windows.Forms.Control or not.
Joel Mueller
@Joel: I would have used an initializer, but I didn't want to change the structure of the code more than I needed to, and the initializer wouldn't have actually optimized anything, it would have just looked prettier ;)
Adam Robinson
+1  A: 

In WPF, you can use Attached Properties to provide this behavior. This is how things like Grid.Row works.

In Windows Forms, you'll want to use an Extender Provider.

Note that neither option truly adds properties to the base class - you can't change the base class structure in a subclass. They do add properties available at design time that are usable with the control.

Reed Copsey
The impression I got was that he wanted to do the opposite: expose a property from a child control on the parent control.
Adam Robinson
Not sure what he's after, at least not 100% sure. Maybe Nathan can chime in with more details.
Reed Copsey
Please see the edit to my original post above and maybe it will be more clear.
Nathan