Resources

Creating custom controls with C#

Lesson 1 - GDI+

System.Drawing Namespace

System.Drawing.Graphics Object

System.Drawing.Graphics myFormGraphics;

myFormGraphics = myForm.CreateGraphics();

Bitmap myImage = new Bitmap("C:\\myImage.bmp");

System.Drawing.Graphics myBmpGraphics;

myBmpGraphics = Graphics.FromImage(myImage);

Coordinates

Point myOrigin = new Point(10,10);

Size mySize = new Size(20,20);

Rectangle myRectangle = new Rectangle(myOrigin, mySize);

Drawing Shapes

Colors, Brushes, Pens

Rendering

SolidBrush myBrush = new SolidBrush(Color.MintCream);

Graphics g = this.CreateGraphics();

Rectangle myRectangle = new Rectangle(0, 0, 30, 20);

g.FillElipse(myBrush, myRectangle);

g.Dispose();

myBrush.Dispose();
GraphicsPath myPath = new GraphicsPath(new Point[] {new Point(1,1), new Point(32,54), new Point(33,5)}, new byte[] {(byte)PathPointType.Start, (byte)PathPointType.Line, (byte)PathPoint.Bezier});

Lesson 2 - Authoring Controls

Inheritance

[System.ComponentModel.Browsable(false)]
public int StockNumber

{

}

Create Inherited Control

public class NumberBox : System.Windows.Forms.TextBox

{
protected override void OnKeyPress(KeyPressEventArgs e)
{
if(char.IsNumber(e.KeyChar) == false)
e.Handled = true;
}
}
public class Wowbutton : System.Windows.Forms.Button  
{
protected override void OnPaint(PaintEventArgs pe)
{
System.Drawing.Drawing2D.GraphicsPath myPath = new System.Drawing.Drawing2D.GraphicsPath();

myPath.AddString("Wow!", Font.Family, (int)Font.Style, 72, new PointF(0,0), StringFormat.GenericDefault);

Region myRegion = new Region(myPath);

this.Region = myRegion;
}
}

Creating UserControl

public color ButtonColor  
{
get
{
return Button1.BackColor;
}

set
{
Button1.BackColor = value;
}
}

Creating Custom Control

protected override void OnPaint(PaintEventArgs e)  
{
Brush aBrush = new SolidBrush(Color.Red);

Rectangle clientRectangle = new Rectangle(new Point(0,0), this.Size);

e.Graphics.FillEllipse(aBrush, clientRectangle);
}

Lesson 3 - Using Controls

Adding to Toolbox

Providing Toolbox Bitmap

[ToolboxBitmap(@"C:\Pasta.bmp")  
public class PastaMaker : Control
{

}

Specify a type - custom control will have same Toolbox bitmap as that of specified type

[ToolboxBitmpa(typeof(Button))]  
public class myButton : Button
{

}

Debugging Control

Licensing

[LicenseProvider(typeof(LicFileLicenseProvider))]  
public class Widget : System.Windows.Forms.Control
{
private License myLicense;

public Widget()
{
myLicense = LicenseManager.Validate(typeof(Widget), this);
}

protected override void Dispose(bool Disposing)
{
if(myLicense != null)
{
myLicense.Dispose();
myLicense = null;
}
}
}

Hosting in IE

<OBJECT id="myControl" classid="http:ControlLibrary.dll#ControlLibrary1.myControl" VIEWASTEXT>

</OBJECT>

Downloads