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

📄 bindingdemoform15.vb

📁 是一个用SQL和VB.NET来做的源码。
💻 VB
📖 第 1 页 / 共 3 页
字号:
        Me.btnFirst.Image = CType(resources.GetObject("btnFirst.Image"), System.Drawing.Bitmap)
        Me.btnFirst.Location = New System.Drawing.Point(184, 184)
        Me.btnFirst.Name = "btnFirst"
        Me.btnFirst.Size = New System.Drawing.Size(58, 27)
        Me.btnFirst.TabIndex = 86
        '
        'TextBoxCustomerPosition
        '
        Me.TextBoxCustomerPosition.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(255, Byte), CType(192, Byte))
        Me.TextBoxCustomerPosition.ForeColor = System.Drawing.Color.FromArgb(CType(0, Byte), CType(0, Byte), CType(192, Byte))
        Me.TextBoxCustomerPosition.Location = New System.Drawing.Point(192, 144)
        Me.TextBoxCustomerPosition.Name = "TextBoxCustomerPosition"
        Me.TextBoxCustomerPosition.Size = New System.Drawing.Size(290, 25)
        Me.TextBoxCustomerPosition.TabIndex = 85
        Me.TextBoxCustomerPosition.Text = ""
        Me.TextBoxCustomerPosition.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
        '
        'Panel1
        '
        Me.Panel1.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right)
        Me.Panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.Panel1.Location = New System.Drawing.Point(24, 256)
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(625, 221)
        Me.Panel1.TabIndex = 90
        '
        'BindingDemoForm15
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 18)
        Me.ClientSize = New System.Drawing.Size(672, 496)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label3, Me.Label2, Me.Label1, Me.TextBoxDiscount, Me.lblDiscount, Me.TextBoxQuantity, Me.lblQuantity, Me.TextBoxPrice, Me.lblPrice, Me.TextBoxProductName, Me.lblProductName, Me.TextBoxProductID, Me.lblProductID, Me.TextBoxOrderID, Me.lblOrderID, Me.TextBoxOrderPosition, Me.btnChildEnd, Me.btnChildNext, Me.btnChildBack, Me.btnChildFirst, Me.lblAmount, Me.lblOrderDate, Me.TextBoxAmount, Me.DateTimePickerOrderDate, Me.lblPhone, Me.TextBoxPhone, Me.lblContact, Me.lblCompany, Me.lblCustomerID, Me.TextBoxCustomerID, Me.TextBoxContact, Me.TextBoxCompanyName, Me.btnEnd, Me.btnNext, Me.btnBack, Me.btnFirst, Me.TextBoxCustomerPosition, Me.Panel1})
        Me.Name = "BindingDemoForm15"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Binding.Format 与 Binding.Parse 事件"
        Me.ResumeLayout(False)

    End Sub

