Posted by: Arimas Artadi | September 2, 2007

How To Create Custom Properties in dotNet C#

creating custom properties is one of many things that I learned while taking APL class last semester. . .

in this post I will share my knowledge.. I hope this post will be worthy enough for all of you

To create a custom properties we just need to modify a new [user control] ( you’ll have to make this [user control] your self )

I use this scenario for my example : ImageButton [user control]. This [user control] will react on [mouse click], [mouse hover] and [mouse leave]. . .

these is the steps to complete this scenario :

1. create a new class as a sub class of class PictureBox, and add 3 image attributes. . . this what it’s should look like

    class ImageButton : System.Windows.Forms.PictureBox 
    { 
        private System.Drawing.Image imageNormal; 
        private System.Drawing.Image imageHover; 
        private System.Drawing.Image imageClick;
    }

2. Now, how exactly we create the custom properties? the “magic” is in this portion of code. . . add this code for each of the attribute we have above. . .

public System.Drawing.Image  ImageNormal{
        get { return imageNormal; }
        set { imageNormal = value; }
}
this's how the entire code soulh look like

    class ImageButton : System.Windows.Forms.PictureBox 
    { 
        private System.Drawing.Image imageNormal; 
        private System.Drawing.Image imageHover; 
        private System.Drawing.Image imageClick;
        public System.Drawing.Image  ImageNormal
        {
              get { return imageNormal; }
              set { imageNormal = value; }
        }

    }
3. Compile the code and then you can use this class’es properties in [design view] of a form or else…

Quite simple isn’t ?

To make this class working ( as an ImageButton ) you’ll need to add some code in event : [MouseHover], [MouseClick], [MouseLeave]. . .

well good luck, and I hope this piece of knowledge can be usefull. . . Next time I’ll try to share my knowledge about [Custom Event]

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

Categories

%d bloggers like this: