Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create Common Properties for Custom Controls

Status
Not open for further replies.

svagelis

Programmer
May 2, 2001
121
GR
Hello,
i m implementing a user interface with some custom controls. Each of those controls inherits from its base class (textbox, listbox etc) plus some custom properties. Those properties are common to each of those controls (DataType, IsRequired, IsListOfValues, MinValue, MaxValue ..... etc). I ve implemented my customTextbox and now i have all the rest.Well, How can i reuse the code what implements those properties (inside customTextBox) and define them in the other custom classes?
It sounds to me like mutli-inheritance, because i want each control's class to inherit from control's base class and from another class what contains the common properties...
Any help?

Thanks in advance...!
 
You can only inherit from one base class. But you can use Interfaces. Interfaces are basically a definition of routines which implementing objects must then define and provide the functionality. Simply put, you create an interface and tell it that DataType, IsRequired, IsListOfValues properties exist. Then when you implement it with your custom classes, those classes will be required to create those properties. Here is a simple example. To save you time, you will notice in Visual Studio that as soon as you type the Implements CustomProperties statement, your classes will automatically create the shell of the code for the properties.

Code:
Interface CustomProperties
    Property DataType() As String
    Property IsValid() As Boolean
End Interface

Class TextBoxCustom
    Inherits System.Windows.Forms.TextBox
    Implements CustomProperties

    Public Property DataType() As String Implements CustomProperties.DataType
        Get

        End Get
        Set(ByVal value As String)

        End Set
    End Property

    Public Property IsValid() As Boolean Implements CustomProperties.IsValid
        Get

        End Get
        Set(ByVal value As Boolean)

        End Set
    End Property
End Class

Class ListBoxCustom
    Inherits System.Windows.Forms.ListBox
    Implements CustomProperties

    Public Property DataType() As String Implements CustomProperties.DataType
        Get

        End Get
        Set(ByVal value As String)

        End Set
    End Property

    Public Property IsValid() As Boolean Implements CustomProperties.IsValid
        Get

        End Get
        Set(ByVal value As Boolean)

        End Set
    End Property
End Class
 
Thanks for the reply RiverGuy,

but as i can see it, the only benefit that i would have if use interfaces is that i my custom controls would have the same properties in order my project to work. but i will have to describe those properties to each of my controls. I was wondering if i could describe those properties once and include them to each control
 
If it is only one field for instance you can use the tag common to all controls. Otherwise what RiverGuy showed is the easiest way to implement what you want.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top