Link to home
Start Free TrialLog in
Avatar of ziwez0
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?

Avatar of TheAvenger
TheAvenger
Flag of Switzerland image

ASP.NET or Windows?
Avatar of ziwez0
ziwez0

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.
Avatar of ziwez0

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



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
Avatar of ziwez0

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.AllPaintingInWmPaint | ControlStyles.UserPaint |      ControlStyles.Opaque | ControlStyles.DoubleBuffer, true);
                  SetAllDefaultPropertyValues();
                  Enabled = true;
            }

            private void SetAllDefaultProperyValues()
            {
                  mainoutlineColumnWidth =150;
                  mainoutlineColumnHeight = 100;
                  mainoutlineBackColor = SystemColors.Desktop;
                  mainoutlineBorderColor = SystemColors.ControlDarkDark;

            }
            protected virtual void DrawAndFillControlRect(Graphics g)
            {
                  Brush b = new SolidBrush(BackColor);
                  try
                  {
                        Rectangle Rect = new Rectangle(0,0,ClientRectangle.Width -1,ClientRectangle.Height -1);
                        g.FillRectangle(b,Rect);
                  }
                  finally
                  {
                        b.Dispose();
                  }
            }
            protected virtual void DrawBorder(Graphics g)
            {
                  ControlPaint.DrawBorder(g,ClientRectangle,SystemColors.WindowFrame,ButtonBorderStyle.Solid);
            }
            protected override void OnPaint(PaintEventArgs e)
            {
                  DrawControl(e.Graphics);
            }
            protected virtual void DrawHeader(Graphics g)
            {
                  
                  Brush b = new SolidBrush(mainoutline.BackColor);
                  try
                  {
                        Rectangle Rect = new Rectangle();
                        
                        g.FillRectangle(b,Rect);
                        g.DrawLine(mainoutlineColumnHeight);
                        g.DrawLine(mainoutlineColumnWidth);
                        
                  }
            }
      }
//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?
Avatar of ziwez0

ASKER

protected override void OnPaint(PaintEventArgs e)
            {
                  DrawControl(e.Graphics);
            }

            protected virtual void DrawControl(Graphics g)
            {
                  Brush b = new SolidBrush(SystemColors.Desktop);
                  try
                  {
                        Rectangle Rect = new Rectangle(0,0,ClientRectangle.Width -1,ClientRectangle.Height -1);
                        g.FillRectangle(b,Rect);
                  }
                  finally
                  {
                        b.Dispose();
                  }

thanks but have sorted it
ASKER CERTIFIED SOLUTION
Avatar of DarthMod
DarthMod
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial