Click here to Skip to main content
13,096,256 members (52,373 online)
Click here to Skip to main content
Add your own
alternative version


32 bookmarked
Posted 16 Oct 2005

Adding custom dialogs to your applications

, 16 Oct 2005
Rate this:
Please Sign up or sign in to vote.
The article introduces a base dialog form class, which can be used to create all kinds of OK/Cancel type of dialogs. It also gives the basics of custom control creation and form inheritance.

Sample Image - CustomDialogs.png


Dialogs are one of the most widely used types of UI windows. One can hardly imagine Windows application without dialogs. Some of them are easy enough like message boxes with several buttons. Their functionality is pretty much covered by the .NET MessageBox class. Some are more complex, like file save/load dialogs or printer settings. These also have their corresponding .NET classes. But what if we need some other form of dialog, not covered by MessageBox or standard dialog controls? In real life applications, the need for custom dialogs arises far too often. Of course, this is a simple matter of putting the necessary controls on the form, adding OK and Cancel buttons, and voila! But why do the same chore over and over again? Let's make the base dialog class and reuse it through inheritance. Isn't it what the OOP is all about?

BaseDialog class

In the beginning, let's ask ourselves what we want of the basic dialog.

First, we need it to have OK and Cancel buttons. Let's put them to the bottom left of the form.

Second, let's draw a line separating them from the rest of the form. Oops, there is no line control in .NET. Never mind, we will make this control ourselves, and learn the basics of custom control creation in the process.

Third, we need to update the layout of these controls in case of resizing of the inheriting form.

protected override void OnLoad(EventArgs e) {
  base.OnLoad (e);
protected override void OnResize(EventArgs e) {
  base.OnResize (e);
  Line1.Location = new Point(6, this.Size.Height - 64);
  Line1.Size = new Size(this.Size.Width - 18, 2);
  int okx = this.Size.Width - cbOk.Size.Width - 14;
  if(cbCancel.Visible && this.Visible)
    okx -= cbCancel.Size.Width + 4;
  cbOk.Location = new Point(okx, this.Size.Height - cbOk.Size.Height - 32);
  cbCancel.Location = new Point(this.Size.Width - 
           cbCancel.Size.Width - 14, 
           this.Size.Height - cbCancel.Size.Height - 32);

And, finally, the most important thing. The inheritors should be able to add some custom actions when the dialog completes, either with DialogResult.Ok or DialogResult.Cancel. These could be some validating actions, with the ability to prevent dialog completion. protected virtual methods with boolean return value will serve this purpose just nice.

private void cbOk_Click(object sender, System.EventArgs e) {

private void cbCancel_Click(object sender, System.EventArgs e) {

protected void ExitOk() { 
  if(cbOk.Enabled==true) {
    if(OnOk()) {
      this.DialogResult = DialogResult.OK;

protected void ExitCancel() {
  if(cbCancel.Enabled==true) {
    if(OnCancel()) {
      this.DialogResult = DialogResult.Cancel;

protected virtual bool OnOk() {
  return true;

protected virtual bool OnCancel() {
  return true;

Line Control

That one is easy as 1-2-3.

  1. Inherit from System.Windows.Forms.Control.
  2. Paint it ourselves.
    protected override void OnPaint(PaintEventArgs e) {
  3. Repaint it on resize.
    protected override void OnResize(EventArgs e) {
      // height shouldn't change, only length changes
      this.Height = 6;

Here goes the painting:

private void Redraw(Graphics g) {
  // drawing dark edge of line
  Pen pen = new Pen(SystemColors.ControlDark, 1);
  Point p1 = new Point(1, 2);
  Point p2 = new Point(this.Width - 2, 2);
  g.DrawLine(pen, p1, p2);
  // drawing bright edge of line
  pen = new Pen(SystemColors.ControlLightLight, 1);
  p1 = new Point(1, 3);
  p2 = new Point(this.Width - 2, 3);
  g.DrawLine(pen, p1, p2);
  p1 = new Point(this.Width - 2, 2);
  g.DrawLine(pen, p1, p2);

How to use

Now dialog creation becomes a pleasure. Add an inherited form to your project (choose BaseDialog to inherit from). Add necessary controls and code. Don't forget to make a good use of the Anchor property for added controls. Resize the form to the preferred size. Override OnOk and OnCancel methods to add validation code if necessary.

Add the calling code to the application form. For example:

private void button1_Click(object sender, System.EventArgs e) {
  SampleDialog sd = new SampleDialog();
  if(sd.ShowDialog()==DialogResult.OK) {
    // ...

Now some exercises:

  • Try to make a dialog without a Cancel button.
  • Try to make a Yes/No dialog.
  • Try to hide a line and put your controls in a groupbox instead.

All this can be done without making any changes to BaseDialog.


  • 16th October 2005 - article submitted.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
Russian Federation Russian Federation
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralFor the uninitiated Pin
MaxHacker20-Oct-05 9:48
memberMaxHacker20-Oct-05 9:48 
GeneralRe: For the uninitiated Pin
yfoulon20-Oct-05 22:41
memberyfoulon20-Oct-05 22:41 
AnswerRe: For the uninitiated Pin
miklovan1-Nov-05 18:15
membermiklovan1-Nov-05 18:15 
Generalinterest Pin
yfoulon18-Oct-05 8:30
memberyfoulon18-Oct-05 8:30 
GeneralRe: interest Pin
miklovan18-Oct-05 20:24
membermiklovan18-Oct-05 20:24 
GeneralNot much need for base dialog class. Pin
wout de zeeuw17-Oct-05 3:33
memberwout de zeeuw17-Oct-05 3:33 
GeneralRe: Not much need for base dialog class. Pin
miklovan17-Oct-05 4:47
membermiklovan17-Oct-05 4:47 
QuestionSourcecode? Pin
AxelM16-Oct-05 20:56
memberAxelM16-Oct-05 20:56 
AnswerRe: Sourcecode? Pin
miklovan16-Oct-05 21:25
membermiklovan16-Oct-05 21:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170813.1 | Last Updated 16 Oct 2005
Article Copyright 2005 by miklovan
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid