Solved

C# Components

Posted on 2004-03-24
10
147 Views
Last Modified: 2010-04-15
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?

0
Comment
Question by:ziwez0
  • 4
  • 4
10 Comments
 
LVL 20

Expert Comment

by:TheAvenger
ID: 10665817
ASP.NET or Windows?
0
 
LVL 2

Author Comment

by:ziwez0
ID: 10665826
windows
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 10665842
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.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 2

Author Comment

by:ziwez0
ID: 10665897
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



0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 10666337
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
0
 
LVL 2

Author Comment

by:ziwez0
ID: 10666397
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
}
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 10666718
I think you should call base.OnPaint before your own drawing (although I am not sure)

Why do you think you miss something?
0
 
LVL 2

Author Comment

by:ziwez0
ID: 10666884
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
0
 
LVL 1

Accepted Solution

by:
DarthMod earned 0 total points
ID: 11266129
PAQed, with points refunded (200)

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question