Font dialog apply action : FontDialog « GUI « VB.Net Tutorial






Font dialog apply action
Imports System.Drawing
Imports System.Windows.Forms

public class FontDialogApplyAction
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
Public Class Form1
    Private Sub btnSetFont_Click(ByVal sender As System.Object, _
     ByVal e As System.EventArgs) Handles btnSetFont.Click

        Dim old_font As Font = Me.Font
        Dim old_color As Color = Me.ForeColor

        dlgFont.ShowColor = True    ' Let the user select font color.   
        dlgFont.ShowEffects = True  ' Let the user select Strikeout and Underline.
        dlgFont.ShowApply = True    ' Display the Apply button.
        dlgFont.Font = Me.Font      ' Set the initial font.

        If dlgFont.ShowDialog() = DialogResult.OK Then
            SetFormFont(dlgFont.Font, dlgFont.Color)
        Else
            SetFormFont(old_font, old_color)
        End If
    End Sub

    Private Sub dlgFont_Apply(ByVal sender As Object, _
     ByVal e As System.EventArgs) Handles dlgFont.Apply
        Dim dlg As FontDialog = DirectCast(sender, FontDialog)
        SetFormFont(dlg.Font, dlg.Color)
    End Sub

    Private Sub SetFormFont(ByVal new_font As Font, ByVal new_color As Color)
        Me.Font = new_font
        Me.ForeColor = new_color
    End Sub
End Class
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Public Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.btnSetFont = New System.Windows.Forms.Button
        Me.Label1 = New System.Windows.Forms.Label
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.dlgFont = New System.Windows.Forms.FontDialog
        Me.SuspendLayout()
        '
        'btnSetFont
        '
        Me.btnSetFont.Location = New System.Drawing.Point(8, 8)
        Me.btnSetFont.Name = "btnSetFont"
        Me.btnSetFont.Size = New System.Drawing.Size(96, 32)
        Me.btnSetFont.TabIndex = 0
        Me.btnSetFont.Text = "Set Font"
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(8, 48)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(35, 13)
        Me.Label1.TabIndex = 1
        Me.Label1.Text = "Label1"
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(8, 80)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(160, 20)
        Me.TextBox1.TabIndex = 2
        Me.TextBox1.Text = "Here's a TextBox"
        '
        'Form1
        '
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None
        Me.ClientSize = New System.Drawing.Size(325, 273)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.btnSetFont)
        Me.Name = "Form1"
        Me.Text = "UseFontDialogWithShowEffects"
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub
    Friend WithEvents btnSetFont As System.Windows.Forms.Button
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents dlgFont As System.Windows.Forms.FontDialog

End Class








14.71.FontDialog
14.71.1.Font dialog apply actionFont dialog apply action
14.71.2.Font dialog showing effectsFont dialog showing effects
14.71.3.Set Properties for a FontDialogSet Properties for a FontDialog
14.71.4.Get selected font from FontDialog
14.71.5.Use Font dialog to set Label fontUse Font dialog to set Label font