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.
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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
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 |