Hey All!

I have searched around on Google and have found nothing!

I want to know how to make custom controls in C#. But customly designed. For example I don't want to use the custom c# 'Button', but design my own in Photoshop, and bring it in giving it all the applicable states.

Another control I would like to create is a bar that spans the top of the screen containing large buttons, much like that of the Ribbon UI.

Where do I start? I know my way around C# well enough, I really just need a tutorial or video etc to get me started.

Greatly appreciated in advance :D

Recommended Answers

All 3 Replies

You could use a UserControl.
Or you could derive from an existing button class like I did in this code snippet

commented: good work +6

Take a look around google...:
http://www.dreamincode.net/forums/showtopic51374.htm
http://ondotnet.com/pub/a/dotnet/2002/03/18/customcontrols.html

You should create a class called something like "ButtonEx" and start overriding members. To do your own graphics you will want to manually paint the button yourself.

This doesn't work right but it should get you started:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;

namespace daniweb
{
  public class ButtonEx : Button
  {
    private bool _useCustomBackground;

    /// <summary>
    /// Indicates whether a custom background should be used
    /// </summary>
    [DefaultValue(false)]
    public bool UseCustomBackground
    {
      get { return _useCustomBackground; }
      set 
      {
        if (value != _useCustomBackground)
        {
          _useCustomBackground = value;
          this.Invalidate(); //redraw the control
        }
      }
    }

    public ButtonEx()
      : base()
    {

    }

    protected override void OnPaint(PaintEventArgs pevent)
    {
      if (this.UseCustomBackground)
      {
        Pen p = new Pen(Brushes.Red);
        pevent.Graphics.DrawRectangle(p, pevent.ClipRectangle);
      }
      else
      {
        base.OnPaint(pevent);
      }
    }

    protected override void OnPaintBackground(PaintEventArgs pevent)
    {
      if (this.UseCustomBackground)
      {
        Pen p = new Pen(Brushes.Red);
        pevent.Graphics.DrawRectangle(p, pevent.ClipRectangle);
      }
      else
      {
        base.OnPaintBackground(pevent);
      }
    }
  }
}

[edit] Danny's example is better :)

Creating images and loading them you will notice you will either have to include the images as resources, or as files along with your program. Its a lot of trouble, you won't get good results. especially when the control is resized. or if you have non rectangular controls.

Drawing the control by overriding the onPaint method of a control and using gdi is the best way to go about it, as the examples shown before from sknake and dan.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.