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

📄 bindingdemoform9.vb

📁 是一个用SQL和VB.NET来做的源码。
💻 VB
字号:
' 导入命名空间
Imports System.Data.SqlClient
Public Class BindingDemoForm9
    Inherits System.Windows.Forms.Form

    ' 数据集对象的类层级建立
    Dim ds As DataSet = New DataSet()

    ' CurrencyManager 对象的类层级声明
    Dim bmFoxStudio As BindingManagerBase

#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 lblHeader As System.Windows.Forms.Label
    Friend WithEvents NumericUpDownSalary As System.Windows.Forms.NumericUpDown
    Friend WithEvents lblContent As System.Windows.Forms.Label
    Friend WithEvents TextBoxContent As System.Windows.Forms.TextBox
    Friend WithEvents DateTimePickerBirthday As System.Windows.Forms.DateTimePicker
    Friend WithEvents CheckBoxGender As System.Windows.Forms.CheckBox
    Friend WithEvents lblDepartment As System.Windows.Forms.Label
    Friend WithEvents lblSalary As System.Windows.Forms.Label
    Friend WithEvents lblBirthday As System.Windows.Forms.Label
    Friend WithEvents lblAddress As System.Windows.Forms.Label
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents lblName As System.Windows.Forms.Label
    Friend WithEvents lblID As System.Windows.Forms.Label
    Friend WithEvents TextBoxDepartment As System.Windows.Forms.TextBox
    Friend WithEvents TextBoxAddress As System.Windows.Forms.TextBox
    Friend WithEvents TextBoxID As System.Windows.Forms.TextBox
    Friend WithEvents ComboBoxName As System.Windows.Forms.ComboBox
    Friend WithEvents TextBoxPosition As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.lblHeader = New System.Windows.Forms.Label
        Me.NumericUpDownSalary = New System.Windows.Forms.NumericUpDown
        Me.lblContent = New System.Windows.Forms.Label
        Me.TextBoxContent = New System.Windows.Forms.TextBox
        Me.DateTimePickerBirthday = New System.Windows.Forms.DateTimePicker
        Me.CheckBoxGender = New System.Windows.Forms.CheckBox
        Me.lblDepartment = New System.Windows.Forms.Label
        Me.lblSalary = New System.Windows.Forms.Label
        Me.lblBirthday = New System.Windows.Forms.Label
        Me.lblAddress = New System.Windows.Forms.Label
        Me.Label1 = New System.Windows.Forms.Label
        Me.lblName = New System.Windows.Forms.Label
        Me.lblID = New System.Windows.Forms.Label
        Me.TextBoxDepartment = New System.Windows.Forms.TextBox
        Me.TextBoxAddress = New System.Windows.Forms.TextBox
        Me.TextBoxID = New System.Windows.Forms.TextBox
        Me.ComboBoxName = New System.Windows.Forms.ComboBox
        Me.TextBoxPosition = New System.Windows.Forms.TextBox
        CType(Me.NumericUpDownSalary, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'lblHeader
        '
        Me.lblHeader.AutoSize = True
        Me.lblHeader.Font = New System.Drawing.Font("宋体", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.lblHeader.ForeColor = System.Drawing.Color.Red
        Me.lblHeader.Location = New System.Drawing.Point(125, 12)
        Me.lblHeader.Name = "lblHeader"
        Me.lblHeader.Size = New System.Drawing.Size(253, 22)
        Me.lblHeader.TabIndex = 54
        Me.lblHeader.Text = "使用 ComboBox 控件浏览数据记录"
        '
        'NumericUpDownSalary
        '
        Me.NumericUpDownSalary.DecimalPlaces = 2
        Me.NumericUpDownSalary.Location = New System.Drawing.Point(144, 174)
        Me.NumericUpDownSalary.Maximum = New Decimal(New Integer() {300000, 0, 0, 0})
        Me.NumericUpDownSalary.Name = "NumericUpDownSalary"
        Me.NumericUpDownSalary.TabIndex = 49
        Me.NumericUpDownSalary.ThousandsSeparator = True
        '
        'lblContent
        '
        Me.lblContent.AutoSize = True
        Me.lblContent.Location = New System.Drawing.Point(71, 230)
        Me.lblContent.Name = "lblContent"
        Me.lblContent.Size = New System.Drawing.Size(60, 17)
        Me.lblContent.TabIndex = 52
        Me.lblContent.Text = "自我介绍:"
        '
        'TextBoxContent
        '
        Me.TextBoxContent.ForeColor = System.Drawing.Color.Blue
        Me.TextBoxContent.Location = New System.Drawing.Point(144, 230)
        Me.TextBoxContent.Multiline = True
        Me.TextBoxContent.Name = "TextBoxContent"
        Me.TextBoxContent.ScrollBars = System.Windows.Forms.ScrollBars.Both
        Me.TextBoxContent.Size = New System.Drawing.Size(429, 89)
        Me.TextBoxContent.TabIndex = 53
        Me.TextBoxContent.Text = ""
        '
        'DateTimePickerBirthday
        '
        Me.DateTimePickerBirthday.Location = New System.Drawing.Point(144, 149)
        Me.DateTimePickerBirthday.Name = "DateTimePickerBirthday"
        Me.DateTimePickerBirthday.TabIndex = 47
        '
        'CheckBoxGender
        '
        Me.CheckBoxGender.Location = New System.Drawing.Point(144, 100)
        Me.CheckBoxGender.Name = "CheckBoxGender"
        Me.CheckBoxGender.Size = New System.Drawing.Size(16, 18)
        Me.CheckBoxGender.TabIndex = 43
        '
        'lblDepartment
        '
        Me.lblDepartment.AutoSize = True
        Me.lblDepartment.Location = New System.Drawing.Point(96, 201)
        Me.lblDepartment.Name = "lblDepartment"
        Me.lblDepartment.Size = New System.Drawing.Size(35, 17)
        Me.lblDepartment.TabIndex = 50
        Me.lblDepartment.Text = "部门:"
        '
        'lblSalary
        '
        Me.lblSalary.AutoSize = True
        Me.lblSalary.Location = New System.Drawing.Point(65, 177)
        Me.lblSalary.Name = "lblSalary"
        Me.lblSalary.Size = New System.Drawing.Size(60, 17)
        Me.lblSalary.TabIndex = 48
        Me.lblSalary.Text = "当前工资:"
        '
        'lblBirthday
        '
        Me.lblBirthday.AutoSize = True
        Me.lblBirthday.Location = New System.Drawing.Point(65, 152)
        Me.lblBirthday.Name = "lblBirthday"
        Me.lblBirthday.Size = New System.Drawing.Size(60, 17)
        Me.lblBirthday.TabIndex = 46
        Me.lblBirthday.Text = "出生日期:"
        '
        'lblAddress
        '
        Me.lblAddress.AutoSize = True
        Me.lblAddress.Location = New System.Drawing.Point(96, 127)
        Me.lblAddress.Name = "lblAddress"
        Me.lblAddress.Size = New System.Drawing.Size(35, 17)
        Me.lblAddress.TabIndex = 44
        Me.lblAddress.Text = "地址:"
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(96, 102)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(35, 17)
        Me.Label1.TabIndex = 42
        Me.Label1.Text = "性别:"
        '
        'lblName
        '
        Me.lblName.AutoSize = True
        Me.lblName.Location = New System.Drawing.Point(102, 51)
        Me.lblName.Name = "lblName"
        Me.lblName.Size = New System.Drawing.Size(184, 17)
        Me.lblName.TabIndex = 40
        Me.lblName.Text = "请选择您所要查看的员工的姓名:"
        '
        'lblID
        '
        Me.lblID.AutoSize = True
        Me.lblID.Location = New System.Drawing.Point(49, 80)
        Me.lblID.Name = "lblID"
        Me.lblID.Size = New System.Drawing.Size(72, 17)
        Me.lblID.TabIndex = 38
        Me.lblID.Text = "身份证号码:"
        '
        'TextBoxDepartment
        '
        Me.TextBoxDepartment.Location = New System.Drawing.Point(144, 199)
        Me.TextBoxDepartment.Name = "TextBoxDepartment"
        Me.TextBoxDepartment.TabIndex = 51
        Me.TextBoxDepartment.Text = ""
        '
        'TextBoxAddress
        '
        Me.TextBoxAddress.Location = New System.Drawing.Point(144, 124)
        Me.TextBoxAddress.Name = "TextBoxAddress"
        Me.TextBoxAddress.Size = New System.Drawing.Size(344, 21)
        Me.TextBoxAddress.TabIndex = 45
        Me.TextBoxAddress.Text = ""
        '
        'TextBoxID
        '
        Me.TextBoxID.Location = New System.Drawing.Point(144, 78)
        Me.TextBoxID.Name = "TextBoxID"
        Me.TextBoxID.TabIndex = 39
        Me.TextBoxID.Text = ""
        '
        'ComboBoxName
        '
        Me.ComboBoxName.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.ComboBoxName.Location = New System.Drawing.Point(331, 49)
        Me.ComboBoxName.Name = "ComboBoxName"
        Me.ComboBoxName.Size = New System.Drawing.Size(152, 20)
        Me.ComboBoxName.TabIndex = 55
        '
        'TextBoxPosition
        '
        Me.TextBoxPosition.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(255, Byte), CType(192, Byte))
        Me.TextBoxPosition.ForeColor = System.Drawing.Color.FromArgb(CType(0, Byte), CType(0, Byte), CType(192, Byte))
        Me.TextBoxPosition.Location = New System.Drawing.Point(126, 336)
        Me.TextBoxPosition.Name = "TextBoxPosition"
        Me.TextBoxPosition.Size = New System.Drawing.Size(333, 21)
        Me.TextBoxPosition.TabIndex = 56
        Me.TextBoxPosition.Text = ""
        Me.TextBoxPosition.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
        '
        'BindingDemoForm9
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(584, 367)
        Me.Controls.Add(Me.TextBoxPosition)
        Me.Controls.Add(Me.ComboBoxName)
        Me.Controls.Add(Me.lblHeader)
        Me.Controls.Add(Me.NumericUpDownSalary)
        Me.Controls.Add(Me.lblContent)
        Me.Controls.Add(Me.TextBoxContent)
        Me.Controls.Add(Me.DateTimePickerBirthday)
        Me.Controls.Add(Me.CheckBoxGender)
        Me.Controls.Add(Me.lblDepartment)
        Me.Controls.Add(Me.lblSalary)
        Me.Controls.Add(Me.lblBirthday)
        Me.Controls.Add(Me.lblAddress)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.lblName)
        Me.Controls.Add(Me.lblID)
        Me.Controls.Add(Me.TextBoxDepartment)
        Me.Controls.Add(Me.TextBoxAddress)
        Me.Controls.Add(Me.TextBoxID)
        Me.Name = "BindingDemoForm9"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "ComboBox 控件---复杂的数据绑定"
        CType(Me.NumericUpDownSalary, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub BindingDemoForm9_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' 设定窗体的最小大小
        Me.MinimumSize = New Size(592, 512)

        ' 建立一个连接字符串
        Dim strConnection As String = "Server=(local)\NetSDK;Database=北风贸易;Integrated Security=SSPI"

        ' 建立一个查询命令字符串
        Dim strSql As String = "SELECT 身份证号码,姓名,员工性别,家庭住址,出生日期,当前工资,部门,自我介绍 FROM 飞狐工作室"

        ' 建立一个数据连接
        Dim myConnection As SqlConnection = _
            New SqlConnection(strConnection)

        ' 建立一个数据适配器以便针对数据源执行 SELECT 语句来提取出要填入数据集的数据记录
        Dim myAD As SqlDataAdapter = New SqlDataAdapter(strSql, myConnection)

        ' 将数据填入数据集
        myAD.Fill(ds, "飞狐工作室")

        ' 将 ComboBox 控件绑定至数据集 ds 中的 飞狐工作室 数据表
        ComboBoxName.DataSource = ds.Tables("飞狐工作室")
        ComboBoxName.DisplayMember = "姓名"

        ' 将 TextBox 控件的 Text 属性绑定至数据集 ds 内的 飞狐工作室 数据表的 身份证号码 字段
        TextBoxID.DataBindings.Add("Text", ds, "飞狐工作室.身份证号码")
        ' 将 CheckBox 控件的 Checked 属性绑定至数据集 ds 内的 飞狐工作室 数据表的 员工性别 字段
        CheckBoxGender.DataBindings.Add("Checked", ds, "飞狐工作室.员工性别")
        ' 将 TextBox 控件的 Text 属性绑定至数据集 ds 内的 飞狐工作室 数据表的 家庭住址 字段
        TextBoxAddress.DataBindings.Add("Text", ds, "飞狐工作室.家庭住址")
        ' 将 DateTimePicker 控件的 Value 属性绑定至数据集 ds 内的 飞狐工作室 数据表的 出生日期 字段
        DateTimePickerBirthday.DataBindings.Add("Value", ds, "飞狐工作室.出生日期")
        ' 将 NumericUpDown 控件的 Value 属性绑定至数据集 ds 内的 飞狐工作室 数据表的 当前工资 字段
        NumericUpDownSalary.DataBindings.Add("Value", ds, "飞狐工作室.当前工资")
        ' 将 TextBox 控件的 Text 属性绑定至数据集 ds 内的 飞狐工作室 数据表的 部门 字段
        TextBoxDepartment.DataBindings.Add("Text", ds, "飞狐工作室.部门")
        ' 将 TextBox 控件的 Text 属性绑定至数据集 ds 内的 飞狐工作室 数据表的 自我介绍 字段
        TextBoxContent.DataBindings.Add("Text", ds, "飞狐工作室.自我介绍")

        ' 取得代表 飞狐工作室 数据表的 CurrencyManager 对象
        bmFoxStudio = Me.BindingContext(ds, "飞狐工作室")

        ' 设定当引发 PositionChanged 事件时便执行事件处理例程 飞狐工作室_PositionChanged
        AddHandler bmFoxStudio.PositionChanged, AddressOf 飞狐工作室_PositionChanged

        ' 设定数据记录当前位置讯息的初值
        TextBoxPosition.Text = String.Format("数据记录:当前位置 {0} 总数 {1}", bmFoxStudio.Position + 1, bmFoxStudio.Count)

        ' 关闭对数据库的连接
        myConnection.Close()
    End Sub

    ' 更新数据记录当前位置的讯息
    Protected Sub 飞狐工作室_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        TextBoxPosition.Text = String.Format("数据记录:当前位置 {0} 总数 {1}", Me.BindingContext(ds, "飞狐工作室").Position + 1, Me.BindingContext(ds, "飞狐工作室").Count)
    End Sub

    Private Sub ComboBoxName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxName.SelectedIndexChanged
        bmFoxStudio.Position = ComboBoxName.SelectedIndex
    End Sub
End Class

⌨️ 快捷键说明

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