CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2011
    Location
    .Net 4.0
    Posts
    39

    Extending Generic Control

    I would like to add properties to controls of every type. For example, each generic control has a "Tag" property. I'd like to add a property "ControlDescription" and some others to do some things that really will not help to explain.

    I know how to create extended versions of each TYPE of control, one by one. I can do the button, the radioButton, the checkBox, etc. But that would be a lot of duplicitous coding, not to mention duplicitous upkeep.

    On a one by one basis, you can do this:

    CustomLabel myLabel=new CustomLabel();

    But to extend all controls, you can't do this:

    Label myLabel=new CustomControl() ** hoping that Label will inherit from Control since CustomControl() inherits from Control.

    How can I do what I want?

    Thanks.

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: Extending Generic Control

    Interesting question!

    However, I'm not certain that there is a way to express that under the current standard. The "obvious" way to me would be to define a generic class like:

    Code:
    public class GenericDerived<T> : T
    {
        //...Class implementation
    }
    But that will fail, because C# will not allow you to "generically" extend a class specified by a type parameter.

    Possibly some strategy involving reflection...?

    Anyone have any bright ideas?
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Dec 2011
    Location
    .Net 4.0
    Posts
    39

    Re: Extending Generic Control

    I didn't think of Reflection, so thanks for the suggestion. I see nothing in PropertyInfo that allows the addition of a property to a particular type.

    Where class CustomControl: Control....

    Casting (Label)newCustomControl does not work.

    Casting (CustomControl)newLabel does not work, either.

  4. #4
    Join Date
    Feb 2012
    Location
    Strasbourg, France
    Posts
    116

    Re: Extending Generic Control

    Here's my suggestion if it's what you're looking for

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            public Label l = new Label();
    
            private void Form1_Load(object sender, EventArgs e)
            {
                
                l.Text = "iuhiuhmihbmiuhb";
                this.Controls.Add(l);  
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                l.SetControlDescription("this is a description");
                l.Text = (string)l.GetControlDescription();
            }
        }
    }
    
    namespace System.Windows.Forms
    {
        public static class ControlExtensions
        {
            private static object ControlDescription;
            public static object GetControlDescription(this Control control)
            {
                return ControlDescription;
            }
            public static void SetControlDescription(this Control control, object o)
            {
                ControlDescription = o;
            }
        }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured