VB.Net Tutorial/GUI

Материал из VB Эксперт
Перейти к: навигация, поиск

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.rupilerServices.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.ruponentModel.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

Font dialog showing effects

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.rupilerServices.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.ruponentModel.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

Get selected font from FontDialog

Imports System.IO
Imports System.Windows.Forms
public class FontDialogSelectedFont
   public Shared Sub Main
        Dim FontDB As New FontDialog()
        If (FontDB.ShowDialog() = DialogResult.OK) Then
            Console.WriteLine(FontDB.Font)
        End If
   End Sub
End class

Installed Font Collection

Imports System.Globalization
Imports System.Text
Imports System.Collections
Imports System.ruponentModel
Imports System.Windows.Forms
Imports System.Data
Imports System.Drawing
Imports System.Drawing.Text
Imports System

public class Tester
   public Shared Sub Main
   Dim family As FontFamily
        Dim installedFonts As InstalledFontCollection = New InstalledFontCollection()
        For Each family In installedFonts.Families
            Console.WriteLine(family.Name)
        Next family
   End Sub
End class

List all font family name

Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ruponentModel
Imports System.Windows.Forms
Imports System.Globalization

public class Tester
   public Shared Sub Main
        Dim family As FontFamily
        For Each family In FontFamily.Families
            If family.IsStyleAvailable(FontStyle.Bold) = False Then Exit For
            Console.WriteLine(family.Name)
        Next family

   End Sub
End class
Aharoni
Andalus
Angsana New
AngsanaUPC
Arabic Transparent
Arial
Arial Black
Arial Narrow
Batang
BatangChe
Book Antiqua
Bookman Old Style
Browallia New
BrowalliaUPC
Comic Sans MS
Cordia New
CordiaUPC
Courier New
David
David Transparent
DilleniaUPC
Dotum
DotumChe
Estrangelo Edessa
EucrosiaUPC
Fixed Miriam Transparent
Franklin Gothic Medium
FrankRuehl
FreesiaUPC
Garamond
Gautami
Georgia
Gulim
GulimChe
Gungsuh
GungsuhChe
Haettenschweiler
Impact
IrisUPC
JasmineUPC
Kartika
KodchiangUPC
Latha
Levenim MT
LilyUPC
Lucida Console
Lucida Sans Unicode
Mangal
Marlett
Microsoft Sans Serif
MingLiU
Miriam
Miriam Fixed
Miriam Transparent

Paint all available font families

Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ruponentModel
Imports System.Windows.Forms
Imports System.Data
Imports System.Drawing.Text
public class PaintAllAvailableFonts
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
public class Form1
  Inherits System.Windows.Forms.Form
  Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        Dim g As Graphics = e.Graphics
        g.Clear(Me.BackColor)
        Dim families As FontFamily() = FontFamily.GetFamilies(g)
        Dim familiesFont As Font
        Dim familyString As String
        Dim spacing As Single = 0
        Dim family As FontFamily
        For Each family In families
            familiesFont = New Font(family, 16, FontStyle.Bold)
            familyString = "This is the " + family.Name + "family."
            g.DrawString(familyString, familiesFont, Brushes.Black, New PointF(0, spacing))
            spacing += familiesFont.Height
        Next family
        g.Dispose()
  End Sub
  Public Sub New()
   
    MyBase.New()
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(292, 273)
    Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
  End Sub
End Class

Set Properties for a FontDialog

Imports System.Drawing
Imports System.Windows.Forms
public class FontDialogProperty
   public Shared Sub Main

        Dim FontDialog1 As FontDialog = New System.Windows.Forms.FontDialog
        
        Dim objNewFont As Font
        objNewFont = New Font("Verdana", 12, FontStyle.Underline)
        With FontDialog1
            .Font = objNewFont
            .AllowScriptChange = False
            .ShowColor = True
            .Color = Color.Blue
            .ShowApply = True
            ".ShowEffects = False
        End With
        If FontDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            Console.WriteLine(FontDialog1.Font)
            Console.WriteLine(FontDialog1.Color)
        End If
   End Sub
End class

Use Font dialog to set Label font

Imports System.Drawing.Text
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class UseFontDialogToSetLabelFont
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
Public Class Form1
    Inherits System.Windows.Forms.Form

    Public Sub New()
        MyBase.New()
        InitializeComponent()
    End Sub
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    Private components As System.ruponentModel.IContainer
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents FontDialog1 As System.Windows.Forms.FontDialog
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label
        Me.Button1 = New System.Windows.Forms.Button
        Me.FontDialog1 = New System.Windows.Forms.FontDialog
        Me.SuspendLayout()
        "
        "Label1
        "
        Me.Label1.Location = New System.Drawing.Point(48, 16)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(168, 96)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Font Setting"
        "
        "Button1
        "
        Me.Button1.Location = New System.Drawing.Point(88, 136)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(88, 24)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Change Font"
        "
        "Form1
        "
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(272, 190)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.Label1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        FontDialog1.ShowDialog()
        Label1.Font = FontDialog1.Font
    End Sub
End Class