📄 bindingdemoform2.vb
字号:
' 导入命名空间
Imports System.Data.SqlClient
Public Class BindingDemoForm2
Inherits System.Windows.Forms.Form
' 数据集对象的类层级建立
Dim ds As DataSet = New DataSet()
#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 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 TextBoxName As System.Windows.Forms.TextBox
Friend WithEvents TextBoxID As System.Windows.Forms.TextBox
Friend WithEvents CheckBoxGender As System.Windows.Forms.CheckBox
Friend WithEvents DateTimePickerBirthday As System.Windows.Forms.DateTimePicker
Friend WithEvents TextBoxContent As System.Windows.Forms.TextBox
Friend WithEvents lblContent As System.Windows.Forms.Label
Friend WithEvents NumericUpDownSalary As System.Windows.Forms.NumericUpDown
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
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.TextBoxName = New System.Windows.Forms.TextBox
Me.TextBoxID = New System.Windows.Forms.TextBox
Me.CheckBoxGender = New System.Windows.Forms.CheckBox
Me.DateTimePickerBirthday = New System.Windows.Forms.DateTimePicker
Me.TextBoxContent = New System.Windows.Forms.TextBox
Me.lblContent = New System.Windows.Forms.Label
Me.NumericUpDownSalary = New System.Windows.Forms.NumericUpDown
CType(Me.NumericUpDownSalary, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'lblDepartment
'
Me.lblDepartment.AutoSize = True
Me.lblDepartment.Location = New System.Drawing.Point(80, 156)
Me.lblDepartment.Name = "lblDepartment"
Me.lblDepartment.Size = New System.Drawing.Size(35, 17)
Me.lblDepartment.TabIndex = 12
Me.lblDepartment.Text = "部门:"
'
'lblSalary
'
Me.lblSalary.AutoSize = True
Me.lblSalary.Location = New System.Drawing.Point(48, 133)
Me.lblSalary.Name = "lblSalary"
Me.lblSalary.Size = New System.Drawing.Size(60, 17)
Me.lblSalary.TabIndex = 10
Me.lblSalary.Text = "当前工资:"
'
'lblBirthday
'
Me.lblBirthday.AutoSize = True
Me.lblBirthday.Location = New System.Drawing.Point(48, 107)
Me.lblBirthday.Name = "lblBirthday"
Me.lblBirthday.Size = New System.Drawing.Size(60, 17)
Me.lblBirthday.TabIndex = 8
Me.lblBirthday.Text = "出生日期:"
'
'lblAddress
'
Me.lblAddress.AutoSize = True
Me.lblAddress.Location = New System.Drawing.Point(80, 81)
Me.lblAddress.Name = "lblAddress"
Me.lblAddress.Size = New System.Drawing.Size(35, 17)
Me.lblAddress.TabIndex = 6
Me.lblAddress.Text = "地址:"
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(80, 58)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(35, 17)
Me.Label1.TabIndex = 4
Me.Label1.Text = "性别:"
'
'lblName
'
Me.lblName.AutoSize = True
Me.lblName.Location = New System.Drawing.Point(80, 31)
Me.lblName.Name = "lblName"
Me.lblName.Size = New System.Drawing.Size(35, 17)
Me.lblName.TabIndex = 2
Me.lblName.Text = "姓名:"
'
'lblID
'
Me.lblID.AutoSize = True
Me.lblID.Location = New System.Drawing.Point(32, 6)
Me.lblID.Name = "lblID"
Me.lblID.Size = New System.Drawing.Size(72, 17)
Me.lblID.TabIndex = 0
Me.lblID.Text = "身份证号码:"
'
'TextBoxDepartment
'
Me.TextBoxDepartment.Location = New System.Drawing.Point(128, 156)
Me.TextBoxDepartment.Name = "TextBoxDepartment"
Me.TextBoxDepartment.TabIndex = 13
Me.TextBoxDepartment.Text = ""
'
'TextBoxAddress
'
Me.TextBoxAddress.Location = New System.Drawing.Point(128, 81)
Me.TextBoxAddress.Name = "TextBoxAddress"
Me.TextBoxAddress.Size = New System.Drawing.Size(344, 21)
Me.TextBoxAddress.TabIndex = 7
Me.TextBoxAddress.Text = ""
'
'TextBoxName
'
Me.TextBoxName.Location = New System.Drawing.Point(128, 31)
Me.TextBoxName.Name = "TextBoxName"
Me.TextBoxName.TabIndex = 3
Me.TextBoxName.Text = ""
'
'TextBoxID
'
Me.TextBoxID.Location = New System.Drawing.Point(128, 6)
Me.TextBoxID.Name = "TextBoxID"
Me.TextBoxID.TabIndex = 1
Me.TextBoxID.Text = ""
'
'CheckBoxGender
'
Me.CheckBoxGender.Location = New System.Drawing.Point(128, 55)
Me.CheckBoxGender.Name = "CheckBoxGender"
Me.CheckBoxGender.Size = New System.Drawing.Size(16, 19)
Me.CheckBoxGender.TabIndex = 5
'
'DateTimePickerBirthday
'
Me.DateTimePickerBirthday.Location = New System.Drawing.Point(128, 107)
Me.DateTimePickerBirthday.Name = "DateTimePickerBirthday"
Me.DateTimePickerBirthday.TabIndex = 9
'
'TextBoxContent
'
Me.TextBoxContent.ForeColor = System.Drawing.Color.Blue
Me.TextBoxContent.Location = New System.Drawing.Point(127, 184)
Me.TextBoxContent.Multiline = True
Me.TextBoxContent.Name = "TextBoxContent"
Me.TextBoxContent.ScrollBars = System.Windows.Forms.ScrollBars.Both
Me.TextBoxContent.Size = New System.Drawing.Size(349, 88)
Me.TextBoxContent.TabIndex = 15
Me.TextBoxContent.Text = ""
'
'lblContent
'
Me.lblContent.AutoSize = True
Me.lblContent.Location = New System.Drawing.Point(55, 184)
Me.lblContent.Name = "lblContent"
Me.lblContent.Size = New System.Drawing.Size(60, 17)
Me.lblContent.TabIndex = 14
Me.lblContent.Text = "自我介绍:"
'
'NumericUpDownSalary
'
Me.NumericUpDownSalary.DecimalPlaces = 2
Me.NumericUpDownSalary.Location = New System.Drawing.Point(128, 131)
Me.NumericUpDownSalary.Maximum = New Decimal(New Integer() {300000, 0, 0, 0})
Me.NumericUpDownSalary.Name = "NumericUpDownSalary"
Me.NumericUpDownSalary.TabIndex = 11
Me.NumericUpDownSalary.ThousandsSeparator = True
'
'BindingDemoForm2
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(488, 280)
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.TextBoxName)
Me.Controls.Add(Me.TextBoxID)
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "BindingDemoForm2"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "简单的数据绑定---绑定至字段"
CType(Me.NumericUpDownSalary, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub BindingDemoForm2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
' 设定窗体的最小大小
Me.MinimumSize = New Size(496, 400)
' 建立一个连接字符串
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 myCommand As SqlDataAdapter = New SqlDataAdapter(strSql, myConnection)
' 将数据填入数据集
myCommand.Fill(ds, "飞狐工作室")
' 将 TextBox 控件的 Text 属性绑定至数据集 ds 内的 飞狐工作室 数据表的 身份证号码 字段
TextBoxID.DataBindings.Add("Text", ds, "飞狐工作室.身份证号码")
' 将 TextBox 控件的 Text 属性绑定至数据集 ds 内的 飞狐工作室 数据表的 姓名 字段
TextBoxName.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, "飞狐工作室.自我介绍")
' 关闭对数据库的连接
myConnection.Close()
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -