ARTICLE

Visual Inheritance Part 2

Posted by Prasad H Articles | C# Language September 26, 2001
We all know that Inheritance means a extending a class with more Features without worrying about the implementation of features of hidden inside the class to be inherited.
Reader Level:

Description :

We all know that Inheritance means a extending a class with more Features without worrying about the implementation of features of hidden inside the class to be inherited. Let's work through a running example of visual inheritance.The Label class under System.Windows.Forms is control to display static Text it hides from the developers how the text appears on the screen when Text property is changed all these stuffs are hidden from the developer.Let's use this feature of Label and put some fancy effects to the Label control in built by inheriting from the Label class.

Let's Construct a useful Label which should can be made blinking always. And expose properties to change the Interval time and provide whether It should change color on blinking.

So when we need the many Label controls to blink what we can Do is simply copy the class BlinkLabel below and use :

BlinkLabel l1=new BlinkLabel() instead of Label after this the BlinkLabel Will behave just like Label but will blink.

How this is achieved.This functionality is achieved by making use of inheritance:

class BlinkLabel:Label

this statement BlinkLabel should have all the features of Label.To make the Blink  a timer is created for 1 second and when the timer elapses the text is displayed and hidden alternatively.Methods are exposed to start and stop The blinking and also expose property to ChangeColor on blinking or not.

Full Source Code:

// Source Code starts
using System;
using System.Drawing;
using System.Windows.Forms;
class BlinkLabel:Label
{
private Timer BlinkTimer;
private string TextToShow;
private bool CanStop;
private bool CanShow;
private bool CanChangeColor=false;
public BlinkLabel(string text):this(text,1000,false)
{
}
public BlinkLabel(string text,int interval):this(text,interval,false)
{
}
public BlinkLabel(string text,int interval,bool changecolor)
{
this.TextToShow=text;
this.BlinkTimer=new Timer();
this.AutoSize=true;
this.BlinkTimer.Interval=interval;
this.BlinkTimer.Tick+=new EventHandler(Timer_Tick);
this.BlinkTimer.Start();
CanStop=
false;
CanShow=
true;
CanChangeColor=changecolor;
}
public void Timer_Tick(object sender,EventArgs e)
{
if(!CanStop)
{
this.Text=(CanShow)?this.TextToShow:"";
CanShow=!CanShow;
}
else
{
this.Text=this.TextToShow;
this.BlinkTimer.Stop();
}
if(CanChangeColor)
{
Color c=GetColor();
if(this.ForeColor!=this.BackColor)
this.ForeColor=c;
}
}
public void Start()
{
this.BlinkTimer.Start();
CanStop=
false;
CanShow=
true;
}
public void Stop()
{
CanStop=
true;
Timer_Tick(null,null);
}
public int Interval
{
get
{
return this.BlinkTimer.Interval;
}
set
{
try
{
this.BlinkTimer.Interval=value;
}
catch(Exception)
{
this.BlinkTimer.Interval=1000;
}
}
}
public bool ChangeColor
{
get
{
return CanChangeColor;
}
set
{
CanChangeColor=
value;
}
}
public Color GetColor()
{
Random rand=
new Random((int)DateTime.Now.Ticks);
int Red=rand.Next(0,255);
int Green=rand.Next(0,255);
int Blue=rand.Next(0,255);
return Color.FromArgb(Red,Green,Blue);
}
}
class LabelDemo:Form
{
BlinkLabel b1;
Button cmdStart;
Button cmdEnd;
Button cmdChange;
Label l1;
TextBox t1;
CheckBox c1;
public LabelDemo()
{
b1=
new BlinkLabel("Welcome to C#",1000,false);
b1.Font=
new Font("Verdana",24);
cmdStart=
new Button();
cmdStart.Text="Start";
cmdEnd=
new Button();
cmdEnd.Text="End";
cmdStart.Click+=new EventHandler(Button_Click);
cmdEnd.Click+=
new EventHandler(Button_Click);
cmdStart.Location=
new Point(b1.Left,b1.Top+b1.Height+20);
cmdEnd.Location=
new Point(cmdStart.Left+cmdStart.Width+15,cmdStart.Top);
l1=
new Label();
l1.AutoSize=
true;
l1.Location=
new Point(cmdEnd.Left+cmdEnd.Width+10,cmdStart.Top);
l1.Text="Interval: ";
t1=
new TextBox();
t1.Location=
new Point(l1.Left+l1.Width+10,cmdStart.Top);
cmdChange=
new Button();
cmdChange.Text="Change";
cmdChange.Click+=
new EventHandler(Button_Click);
cmdChange.Location=
new Point(t1.Left+t1.Width+10,cmdStart.Top);
c1=
new CheckBox();
c1.Location=
new Point(cmdChange.Left+cmdChange.Width+10,cmdStart.Top);
c1.Text="Change Color";
c1.CheckedChanged+=
new EventHandler(Checkbox_Change);
this.Controls.AddRange(new Control[]{b1,cmdStart,cmdEnd,l1,t1,cmdChange,c1});
}
public void Checkbox_Change(object sender,EventArgs e)
{
b1.ChangeColor=c1.Checked;
}
public void Button_Click(object sender,EventArgs e)
{
if(sender==cmdStart)
{
b1.Start();
}
if(sender==cmdEnd)
{
b1.Stop();
}
if(sender==cmdChange)
{
int x=1000;
try
{
x=Int32.Parse(t1.Text);
}
catch(Exception)
{
}
finally
{
b1.Interval=x;
}
}
}
public static void Main()
{
Application.Run(
new LabelDemo());
}
}
// Source Code End

COMMENT USING

Trending up