In this example I create a class library ("package" for the Delphi crowd) named "CustomLabels" with one component named "MarqueeLabel". The MarqueeLabel will simply be a label that scrolls horizontally. The HTML element used is "MARQUEE".
In order to have this done, we have to make two important things:
- Create a class library
- New -> Class library -> specify a name (CustomLabels) (This will create a library that packages our custom components.)
-
Code the control
- Rename Class1.cs to custom name (MarqueeLabel). Rename the constructor as well.
- Add Atozed.Intraweb.dll to References (Yes, you have to have Intraweb for ASP.NET installed.)
- Make the class inherit from Atozed.Intraweb.CompLabel.CustomLabel (The CustomLabel already has most of the properties we need for our label, so there's no point in inheriting from a more basic control)
- Overwrite RenderHTML (RenderHTML is the procedure the engine calls everytime the component has to generate its HTML code. Whatever you make RenderHTML output will be sent to the user's browser)
- Expose some of the CustomLabel's properties
- Add some new properties (marquee direction, speed and scroll amount)
Notes:
- the Browsable attribute must be explicitely set to true for properties reintroduced with 'new'
- the ToolboxItem attribute must be set explicitely to true as well, otherwise the component won't appear in the toolbox
Credits go to Gabriel who's been my personal MSDN and Christina who listened to all I had to say about C# (nothing good). Go ahead and ask Gabriel if you have any questions on .NET, he really knows the stuff. Don't send bad comments to Christina though, use your own wives, girlfriends etc.
Any comments are welcome. In the future there will be a VB.NET example and an example on how to do custom design time painting for the components.
Here's the code (MarqueeLabel.cs):
using System;
using System.Drawing.Design;
using System.ComponentModel;
namespace CustomLabels
{
public enum MarqueeDirection {mdLeft, mdRight};
public enum MarqueeBehavior {mbScroll, mbSlide, mbAlternate};
///
/// Summary description for Class1.
///
[ToolboxItem(true)]
public class MarqueeLabel : Atozed.Intraweb.CompLabel.CustomLabel
{
[Browsable(true)]
new public string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
}
}
[Browsable(true)]
new public bool RawText
{
get
{
return base.RawText;
}
set
{
base.RawText = value;
}
}
private MarqueeDirection FDirection = MarqueeDirection.mdLeft;
public MarqueeDirection Direction
{
get
{
return FDirection;
}
set
{
FDirection = value;
}
}
private MarqueeBehavior FBehavior = MarqueeBehavior.mbScroll;
public MarqueeBehavior Behavior
{
get
{
return FBehavior;
}
set
{
FBehavior = value;
}
}
private int FDelay = 85;
public int Delay
{
get
{
return FDelay;
}
set
{
FDelay = value;
}
}
private int FScrollAmount = 6;
public int ScrollAmount
{
get
{
return FScrollAmount;
}
set
{
FScrollAmount = value;
}
}
public MarqueeLabel()
{
//
// TODO: Add constructor logic here
//
}
public override Atozed.Intraweb.HTMLTagUnit.HTMLTag RenderHTML (Atozed.Intraweb.RenderContext.BaseHTMLComponentContext AContext)
{
Atozed.Intraweb.HTMLTagUnit.HTMLTag Result = new Atozed.Intraweb.HTMLTagUnit.HTMLTag(null);
Result.ClosingTag = Atozed.Intraweb.MarkupLanguageTagUnit.CloseTag.cbTrue;
Result.Tag = "marquee";
string LText;
if (RawText)
LText = Text;
else
LText = TextToHTML(null, Text, true, true);
Result.Contents.AddText(LText, false);
if (WebColor != System.Drawing.Color.Empty)
Result.AddStringParam("BGCOLOR", Atozed.Intraweb.ColorUnit.Unit.ColorToRGBString(WebColor));
Result.AddIntegerParam("DELAY", FDelay);
Result.AddIntegerParam("SCROLLAMOUNT", FScrollAmount);
if (FDirection == MarqueeDirection.mdLeft)
Result.AddStringParam("DIRECTION", "LEFT");
else
Result.AddStringParam("DIRECTION", "RIGHT");
switch (FBehavior)
{
case MarqueeBehavior.mbScroll:
{
Result.AddStringParam("BEHAVIOR", "SCROLL");
break;
}
case MarqueeBehavior.mbSlide:
{
Result.AddStringParam("BEHAVIOR", "SLIDE");
break;
}
case MarqueeBehavior.mbAlternate:
{
Result.AddStringParam("BEHAVIOR", "ALTERNATE");
break;
}
}
return Result;
}
}
}