handlingerrors.vb

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

VB
275
字号
Public Class HandlingErrors
    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 Label4 As System.Windows.Forms.Label
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents txtModelName As System.Windows.Forms.TextBox
    Friend WithEvents txtDescription As System.Windows.Forms.TextBox
    Friend WithEvents txtUnitCost As System.Windows.Forms.TextBox
    Friend WithEvents txtModelNumber As System.Windows.Forms.TextBox
    Friend WithEvents cboModelName As System.Windows.Forms.ComboBox
    Friend WithEvents cmdNext As System.Windows.Forms.Button
    Friend WithEvents cmdPrev As System.Windows.Forms.Button
    Friend WithEvents lblErrorSummary As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label4 = New System.Windows.Forms.Label()
        Me.GroupBox1 = New System.Windows.Forms.GroupBox()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.txtModelName = New System.Windows.Forms.TextBox()
        Me.txtDescription = New System.Windows.Forms.TextBox()
        Me.txtUnitCost = New System.Windows.Forms.TextBox()
        Me.txtModelNumber = New System.Windows.Forms.TextBox()
        Me.cboModelName = New System.Windows.Forms.ComboBox()
        Me.cmdNext = New System.Windows.Forms.Button()
        Me.cmdPrev = New System.Windows.Forms.Button()
        Me.lblErrorSummary = New System.Windows.Forms.Label()
        Me.GroupBox1.SuspendLayout()
        Me.SuspendLayout()
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(12, 16)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(88, 16)
        Me.Label4.TabIndex = 20
        Me.Label4.Text = "Select a Record:"
        '
        'GroupBox1
        '
        Me.GroupBox1.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right)
        Me.GroupBox1.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label3, Me.Label2, Me.Label1, Me.txtModelName, Me.txtDescription, Me.txtUnitCost, Me.txtModelNumber})
        Me.GroupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System
        Me.GroupBox1.Location = New System.Drawing.Point(12, 48)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(400, 228)
        Me.GroupBox1.TabIndex = 19
        Me.GroupBox1.TabStop = False
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(220, 56)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(36, 16)
        Me.Label3.TabIndex = 18
        Me.Label3.Text = "Cost:"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(16, 56)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(52, 16)
        Me.Label2.TabIndex = 17
        Me.Label2.Text = "Model:"
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(16, 28)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(52, 16)
        Me.Label1.TabIndex = 16
        Me.Label1.Text = "Name:"
        '
        'txtModelName
        '
        Me.txtModelName.Location = New System.Drawing.Point(68, 24)
        Me.txtModelName.Name = "txtModelName"
        Me.txtModelName.Size = New System.Drawing.Size(316, 21)
        Me.txtModelName.TabIndex = 15
        Me.txtModelName.Text = ""
        '
        'txtDescription
        '
        Me.txtDescription.Location = New System.Drawing.Point(12, 92)
        Me.txtDescription.Multiline = True
        Me.txtDescription.Name = "txtDescription"
        Me.txtDescription.Size = New System.Drawing.Size(372, 116)
        Me.txtDescription.TabIndex = 14
        Me.txtDescription.Text = ""
        '
        'txtUnitCost
        '
        Me.txtUnitCost.Location = New System.Drawing.Point(256, 52)
        Me.txtUnitCost.Name = "txtUnitCost"
        Me.txtUnitCost.Size = New System.Drawing.Size(128, 21)
        Me.txtUnitCost.TabIndex = 13
        Me.txtUnitCost.Text = ""
        '
        'txtModelNumber
        '
        Me.txtModelNumber.Location = New System.Drawing.Point(68, 52)
        Me.txtModelNumber.Name = "txtModelNumber"
        Me.txtModelNumber.Size = New System.Drawing.Size(136, 21)
        Me.txtModelNumber.TabIndex = 12
        Me.txtModelNumber.Text = ""
        '
        'cboModelName
        '
        Me.cboModelName.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboModelName.Location = New System.Drawing.Point(104, 10)
        Me.cboModelName.Name = "cboModelName"
        Me.cboModelName.Size = New System.Drawing.Size(308, 21)
        Me.cboModelName.TabIndex = 18
        '
        'cmdNext
        '
        Me.cmdNext.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right)
        Me.cmdNext.FlatStyle = System.Windows.Forms.FlatStyle.System
        Me.cmdNext.Location = New System.Drawing.Point(320, 348)
        Me.cmdNext.Name = "cmdNext"
        Me.cmdNext.Size = New System.Drawing.Size(92, 28)
        Me.cmdNext.TabIndex = 22
        Me.cmdNext.Text = "Next >>"
        '
        'cmdPrev
        '
        Me.cmdPrev.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left)
        Me.cmdPrev.Enabled = False
        Me.cmdPrev.FlatStyle = System.Windows.Forms.FlatStyle.System
        Me.cmdPrev.Location = New System.Drawing.Point(16, 348)
        Me.cmdPrev.Name = "cmdPrev"
        Me.cmdPrev.Size = New System.Drawing.Size(92, 28)
        Me.cmdPrev.TabIndex = 21
        Me.cmdPrev.Text = "<< Prev"
        '
        'lblErrorSummary
        '
        Me.lblErrorSummary.ForeColor = System.Drawing.Color.Red
        Me.lblErrorSummary.Location = New System.Drawing.Point(24, 296)
        Me.lblErrorSummary.Name = "lblErrorSummary"
        Me.lblErrorSummary.Size = New System.Drawing.Size(328, 40)
        Me.lblErrorSummary.TabIndex = 23
        '
        'HandlingErrors
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 14)
        Me.ClientSize = New System.Drawing.Size(428, 386)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblErrorSummary, Me.cmdNext, Me.cmdPrev, Me.Label4, Me.GroupBox1, Me.cboModelName})
        Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Name = "HandlingErrors"
        Me.Text = "Handling Errors"
        Me.GroupBox1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private CurrentPage As Integer
    Private ErrFlag As Boolean
    Private dsStore As DataSet
    Private StoreBinding As BindingManagerBase

    Private Sub EditableBinding_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dsStore = New DataSet()

        dsStore.ReadXmlSchema(Application.StartupPath & "\store.xsd")
        dsStore.ReadXml(Application.StartupPath & "\store.xml")

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

        txtModelName.DataBindings.Add("Text", dsStore.Tables("Products"), "ModelName")
        txtModelNumber.DataBindings.Add("Text", dsStore.Tables("Products"), "ModelNumber")
        txtUnitCost.DataBindings.Add("Text", dsStore.Tables("Products"), "UnitCost")
        txtDescription.DataBindings.Add("Text", dsStore.Tables("Products"), "Description")

        StoreBinding = Me.BindingContext(dsStore.Tables("Products"))
        AddHandler StoreBinding.PositionChanged, AddressOf Binding_PositionChanged
        AddHandler dsStore.Tables("Products").ColumnChanged, AddressOf TableChanging
    End Sub

    Private Sub Binding_PositionChanged(ByVal sender As Object, ByVal e As EventArgs)
        If ErrFlag = True Then
            StoreBinding.Position = CurrentPage
        Else
            CurrentPage = StoreBinding.Position
        End If

        If StoreBinding.Position = StoreBinding.Count - 1 Then
            cmdNext.Enabled = False
        Else
            cmdNext.Enabled = True
        End If
        If StoreBinding.Position = 0 Then
            cmdPrev.Enabled = False
        Else
            cmdPrev.Enabled = True
        End If
    End Sub

    Private Sub cmdPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrev.Click
        StoreBinding.Position -= 1
    End Sub

    Private Sub cmdNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNext.Click
        StoreBinding.Position += 1
    End Sub

    Private Sub TableChanging(ByVal sender As Object, ByVal e As System.Data.DataColumnChangeEventArgs)

        Dim Errors As String = DBStore.ValidateProduct(e.Row)

        If Errors = "" Then
            ErrFlag = False
        Else
            ErrFlag = True
        End If

        lblErrorSummary.Text = Errors
    End Sub
End Class

Public Class DBStore

    Public Shared Function ValidateProduct(ByVal row As DataRow) As String
        Dim Errors As String

        If row("UnitCost") <= 0 Then
            Errors &= "* UnitCost value too low" & System.Environment.NewLine
        End If
        If row("ModelNumber") = "" Then
            Errors &= "* You must specify a ModelNumber" & System.Environment.NewLine
        End If
        If row("ModelName") = "" Then
            Errors &= "* You must specify a ModelName" & System.Environment.NewLine
        End If

        Return Errors
    End Function

End Class

⌨️ 快捷键说明

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