#End Region

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

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

        ' 建立一个查询命令字符串
        Dim strSql As String = "SELECT 客户编号, 公司名称, 联系人, 电话 FROM 客户 WHERE 客户编号 IN (SELECT DISTINCT 客户编号 FROM 订货主档)"

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

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


        ' 将数据填入数据集
        myAD.Fill(ds, "客户")

        ' 重新指定用来提取数据源的数据记录的 SELECT 语句
        myAD.SelectCommand.CommandText = _
        "SELECT 订货主档.客户编号, 订货主档.订单号码, 订货主档.订单日期, " & _
           "订货明细.产品编号, 产品资料.产品, 订货明细.单价, 订货明细.数量, 订货明细.折扣, " & _
           "(订货明细.单价 * 订货明细.数量 * (1 - 订货明细.折扣)) AS 订货金额 " & _
        "FROM 订货主档 " & _
        "INNER JOIN 订货明细 " & _
        "ON 订货主档.订单号码 = 订货明细.订单号码 " & _
        "INNER JOIN 产品资料 " & _
        "ON 订货明细.产品编号 = 产品资料.产品编号 " & _
        "ORDER BY 订货主档.客户编号"

        ' 将数据填入数据集内名称为 订单明细金额 的数据表
        myAD.Fill(ds, "订单明细金额")

        ' 声明用来将数据集内的 客户 数据表连接至 订单明细金额 数据表的父字段与子字段
        Dim ParentColumn As DataColumn
        Dim ChildColumn As DataColumn
        ParentColumn = ds.Tables("客户").Columns("客户编号")
        ChildColumn = ds.Tables("订单明细金额").Columns("客户编号")

        ' 建立用来连接 客户 数据表与 订单明细金额 数据表的 DataRelation 对象,其名称为 每一位客户的订单 。
        rel = New DataRelation _
            ("每一位客户的订单", ParentColumn, ChildColumn)
        ds.Relations.Add(rel)

        ' 将 TextBox 控件绑定至父数据表(亦即 客户 数据表)的各个字段
        TextBoxCustomerID.DataBindings.Add("Text", ds, "客户.客户编号")
        TextBoxCompanyName.DataBindings.Add("Text", ds, "客户.公司名称")
        TextBoxContact.DataBindings.Add("Text", ds, "客户.联系人")
        TextBoxPhone.DataBindings.Add("Text", ds, "客户.电话")

        ' 将 TextBox 控件绑定至 DataRelation 对象(亦即 每一位客户的订单 )的各个字段
        TextBoxOrderID.DataBindings.Add("Text", ds, "客户.每一位客户的订单.订单号码")
        TextBoxProductID.DataBindings.Add("Text", ds, "客户.每一位客户的订单.产品编号")
        TextBoxProductName.DataBindings.Add("Text", ds, "客户.每一位客户的订单.产品")
        TextBoxPrice.DataBindings.Add("Text", ds, "客户.每一位客户的订单.单价")
        TextBoxQuantity.DataBindings.Add("Text", ds, "客户.每一位客户的订单.数量")
        TextBoxDiscount.DataBindings.Add("Text", ds, "客户.每一位客户的订单.折扣")

        ' 将 DateTimePicker 控件绑定至 DataRelation 对象(亦即 每一位客户的订单 )的 订单日期 字段
        DateTimePickerOrderDate.DataBindings.Add("Value", ds, "客户.每一位客户的订单.订单日期")

        ' 建立一个新的 Binding 对象并替它加入 Parse 与 Format 事件的委派,
        ' 然后将此 Binding 对象加至名称为 TextBoxAmount 的 TextBox 控件的 BindingsCollection 中。
        ' 您务必在将 Binding 对象加至集合中之前就先加入事件委派,否则,除非数据源
        ' 的 BindingManagerBase 的当前对象改变,将不会有任何格式化发生。
        Dim myBindingObject As Binding = New Binding("Text", ds, "客户.每一位客户的订单.订货金额")
        AddHandler myBindingObject.Format, AddressOf DecimalToCurrencyString
        AddHandler myBindingObject.Parse, AddressOf CurrencyStringToDecimal
        TextBoxAmount.DataBindings.Add(myBindingObject)

        ' 取得代表 客户 数据表的 CurrencyManager 对象
        bmCustomers = Me.BindingContext(ds, "客户")

        ' 设定当引发 bmCustomers 的 PositionChanged 事件时便执行事件处理例程 客户_PositionChanged 
        AddHandler bmCustomers.PositionChanged, AddressOf 客户_PositionChanged

        ' 取得代表 DataRelation 对象(亦即 每一位客户的订单 )的 CurrencyManager 对象
        bmOrders = Me.BindingContext(ds, "客户.每一位客户的订单")

        ' 设定当引发 bmOrders 的 PositionChanged 事件时便执行事件处理例程 订单_PositionChanged 
        AddHandler bmOrders.PositionChanged, AddressOf 订单_PositionChanged

        ' 设定数据记录当前位置讯息的初值
        TextBoxCustomerPosition.Text = String.Format("客户记录:当前位置 {0} 总数 {1}", bmCustomers.Position + 1, bmCustomers.Count)
        TextBoxOrderPosition.Text = String.Format("订单记录:当前位置 {0} 总数 {1}", bmOrders.Position + 1, bmOrders.Count)

        myConnection.Close()
    End Sub

    ' 此方法是 Format 事件的事件处理例程。每当控件显示一个新的数值,该数值便会由其原生类型
    ' 被转换成一个货币格式的字符串。ToString 方法使用格式化字符 "c" 将该数值转换成一个货币格式的字符串。
    Private Sub DecimalToCurrencyString(ByVal sender As Object, ByVal cevent As ConvertEventArgs)

        ' 使用 DesiredType 来检测是否能转换成 String 类型
        If Not cevent.DesiredType Is GetType(String) Then
            Exit Sub
        End If
        ' 使用 ToString 方法将数值转换成货币格式的字符串
        cevent.Value = CType(cevent.Value, Decimal).ToString("c")
    End Sub


    ' 此方法是 Parse 事件的事件处理例程。每当所显示的数值改变时,Parse 事件就会被引发。
    ' 在此主要是将货币格式的字符串转换回 Decimal 类型。
    Private Sub CurrencyStringToDecimal(ByVal sender As Object, ByVal cevent As ConvertEventArgs)

        ' 使用 DesiredType 来检测是否能转换成 Decimal 类型
        If Not cevent.DesiredType Is GetType(Decimal) Then
            Exit Sub
        End If

        ' 将货币格式的字符串转换回 Decimal 类型
        cevent.Value = Decimal.Parse(cevent.Value.ToString, NumberStyles.Currency, Nothing)
    End Sub

    Protected Sub 客户_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        TextBoxCustomerPosition.Text = String.Format("客户记录:当前位置 {0} 总数 {1}", bmCustomers.Position + 1, bmCustomers.Count)
        TextBoxOrderPosition.Text = String.Format("订单记录:当前位置 {0} 总数 {1}", bmOrders.Position + 1, bmOrders.Count)
    End Sub

    Protected Sub 订单_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        TextBoxOrderPosition.Text = String.Format("订单记录:当前位置 {0} 总数 {1}", bmOrders.Position + 1, bmOrders.Count)
    End Sub

    ' 按下浏览父数据表的 第一条 按钮
    Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
        ' 将 Position 属性设定成 0
        bmCustomers.Position = 0
    End Sub

    ' 按下浏览父数据表的 上一条 按钮
    Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
        If bmCustomers.Position > 0 Then
            ' 将 Position 属性递减 1
            bmCustomers.Position -= 1
        End If
    End Sub

    ' 按下浏览父数据表的 下一条 按钮
    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        If bmCustomers.Position < bmCustomers.Count - 1 Then
            ' 将 Position 属性递增 1
            bmCustomers.Position += 1
        End If
    End Sub

    ' 按下浏览父数据表的 最后一条 按钮
    Private Sub btnEnd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnd.Click
        bmCustomers.Position = bmCustomers.Count - 1
    End Sub

    ' 按下浏览 DataRelation 对象的 第一条 按钮
    Private Sub btnChildFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChildFirst.Click
        bmOrders.Position = 0
    End Sub

    ' 按下浏览 DataRelation 对象的 上一条 按钮
    Private Sub btnChildBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChildBack.Click
        If bmOrders.Position > 0 Then
            bmOrders.Position -= 1
        End If
    End Sub

    ' 按下浏览 DataRelation 对象的 下一条 按钮
    Private Sub btnChildNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChildNext.Click
        If bmOrders.Position < bmOrders.Count - 1 Then
            bmOrders.Position += 1
        End If
    End Sub

    ' 按下浏览 DataRelation 对象的 最后一条 按钮
    Private Sub btnChildEnd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChildEnd.Click
        bmOrders.Position = bmOrders.Count - 1
    End Sub
End Class

⌨️ 快捷键说明

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