This website completely moved to new platform. For latest content, visit www.programmingposts.com

Search this Site

8 Sep 2013

USING FONT-DIALOG AND CHANGING TEXTBOX FONT STYLE IN C#.Net and VB.Net

In this post we will see the usage of font-dialog and changing the font style of textbox in our c#.net or vb.net windows application.
for this i am designing the form as shown in the image below.



In this example we are using a font dialog box and showing the font dialog on the button click and  based on the font dialog result, we are changing the font style of a multi line textbox .

The c# code in the code behind file is given below :


using System;
using System.Windows.Forms;

namespace Font_Dialog
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Text = "This is a sample application to explain the usage of font dialog control";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //showing dialogbox and getting result 
            DialogResult result = fontDialog1.ShowDialog();

            //checking wheter ok is pressed in font dialog
            if (result == DialogResult.OK)
            {

                // Setting TextBox font as fontdialog1 result font
                textBox1.Font = fontDialog1.Font;
                //getting the fontname in lblFontName
                lblFontName.Text = fontDialog1.Font.Name.ToString();
                //getting the fontsize in lblFontSize
                lblFontSize.Text = fontDialog1.Font.Size.ToString();
                //getting the fontStyle in lblFontStyle
                lblFontStyle.Text = fontDialog1.Font.Style.ToString();

                // cheking whether underline is checked in fontdialog1 
                if (fontDialog1.Font.Underline == true)
                {
                    chkUnderLine.Checked = true;
                }

                // cheking whether strikeout is checked in fontdialog1
                if (fontDialog1.Font.Strikeout == true)
                {
                    chkStrikeOut.Checked = true;
                }


            }
        }
    }
}

The VB code in the code behind file is given below :



Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        textBox1.Text = "This is a sample application to explain the usage of font dialog control"
    End Sub

    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click


        'showing dialogbox and getting result 
        Dim result As DialogResult = fontDialog1.ShowDialog()

        'checking wheter ok is pressed in font dialog
        If result = DialogResult.OK Then

            ' Setting TextBox font as fontdialog1 result font
            textBox1.Font = fontDialog1.Font
            'getting the fontname in lblFontName
            lblFontName.Text = fontDialog1.Font.Name.ToString()
            'getting the fontsize in lblFontSize
            lblFontSize.Text = fontDialog1.Font.Size.ToString()
            'getting the fontStyle in lblFontStyle
            lblFontStyle.Text = fontDialog1.Font.Style.ToString()

            ' cheking whether underline is checked in fontdialog1 
            If fontDialog1.Font.Underline = True Then
                chkUnderLine.Checked = True
            End If

            ' cheking whether strikeout is checked in fontdialog1
            If fontDialog1.Font.Strikeout = True Then
                chkStrikeOut.Checked = True


            End If
        End If
    End Sub

End Class


Download Source Code