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

📄 frmcustomer.vb

📁 Visual Basic Oracle数据库联接
💻 VB
📖 第 1 页 / 共 2 页
字号:
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(64, 16)
        Me.Label2.TabIndex = 28
        Me.Label2.Text = "Company"
        '
        'frmCustomer
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(330, 352)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.GroupBox1, Me.btnLoad, Me.btnAdd, Me.btnDelete, Me.btnNavFirst, Me.btnNavPrev, Me.lblNavLocation, Me.btnNavNext, Me.btnLast, Me.btnUpdate, Me.btnClose, Me.txtPhone2, Me.Label6, Me.txtPhone1, Me.Label5, Me.txtAddress, Me.Label4, Me.txtUniqueID, Me.Label1})
        Me.Font = New System.Drawing.Font("Verdana", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        Me.Name = "frmCustomer"
        Me.Text = "Edit Customers"
        CType(Me.dsCust, System.ComponentModel.ISupportInitialize).EndInit()
        Me.GroupBox1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region
    Public CurrentRow As Integer
    Private Sub frmCustomer_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.btnLoad_Click(sender, e)
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Try
            If btnAdd.Text = "&Add" Then
                'Clear out the current edits
                Me.Text = "Add New Customers"
                Me.txtAddress.Text = strEmpty
                Me.txtCoName.Text = strEmpty
                Me.txtContact.Text = strEmpty
                Me.txtPhone1.Text = strEmpty
                Me.txtPhone2.Text = strEmpty
                Me.txtUniqueID.Text = "(Auto)"
                Me.txtTitle.Text = strEmpty
                LockControls(True)
                Me.lblNavLocation.Text = "1 of 1"
                txtCoName.Select()
            Else
                If Not ValidateValues() Then MsgBox("Missing Information: Please check the form.") : Exit Sub
                ' Create a row with these values
                Dim rowVals As DataRow
                rowVals = dsCust.Tables(0).NewRow
                rowVals(0) = IIf(txtUniqueID.Text = "(Auto)", Microsoft.VisualBasic.Left(GenerateID(txtCoName.Text, 2), 7), txtUniqueID.Text)
                rowVals(1) = txtCoName.Text
                rowVals(2) = txtContact.Text
                rowVals(3) = txtTitle.Text
                rowVals(4) = txtAddress.Text
                rowVals(5) = txtPhone1.Text
                rowVals(6) = txtPhone2.Text

                'TODO: Check for duplicates
                'Save to xml file
                Dim strXML As String
                Dim xDoc As XmlDocument = New XmlDocument()
                Dim xReader As XmlTextReader = New XmlTextReader(m_connCustomers)
                Dim xDocFrag As XmlDocumentFragment

                xReader.Read()
                xDoc.Load(xReader)
                xReader.Close()

                strXML = "<Customers>" & _
                    "<CustomerID>" & rowVals(0).ToString & "</CustomerID>" & _
                    "<CompanyName>" & rowVals(1).ToString & "</CompanyName>" & _
                    "<ContactName>" & rowVals(2).ToString & "</ContactName>" & _
                    "<ContactTitle>" & rowVals(3).ToString & "</ContactTitle>" & _
                    "<Address1>" & rowVals(4).ToString & "</Address1>" & _
                    "<Phone1>" & rowVals(5).ToString & "</Phone1>" & _
                    "<Phone2>" & rowVals(6).ToString & "</Phone2>" & _
                    "</Customers>"

                xDocFrag = xDoc.CreateDocumentFragment
                xDocFrag.InnerXml = strXML
                xDoc.DocumentElement.AppendChild(xDocFrag)
                xDoc.Save(m_connCustomers)
                LoadDataSet()

                CurrentRow = 0
                LockControls(False)
                Me.dsCust_PositionChanged()
            End If
        Catch eEndEdit As System.Exception
            System.Windows.Forms.MessageBox.Show(eEndEdit.Message)
        End Try

    End Sub

    Private Sub LockControls(ByVal bEnabled As Boolean)
        If bEnabled Then
            btnAdd.Text = "&Save"
        Else
            btnAdd.Text = "&Add"
        End If
        'Default is enabled
        btnClose.Enabled = Not bEnabled
        btnDelete.Enabled = Not bEnabled
        btnLast.Enabled = Not bEnabled
        btnNavPrev.Enabled = Not bEnabled
        btnNavNext.Enabled = Not bEnabled
        btnLoad.Enabled = Not bEnabled
        btnUpdate.Enabled = Not bEnabled
        btnNavFirst.Enabled = Not bEnabled
    End Sub

    Private Sub dsCust_PositionChanged()
        Me.lblNavLocation.Text = (((CurrentRow + 1).ToString + " of  ") _
                    + Me.dsCust.Tables(0).Rows.Count.ToString)
        Me.txtAddress.Text = Me.dsCust.Tables(0).Rows(CurrentRow)("Address1").ToString
        Me.txtCoName.Text = Me.dsCust.Tables(0).Rows(CurrentRow)("CompanyName").ToString
        Me.txtContact.Text = Me.dsCust.Tables(0).Rows(CurrentRow)("ContactName").ToString
        Me.txtPhone1.Text = Me.dsCust.Tables(0).Rows(CurrentRow)("Phone1").ToString
        Me.txtPhone2.Text = Me.dsCust.Tables(0).Rows(CurrentRow)("Phone2").ToString
        Me.txtUniqueID.Text = Me.dsCust.Tables(0).Rows(CurrentRow)("CustomerID").ToString
        Me.txtTitle.Text = Me.dsCust.Tables(0).Rows(CurrentRow)("ContactTitle").ToString
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If dsCust.Tables(0).Rows.Count > 0 Then
            dsCust.Tables(0).Rows(CurrentRow).Delete()
            btnNavFirst_Click(sender, e)
            Me.dsCust_PositionChanged()
        End If
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        'Try
            'Send updates to xml file
        Dim xDoc As XmlDocument = New XmlDocument()
        Dim root, id As XmlNode
        Dim i As Integer
        root = xDoc.DocumentElement

        For i = 0 To root.ChildNodes.Count - 1
            id = root.ChildNodes(i).Attributes("CustomerID")
            If (id.InnerText = txtUniqueID.Text) Then
                xDoc.DocumentElement("CustomerID").Value = txtUniqueID.Text
                xDoc.DocumentElement("CompanyName").Value = txtCoName.Text
                xDoc.DocumentElement("ContactName").Value = txtContact.Text
                xDoc.DocumentElement("ContactTitle").Value = txtTitle.Text
                xDoc.DocumentElement("Address1").Value = txtAddress.Text
                xDoc.DocumentElement("Phone1").Value = txtPhone1.Text
                xDoc.DocumentElement("Phone2").Value = txtPhone2.Text
            End If
        Next i
        xDoc.Save(m_connCustomers)

        'Reset and begin again
        LoadDataSet()
        MsgBox("Record has been updated.")
        'Attempt to update the datasource.
        Me.LoadDataSet()
        'Catch eUpdate As System.Exception
        '    'Add your error handling code here.
        '    'Display error message, if any.
        '    System.Windows.Forms.MessageBox.Show(eUpdate.Message)
        'End Try
        Me.dsCust_PositionChanged()

    End Sub

    Public Sub UpdateDataSet()
        'Create a new dataset to hold the changes that have been made to the main dataset.
        Dim objDataSetChanges As DataSet = New DataSet()
        'Stop any current edits.
        Me.BindingContext(dsCust).EndCurrentEdit()
        'Get the changes that have been made to the main dataset.
        objDataSetChanges = CType(dsCust.GetChanges, DataSet)
        'Check to see if any changes have been made.
        If (Not (objDataSetChanges) Is Nothing) Then
            Try
                'There are changes that need to be made, so attempt to update the datasource by
                'calling the update method and passing the dataset and any parameters.
                Me.UpdateDataSource(objDataSetChanges)
                dsCust.Merge(objDataSetChanges)
                dsCust.AcceptChanges()
            Catch eUpdate As System.Exception
                'Add your error handling code here.
                Throw eUpdate
            End Try
            'Add your code to check the returned dataset for any errors that may have been
            'pushed into the row object's error.
        End If
    End Sub

    Public Sub UpdateDataSource(ByVal ChangedRows As DataSet)
        'Try
        '    'The data source only needs to be updated if there are changes pending.
        '    If (Not (ChangedRows) Is Nothing) Then
        '        'Open the connection.
        '        Me.OleDbConnection1.Open()
        '        'Attempt to update the data source.
        '        OleDbDataAdapter1.Update(ChangedRows)
        '        OleDbDataAdapter2.Update(ChangedRows)
        '    End If
        'Catch updateException As System.Exception
        '    'Add your error handling code here.
        '    Throw updateException
        'Finally
        '    'Close the connection whether or not the exception was thrown.
        '    Me.OleDbConnection1.Close()
        'End Try
    End Sub

    Private Function ValidateValues() As Boolean
        Dim b As Boolean
        b = IIf(Len(Me.txtAddress.Text) > 0 And _
             Len(Me.txtCoName.Text) > 0 And _
              Len(Me.txtContact.Text) > 0 And _
                Len(Me.txtPhone1.Text) > 0 And _
                 Len(Me.txtPhone2.Text) > 0 And _
                  Len(Me.txtTitle.Text) > 0, True, False)
        Return b
    End Function

    Public Function FillDataSet() As DataSet
        Dim vdataset As New DataSet()
        'Turn off constraint checking before the dataset is filled.
        'This allows the adapters to fill the dataset without concern
        'for dependencies between the tables.
        vdataset.EnforceConstraints = False
        Try
            vdataset.ReadXml(m_connCustomers)
        Catch fillException As System.Exception
            'Add your error handling code here.
            Throw fillException
        Finally
            'Turn constraint checking back on.
            vdataset.EnforceConstraints = True
        End Try
        Return vdataset
    End Function

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        Try
            'Attempt to load the dataset.
            Me.LoadDataSet()
        Catch eLoad As System.Exception
            'Add your error handling code here.
            'Display error message, if any.
            System.Windows.Forms.MessageBox.Show(eLoad.Message)
        End Try
        Me.dsCust_PositionChanged()
    End Sub
    Private Sub btnNavFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNavFirst.Click
        CurrentRow = 0
        Me.BindingContext(dsCust).Position = 0
        Me.dsCust_PositionChanged()
    End Sub
    Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click

        CurrentRow = (Me.dsCust.Tables(0).Rows.Count - 1)
        Me.BindingContext(dsCust).Position = (Me.dsCust.Tables(0).Rows.Count - 1)
        Me.dsCust_PositionChanged()
    End Sub
    Private Sub btnNavPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNavPrev.Click
        If CurrentRow > 0 Then CurrentRow = CurrentRow - 1
        Me.BindingContext(dsCust).Position = (Me.BindingContext(dsCust).Position - 1)
        Me.dsCust_PositionChanged()
    End Sub
    Private Sub btnNavNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNavNext.Click
        If CurrentRow < Me.dsCust.Tables(0).Rows.Count - 1 Then
            CurrentRow = CurrentRow + 1
        End If
        Me.BindingContext(dsCust).Position = (Me.BindingContext(dsCust).Position + 1)
        Me.dsCust_PositionChanged()
    End Sub
    Private Sub btnCancelAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.dsCust.RejectChanges()
    End Sub

    Public Sub LoadDataSet()
        'Create a new dataset to hold the records returned from the call to FillDataSet.
        Try
            'Empty the old records from the dataset.
            dsCust.Clear()
            'Merge the records into the main dataset.
            dsCust.Merge(Me.FillDataSet)
        Catch eLoadMerge As System.Exception
            'Add your error handling code here.
            Throw eLoadMerge
        End Try

    End Sub

    Private Sub txtCoName_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtCoName.KeyDown, txtAddress.KeyDown, txtContact.KeyDown, txtPhone1.KeyDown, txtPhone2.KeyDown, txtTitle.KeyDown
        If txtUniqueID.Text = "(Auto)" And Len(txtCoName.Text) > 4 Then
            Dim stmp As String
            stmp = GenerateID(txtCoName.Text, 2)
            txtUniqueID.Text = UCase(Microsoft.VisualBasic.Left(stmp, 7))
        End If
    End Sub
End Class

⌨️ 快捷键说明

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