⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usercontrol1.vb

📁 <Visual Basic 数据库开发实例精粹(第二版)>一书首先介绍了Visual Basic(简称VB)开发的技巧和重点技术
💻 VB
字号:
Public Class UserControl1
    Inherits System.Windows.Forms.UserControl

  Private m_EditBox As RichTextBox

  Public Property AttathedTextBox() As RichTextBox
    Get
      Return m_EditBox
    End Get
    Set(ByVal Value As RichTextBox)
      m_EditBox = Value
    End Set
  End Property
#Region " Windows 窗体设计器生成的代码 "

  Public Sub New()
    MyBase.New()

    '该调用是 Windows 窗体设计器所必需的。
    InitializeComponent()

    '在 InitializeComponent() 调用之后添加任何初始化

  End Sub

  'UserControl 重写 dispose 以清理组件列表。
  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

  'Windows 窗体设计器所必需的
  Private components As System.ComponentModel.IContainer

  '注意: 以下过程是 Windows 窗体设计器所必需的
  '可以使用 Windows 窗体设计器修改此过程。
  '不要使用代码编辑器修改它。
  Friend WithEvents palToolbox As System.Windows.Forms.Panel
  Friend WithEvents cboFontName As System.Windows.Forms.ComboBox
  Friend WithEvents cboFontSize As System.Windows.Forms.ComboBox
  Friend WithEvents btnColor As System.Windows.Forms.Button
  Friend WithEvents btnUnderLine As System.Windows.Forms.Button
  Friend WithEvents btnItalic As System.Windows.Forms.Button
  Friend WithEvents btnBold As System.Windows.Forms.Button
  Friend WithEvents ColorDialog1 As System.Windows.Forms.ColorDialog
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(UserControl1))
    Me.palToolbox = New System.Windows.Forms.Panel
    Me.cboFontName = New System.Windows.Forms.ComboBox
    Me.cboFontSize = New System.Windows.Forms.ComboBox
    Me.btnColor = New System.Windows.Forms.Button
    Me.btnUnderLine = New System.Windows.Forms.Button
    Me.btnItalic = New System.Windows.Forms.Button
    Me.btnBold = New System.Windows.Forms.Button
    Me.ColorDialog1 = New System.Windows.Forms.ColorDialog
    Me.palToolbox.SuspendLayout()
    Me.SuspendLayout()
    '
    'palToolbox
    '
    Me.palToolbox.BackColor = System.Drawing.Color.Transparent
    Me.palToolbox.Controls.Add(Me.cboFontName)
    Me.palToolbox.Controls.Add(Me.cboFontSize)
    Me.palToolbox.Controls.Add(Me.btnColor)
    Me.palToolbox.Controls.Add(Me.btnUnderLine)
    Me.palToolbox.Controls.Add(Me.btnItalic)
    Me.palToolbox.Controls.Add(Me.btnBold)
    Me.palToolbox.Dock = System.Windows.Forms.DockStyle.Top
    Me.palToolbox.Location = New System.Drawing.Point(0, 0)
    Me.palToolbox.Name = "palToolbox"
    Me.palToolbox.Size = New System.Drawing.Size(376, 27)
    Me.palToolbox.TabIndex = 34
    '
    'cboFontName
    '
    Me.cboFontName.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
    Me.cboFontName.Location = New System.Drawing.Point(2, 2)
    Me.cboFontName.Name = "cboFontName"
    Me.cboFontName.Size = New System.Drawing.Size(134, 20)
    Me.cboFontName.TabIndex = 25
    '
    'cboFontSize
    '
    Me.cboFontSize.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
    Me.cboFontSize.Location = New System.Drawing.Point(144, 2)
    Me.cboFontSize.Name = "cboFontSize"
    Me.cboFontSize.Size = New System.Drawing.Size(54, 20)
    Me.cboFontSize.TabIndex = 26
    '
    'btnColor
    '
    Me.btnColor.FlatStyle = System.Windows.Forms.FlatStyle.Popup
    Me.btnColor.Image = CType(resources.GetObject("btnColor.Image"), System.Drawing.Image)
    Me.btnColor.Location = New System.Drawing.Point(304, 1)
    Me.btnColor.Name = "btnColor"
    Me.btnColor.Size = New System.Drawing.Size(22, 22)
    Me.btnColor.TabIndex = 30
    '
    'btnUnderLine
    '
    Me.btnUnderLine.FlatStyle = System.Windows.Forms.FlatStyle.Popup
    Me.btnUnderLine.Image = CType(resources.GetObject("btnUnderLine.Image"), System.Drawing.Image)
    Me.btnUnderLine.Location = New System.Drawing.Point(272, 1)
    Me.btnUnderLine.Name = "btnUnderLine"
    Me.btnUnderLine.Size = New System.Drawing.Size(22, 22)
    Me.btnUnderLine.TabIndex = 29
    '
    'btnItalic
    '
    Me.btnItalic.FlatStyle = System.Windows.Forms.FlatStyle.Popup
    Me.btnItalic.Image = CType(resources.GetObject("btnItalic.Image"), System.Drawing.Image)
    Me.btnItalic.Location = New System.Drawing.Point(240, 1)
    Me.btnItalic.Name = "btnItalic"
    Me.btnItalic.Size = New System.Drawing.Size(22, 22)
    Me.btnItalic.TabIndex = 28
    '
    'btnBold
    '
    Me.btnBold.FlatStyle = System.Windows.Forms.FlatStyle.Popup
    Me.btnBold.Image = CType(resources.GetObject("btnBold.Image"), System.Drawing.Image)
    Me.btnBold.Location = New System.Drawing.Point(208, 1)
    Me.btnBold.Name = "btnBold"
    Me.btnBold.Size = New System.Drawing.Size(22, 22)
    Me.btnBold.TabIndex = 27
    '
    'UserControl1
    '
    Me.Controls.Add(Me.palToolbox)
    Me.Name = "UserControl1"
    Me.Size = New System.Drawing.Size(376, 28)
    Me.palToolbox.ResumeLayout(False)
    Me.ResumeLayout(False)

  End Sub

#End Region


#Region "窗体初始化"

  Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    '初始化字体下拉列表框
    InitFontNameCombo(cboFontName)
    InitFontSizeCombo(cboFontSize)

  End Sub

  '初始化字体下拉列表框
  Private Sub InitFontNameCombo(ByRef cbo As ComboBox)
    Dim fontFamilies() As FontFamily
    Dim FontInstalled As New System.Drawing.Text.InstalledFontCollection
    fontFamilies = FontInstalled.Families
    cbo.DataSource = fontFamilies
    cbo.DisplayMember = "Name"
    cbo.ValueMember = "Name"
    cbo.SelectedValue = Me.Font.FontFamily.Name

  End Sub

  '初始化字号下拉列表框
  Private Sub InitFontSizeCombo(ByRef cbo As ComboBox)
    Dim i As Integer
    Dim Ary As New ArrayList
    Ary.Capacity = 20
    For i = 8 To 22
      Ary.Add(i)
    Next
    cbo.DataSource = Ary
    cbo.SelectedIndex = 4

  End Sub
#End Region

#Region "文本格式工具箱控件事件响应代码"

  '字体格式按钮响应代码,包知btnBold, btnItalic, btnUnderLine, btnColor
  Private Sub btnFontStyle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                                                                    Handles btnBold.Click, _
                                                                    btnItalic.Click, _
                                                                    btnUnderLine.Click, _
                                                                    btnColor.Click
    Dim fs As FontStyle
    Dim ft As Font

    If m_EditBox Is Nothing Then
      Return
    End If

    '当选种不同种字体时,SelectionFont为Nothing
    If m_EditBox.SelectionFont Is Nothing Then
      fs = FontStyle.Regular
      ft = m_EditBox.Font
    Else
      fs = m_EditBox.SelectionFont.Style
      ft = m_EditBox.SelectionFont
    End If

    Dim btnSender As Button = DirectCast(sender, Button)

    Select Case btnSender.Name
      Case btnBold.Name
        m_EditBox.SelectionFont = New Font(ft, FontStyle.Bold Xor fs)
      Case btnItalic.Name
        m_EditBox.SelectionFont = New Font(ft, FontStyle.Italic Xor fs)
      Case btnUnderLine.Name
        m_EditBox.SelectionFont = New Font(ft, FontStyle.Underline Xor fs)
      Case btnColor.Name
        If ColorDialog1.ShowDialog() = DialogResult.OK Then
          m_EditBox.SelectionColor = ColorDialog1.Color
        End If
    End Select

    m_EditBox.Focus()

  End Sub

  '字体下拉列表框
  Private Sub cboFontName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboFontName.SelectedIndexChanged
    Dim emSize As Single
    If m_EditBox Is Nothing Then
      Return
    End If

    If m_EditBox.SelectionFont Is Nothing Then
      emSize = cboFontSize.SelectedValue
    Else
      emSize = m_EditBox.SelectionFont.Size
    End If
    m_EditBox.SelectionFont = New Font(cboFontName.SelectedItem.ToString, emSize)

    m_EditBox.Focus()

  End Sub

  '字号下拉列表框
  Private Sub cboFontSize_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboFontSize.SelectedIndexChanged
    Dim emSize As Single
    If m_EditBox Is Nothing Then
      Return
    End If

    emSize = CSng(cboFontSize.SelectedItem)
    If emSize < 1 Then
      emSize = 9
    End If
    m_EditBox.SelectionFont = New Font(m_EditBox.SelectionFont.FontFamily, emSize)

    m_EditBox.Focus()

  End Sub

#End Region
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -