masterdetail.vb

来自「Samples are organized by chapter, and th」· VB 代码 · 共 124 行

VB
124
字号
Public Class MasterDetail
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    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

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents lstProduct As System.Windows.Forms.ListBox
    Friend WithEvents lstCategory As System.Windows.Forms.ListBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.lstProduct = New System.Windows.Forms.ListBox()
        Me.lstCategory = New System.Windows.Forms.ListBox()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.SuspendLayout()
        '
        'lstProduct
        '
        Me.lstProduct.Location = New System.Drawing.Point(208, 24)
        Me.lstProduct.Name = "lstProduct"
        Me.lstProduct.Size = New System.Drawing.Size(208, 225)
        Me.lstProduct.TabIndex = 3
        '
        'lstCategory
        '
        Me.lstCategory.Location = New System.Drawing.Point(8, 24)
        Me.lstCategory.Name = "lstCategory"
        Me.lstCategory.Size = New System.Drawing.Size(192, 225)
        Me.lstCategory.TabIndex = 2
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(8, 8)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(120, 16)
        Me.Label1.TabIndex = 4
        Me.Label1.Text = "Categories:"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(208, 8)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(120, 16)
        Me.Label2.TabIndex = 5
        Me.Label2.Text = "Products:"
        '
        'MasterDetail
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 14)
        Me.ClientSize = New System.Drawing.Size(432, 266)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label2, Me.Label1, Me.lstProduct, Me.lstCategory})
        Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Name = "MasterDetail"
        Me.Text = "Master-Detail List"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private CategoryBinding As BindingManagerBase
    Private dsStore As New DataSet()

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

        dsStore.ReadXmlSchema(Application.StartupPath & "\store.xsd")
        dsStore.ReadXml(Application.StartupPath & "\store.xml")
        
        lstCategory.DataSource = dsStore.Tables("Categories")
        lstCategory.DisplayMember = "CategoryName"

        lstProduct.DataSource = dsStore.Tables("Products")
        lstProduct.DisplayMember = "ModelName"

        CategoryBinding = Me.BindingContext(dsStore.Tables("Categories"))

        AddHandler CategoryBinding.PositionChanged, AddressOf Binding_PositionChanged

        ' Invoke method once to update child table at startup.
        Binding_PositionChanged(Nothing, Nothing)
    End Sub

    Private Sub Binding_PositionChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim Filter As String
        Dim SelectedRow As DataRow

        ' Find the current category row.
        SelectedRow = dsStore.Tables("Categories").Rows(CategoryBinding.Position)

        ' Create a filter expression using its CategoryID.
        Filter = "CategoryID='" & SelectedRow("CategoryID").ToString() & "'"

        ' Modify the view onto the product table.
        dsStore.Tables("Products").DefaultView.RowFilter = Filter
    End Sub

End Class

⌨️ 快捷键说明

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