ziwez0
asked on
C# Components
Hi Guys,
im in need of writing a small component, but have no idea where to start i just to to create a small box, can anyone help?
im in need of writing a small component, but have no idea where to start i just to to create a small box, can anyone help?
ASP.NET or Windows?
ASKER
windows
In your project right click, select Add new item, select to create a new custom or user control. The difference is that user controls are combination of already existing controls (just align them, make them act together, etc.) while the custom control is a control you should make yourself.
The user control inherits from the class UserControl in System.Windows.Forms namespace while the custom control inherits directly from the Control class in the same namespace.
The user control inherits from the class UserControl in System.Windows.Forms namespace while the custom control inherits directly from the Control class in the same namespace.
ASKER
ok, so i want to use the custom control,
i know that i have to use the System.Design , System.Drawing,
basicley i want to design my own rectangle box with the following properties
.ColumnWidth =100
.ColumnHeight =200
.BackColor = SystemColors.Desktop
.BorderColor = Colours.Ivory
then save it as a dll and to be able to use it with another piece of code where i can add it on to the contorl box
i know that i have to use the System.Design , System.Drawing,
basicley i want to design my own rectangle box with the following properties
.ColumnWidth =100
.ColumnHeight =200
.BackColor = SystemColors.Desktop
.BorderColor = Colours.Ivory
then save it as a dll and to be able to use it with another piece of code where i can add it on to the contorl box
You need to make the project a class library then and compile it to a dll. Then you can add the control to the toolbox.
You also need to override the OnPaint method of the Control class. For more info see Control.OnPaint in MSDN
You also need to override the OnPaint method of the Control class. For more info see Control.OnPaint in MSDN
ASKER
yep done that
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;
namespace COM.PROJECT.MENU
{
public class Structure : Control
{
private int mainoutlineColumnWidth;
private int mainoutlineColumnHeight;
private Color mainoutlineBackColor;
private Color mainoutlineBorderColor;
public Structure()
{
SetStyle(ControlStyles.All PaintingIn WmPaint | ControlStyles.UserPaint | ControlStyles.Opaque | ControlStyles.DoubleBuffer , true);
SetAllDefaultPropertyValue s();
Enabled = true;
}
private void SetAllDefaultProperyValues ()
{
mainoutlineColumnWidth =150;
mainoutlineColumnHeight = 100;
mainoutlineBackColor = SystemColors.Desktop;
mainoutlineBorderColor = SystemColors.ControlDarkDa rk;
}
protected virtual void DrawAndFillControlRect(Gra phics g)
{
Brush b = new SolidBrush(BackColor);
try
{
Rectangle Rect = new Rectangle(0,0,ClientRectan gle.Width -1,ClientRectangle.Height -1);
g.FillRectangle(b,Rect);
}
finally
{
b.Dispose();
}
}
protected virtual void DrawBorder(Graphics g)
{
ControlPaint.DrawBorder(g, ClientRect angle,Syst emColors.W indowFrame ,ButtonBor derStyle.S olid);
}
protected override void OnPaint(PaintEventArgs e)
{
DrawControl(e.Graphics);
}
protected virtual void DrawHeader(Graphics g)
{
Brush b = new SolidBrush(mainoutline.Bac kColor);
try
{
Rectangle Rect = new Rectangle();
g.FillRectangle(b,Rect);
g.DrawLine(mainoutlineColu mnHeight);
g.DrawLine(mainoutlineColu mnWidth);
}
}
}
//im missing somehting tho
}
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;
namespace COM.PROJECT.MENU
{
public class Structure : Control
{
private int mainoutlineColumnWidth;
private int mainoutlineColumnHeight;
private Color mainoutlineBackColor;
private Color mainoutlineBorderColor;
public Structure()
{
SetStyle(ControlStyles.All
SetAllDefaultPropertyValue
Enabled = true;
}
private void SetAllDefaultProperyValues
{
mainoutlineColumnWidth =150;
mainoutlineColumnHeight = 100;
mainoutlineBackColor = SystemColors.Desktop;
mainoutlineBorderColor = SystemColors.ControlDarkDa
}
protected virtual void DrawAndFillControlRect(Gra
{
Brush b = new SolidBrush(BackColor);
try
{
Rectangle Rect = new Rectangle(0,0,ClientRectan
g.FillRectangle(b,Rect);
}
finally
{
b.Dispose();
}
}
protected virtual void DrawBorder(Graphics g)
{
ControlPaint.DrawBorder(g,
}
protected override void OnPaint(PaintEventArgs e)
{
DrawControl(e.Graphics);
}
protected virtual void DrawHeader(Graphics g)
{
Brush b = new SolidBrush(mainoutline.Bac
try
{
Rectangle Rect = new Rectangle();
g.FillRectangle(b,Rect);
g.DrawLine(mainoutlineColu
g.DrawLine(mainoutlineColu
}
}
}
//im missing somehting tho
}
I think you should call base.OnPaint before your own drawing (although I am not sure)
Why do you think you miss something?
Why do you think you miss something?
ASKER
protected override void OnPaint(PaintEventArgs e)
{
DrawControl(e.Graphics);
}
protected virtual void DrawControl(Graphics g)
{
Brush b = new SolidBrush(SystemColors.De sktop);
try
{
Rectangle Rect = new Rectangle(0,0,ClientRectan gle.Width -1,ClientRectangle.Height -1);
g.FillRectangle(b,Rect);
}
finally
{
b.Dispose();
}
thanks but have sorted it
{
DrawControl(e.Graphics);
}
protected virtual void DrawControl(Graphics g)
{
Brush b = new SolidBrush(SystemColors.De
try
{
Rectangle Rect = new Rectangle(0,0,ClientRectan
g.FillRectangle(b,Rect);
}
finally
{
b.Dispose();
}
thanks but have sorted it
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.