October 30, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Graphics Programming Using C#

  • July 30, 2002
  • By Anand Narayanaswamy
  • Send Email »
  • More Articles »

Working with Dialog boxes

Dialog boxes are an integral part in any GUI based application. You come across such boxes when you open a file, save a file, pick fonts, pick colors, and so on. These boxes are pre-defined. That means, you only have to apply the relevant coding and rest of the work is done by the .NET runtime. This is a cool way to pick your selection!

If you have ever done Visual Basic or Java programming, you should already be aware of predefined dialog boxes like ColorDialog, FontDialog etc. You may have seen the color dialog box many times if you had used Microsoft Paint. In C#, you can let your user choose a color by applying the standard ColorDialog class. First, you have to create an object of ColorDialog class as shown below:

ColorDialog cd = new ColorDialog();

Using the created object call the ShowDialog() method to display the color dialog box. Finally, invoke the Color property and apply it appropriately as shown in Listing 2:

Listing 2:

using System;
using System.Drawing;
using System.Windows.Forms;

public class Clr:Form  {
  Button b1 = new Button();
  ColorDialog clg = new ColorDialog();

  public Clr()  {
    b1.Click += new EventHandler(b1_click);
    b1.Text = "OK";

    this.Controls.Add(b1);
  }

  public void b1_click(object sender, EventArgs e) {
    clg.ShowDialog();
    this.BackColor = clg.Color;
  }

  public static void Main()  {
    Application.Run(new Clr());
  }
}

In Listing 2, the background color of the form will change as you select a color from the dialog box just like Windows Paint program, where you pick a color for drawing and filling.

Working with FontDialog Box

FontDialog boxes (See Figure 1) enable you to choose the font name, size, styles, and more. You can easily create a font selection dialog box by following the same steps as in the previous listing and by making some minor changes. Listing 3 shown below examines the usage of this useful dialog box:

Listing 3:

using System;
using System.Drawing;
using System.Windows.Forms;

public class Fnt:Form  {
  Button b1 = new Button();
  TextBox tb = new TextBox();
  FontDialog clg = new FontDialog();

  public Fnt()  {
    b1.Click += new EventHandler(b1_click);
    b1.Text = "OK";
    tb.Location = new Point(50,50);

    this.Controls.Add(b1);
    this.Controls.Add(tb);
  }

  public void b1_click(object sender, EventArgs e) {
    clg.ShowDialog();
    tb.Font = clg.Font;
  }

  public static void Main()  {
    Application.Run(new Fnt());
  }
}

Figure 1: Font Selection Box

After executing Listing 3, pick out your font from the dialog box and then type something on the text box. The font inside the text box should reflect your selection.

Summary

In this part of the article, you've seen how to paint text onto the Form. You can try your own font names, sizes and colors by making changes to the above listings. In this part, you have seen a lot of methods along with examples. You have hopely learned how to incorporate these methods into your own program. You also examined some of the pre-defined dialog boxes available to you from the .NET Framework. In the next article you will learn about one of the advanced namespaces of the .NET Framework, System.Drawing2D.

About the Author

Anand Narayanaswamy works as a freelance Web developer and technical writer. He also runs and maintains learnxpress.com, and provides free technical support to users worldwide. He can be reached at anand@learnxpress.com.

# # #





Page 3 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel