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

📄 bindingdemoform14.vb

📁 是一个用SQL和VB.NET来做的源码。
💻 VB
字号:
Public Class BindingDemoForm14
    Inherits System.Windows.Forms.Form

    ' DataTable 对象的类层级建立
    Dim colorSourceTable As DataTable = MakeColorTable()

#Region " Windows Form 设计工具产生的程序代码 "

    Public Sub New()
        MyBase.New()

        '此调用为 Windows Form 设计工具的必要项。
        InitializeComponent()

        '在 InitializeComponent() 调用之后加入所有的初始设定

    End Sub

    'Form 覆盖 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 Form 设计工具的必要项
    Private components As System.ComponentModel.IContainer

    '注意: 以下为 Windows Form 设计工具所需的程序
    '您可以使用 Windows Form 设计工具进行修改。
    '请勿使用程序代码编辑器来修改这些程序。
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents lblFormCaption As System.Windows.Forms.Label
    Friend WithEvents ComboBoxColor As System.Windows.Forms.ComboBox
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.lblFormCaption = New System.Windows.Forms.Label()
        Me.ComboBoxColor = New System.Windows.Forms.ComboBox()
        Me.TextBox1 = New System.Windows.Forms.TextBox()
        Me.SuspendLayout()
        '
        'Label2
        '
        Me.Label2.AutoSize = True
        Me.Label2.Font = New System.Drawing.Font("宋体", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.Label2.Location = New System.Drawing.Point(132, 16)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(157, 24)
        Me.Label2.TabIndex = 4
        Me.Label2.Text = "请选择色彩设定:"
        '
        'lblFormCaption
        '
        Me.lblFormCaption.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.lblFormCaption.Font = New System.Drawing.Font("宋体", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.lblFormCaption.Location = New System.Drawing.Point(80, 176)
        Me.lblFormCaption.Name = "lblFormCaption"
        Me.lblFormCaption.Size = New System.Drawing.Size(520, 23)
        Me.lblFormCaption.TabIndex = 7
        Me.lblFormCaption.Text = "lblFormCaption"
        '
        'ComboBoxColor
        '
        Me.ComboBoxColor.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.ComboBoxColor.Font = New System.Drawing.Font("宋体", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.ComboBoxColor.Location = New System.Drawing.Point(300, 16)
        Me.ComboBoxColor.Name = "ComboBoxColor"
        Me.ComboBoxColor.Size = New System.Drawing.Size(248, 28)
        Me.ComboBoxColor.TabIndex = 5
        '
        'TextBox1
        '
        Me.TextBox1.Font = New System.Drawing.Font("宋体", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.TextBox1.Location = New System.Drawing.Point(80, 128)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(520, 31)
        Me.TextBox1.TabIndex = 6
        Me.TextBox1.Text = "TextBox1"
        '
        'BindingDemoForm14
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 18)
        Me.ClientSize = New System.Drawing.Size(680, 224)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label2, Me.lblFormCaption, Me.ComboBoxColor, Me.TextBox1})
        Me.MinimumSize = New System.Drawing.Size(688, 264)
        Me.Name = "BindingDemoForm14"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "将控件的属性绑定至其它控件的属性"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub BindingDemoForm14_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        BindControlProperties()
    End Sub

    Private Sub BindControlProperties()
        ' 将 ComboBox 控件绑定至 colorSourceTable 数据表的 色彩描述 字段
        ComboBoxColor.DataSource = colorSourceTable
        ComboBoxColor.DisplayMember = "色彩描述"

        ' 在加入新的 Binding 对象之前先移除集合内所有的 Binding 对象
        TextBox1.DataBindings.Clear()
        ' 将 TextBox 控件的 Text、BackColor 与 ForeColor 属性绑定至 DataTable 的字段
        TextBox1.DataBindings.Add("Text", colorSourceTable, "色彩描述")
        TextBox1.DataBindings.Add("BackColor", colorSourceTable, "背景色")
        TextBox1.DataBindings.Add("ForeColor", colorSourceTable, "前景色")

        ' 在加入新的 Binding 对象之前先移除集合内所有的 Binding 对象
        lblFormCaption.DataBindings.Clear()
        ' 将 Label 控件的 Text 属性绑定至窗体的 Text 属性
        lblFormCaption.DataBindings.Add("Text", Me, "Text")
        ' 将 Label 控件的 BackColor 属性绑定至 TextBox 控件的 ForeColor 属性
        lblFormCaption.DataBindings.Add("BackColor", TextBox1, "ForeColor")
        ' 将 Label 控件的 ForeColor 属性绑定至 TextBox 控件的 BackColor 属性
        lblFormCaption.DataBindings.Add("ForeColor", TextBox1, "BackColor")
    End Sub

    Private Function MakeColorTable() As DataTable

        ' 建立一个内含三个字段的 DataTable 对象, 其中两个字段内含 Color 对象。
        Dim colorSourceTable As New DataTable("colorSourceTable")
        colorSourceTable.Columns.Add("色彩描述")
        colorSourceTable.Columns.Add("背景色", GetType(Color))
        colorSourceTable.Columns.Add("前景色", GetType(Color))

        ' 在数据表中加入三条数据记录
        Dim myDataRow As DataRow

        myDataRow = colorSourceTable.NewRow()
        myDataRow("色彩描述") = "红底黄字"
        myDataRow("背景色") = Color.Red
        myDataRow("前景色") = Color.Yellow
        colorSourceTable.Rows.Add(myDataRow)

        myDataRow = colorSourceTable.NewRow()
        myDataRow("色彩描述") = "白底蓝字"
        myDataRow("背景色") = Color.White
        myDataRow("前景色") = Color.Blue
        colorSourceTable.Rows.Add(myDataRow)

        myDataRow = colorSourceTable.NewRow()
        myDataRow("色彩描述") = "紫底水蓝字"
        myDataRow("背景色") = Color.Purple
        myDataRow("前景色") = Color.Cyan
        colorSourceTable.Rows.Add(myDataRow)

        Return colorSourceTable
    End Function
End Class

⌨️ 快捷键说明

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