How to extend controls with base class?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Franna Tech
    New Member
    • Jan 2011
    • 2

    How to extend controls with base class?

    Hi

    I have a BaseClass

    Code:
    public class BaseClass
    {
        [XmlIgnore]
        public PropertyGrid PropertyGridControl
        [XmlAttribute("Name")]
        public string Name
        { get; set; }
    }
    Then I have multiple classes that I want to derive from controls and inherit / implement the BaseClass.

    Code:
    public class MyLabel : Label
    {   
    }
    
    public class MyPanel : Panel
    {
    }
    There is no multiple inheritance in c#, so I have to derive form the control itself, but how do I then extend my class to implement the BaseClass.

    I need to be able to manage common properties of different controls for xml serialization purposes.

    What would be the best way to do this?

    I have thought of creating a BaseClass like
    Code:
    public class BaseClass
    {
        [XmlIgnore]
        public PropertyGrid PropertyGridControl
        [XmlAttribute("Name")]
        public string Name
        { get; set; }
    
        [XmlIgnore]
        protected Control ControlObj;
    }
    and then inherit from that and create a new control like
    Code:
    public class MyLabel : BaseClass
    {   
        public MyLabel() : base()
        {
            ControlObj = new Label();
        }
    }
    
    public class MyPanel : BaseClass
    {   
        public MyPanel() : base()
        {
            ControlObj = new Panel();
        }
    }
    But this way is creating lots of work downstream.

    Is there a better way to implement this scenario?

    Thanks
    Franna
    Last edited by Niheel; Feb 27 '11, 06:04 AM.
  • Bryan Cheung
    New Member
    • Nov 2010
    • 55

    #2
    Have you looked at interfaces? maybe a possibility there?

    Comment

    • Franna Tech
      New Member
      • Jan 2011
      • 2

      #3
      Hi Bryan

      Thanks, yes, I have created an interface and implemented this in my subclasses with an Extension class as follows:

      Code:
      public interface IDesignBaseControl
      {
              
              void Initialize();
      
              string Name { get; set; }
      }
      
      public static class DesignBaseControlExtension
      {
              public static void InitializeBase(this IDesignBaseControl bse)
              {
                  // TODO: Base Initialization
                  bse.BackColor = Color.Beige;
              }
      
              public static void ReadXml(this IDesignBaseControl bse, System.Xml.XmlReader reader)
              {
                  throw new NotImplementedException();
              }
      
              public static void WriteXml(this IDesignBaseControl bse, System.Xml.XmlWriter writer)
              {
                  writer.WriteAttributeString("Name", bse.Name);
              }
      
              public static System.Xml.Schema.XmlSchema GetSchema(this IDesignBaseControl bse)
              {
                  return null;
              }
      }
      
      public class DesignLabel : Label, IDesignBaseControl, IXmlSerializable
      {
              public DesignLabel()
                  : base()
              {
                  Initialize();
              }
      
              public void Initialize()
              {
                  this.InitializeBase();
              }
      
              public void ReadXml(System.Xml.XmlReader reader)
              {
                  throw new NotImplementedException();
              }
      
              public void WriteXml(System.Xml.XmlWriter writer)
              {
                  DesignBaseControlExtension.WriteXml(this, writer);
              }
      }
      
      public class DesignPanel : Panel, IDesignBaseControl, IXmlSerializable
      {
              public DesignPanel()
                  : base()
              {
                  Initialize();
              }
      
              public void Initialize()
              {
                  this.InitializeBase();
              }
      
              public void ReadXml(System.Xml.XmlReader reader)
              {
                  throw new NotImplementedException();
              }
      
              public void WriteXml(System.Xml.XmlWriter writer)
              {
                  DesignBaseControlExtension.WriteXml(this, writer);
              }
      }
      I'm not sure if this is industry standard, but it works.

      Franna

      Comment

      Working...