Tuesday, July 28, 2009

ColorDialog and FontDialog in .Net Windows Forms

ColorDialog displays a list of colors and returns a property containing the selected color.
FontDialog enables the user to select a font and set its properties such as size, style, etc.























All the dialog boxes in .Net (ColorDialog, FontDialog, FolderBrowserDialog, FontDialog, PageSetupDialog and PrintDialog) inherit from System.Windows.Forms.CommonDialog class.

The ShowDialog method of CommonDialog class is used for running a dialog. We are also checking whether the Ok button is dialog is selected. If so we are changing the Back ground color of the form in the case of first button and the font the whole form in the case of second button.

private void btnColor_Click(object sender, EventArgs e)
{
if (colorDialog1.ShowDialog() == DialogResult.OK)
this.BackColor = colorDialog1.Color;
}

private void btnFont_Click(object sender, EventArgs e)
{
if (fontDialog1.ShowDialog() == DialogResult.OK)
this.Font = fontDialog1.Font;
}

No comments:

Post a Comment