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

📄 mnfrm.vb

📁 This application uses OleDb as a backhand communicator with the file to allow the user to : select,
💻 VB
📖 第 1 页 / 共 4 页
字号:

            DataLoaded = True

            menuF_save.Enabled = True
            menuSearch.Enabled = True

            btnAdd.Visible = True
            btnCancel.Visible = True
            btnDelete.Visible = True
            btnUpdate.Visible = True
            btnNavPrev.Visible = True
            btnNavNext.Visible = True
            comboTables.Visible = True
            lblNavLocation.Visible = True

            GPostion = startingPos + 170
            dGrid.Height = Me.Height - GPostion
        End Sub 'arrange_Controls

#End Region

#Region "DataHighlighted Position Change"
        'update the lblnavlocation text
        Private Sub DataSet_PositionChanged()
            Me.lblNavLocation.Text = (Me.BindingContext(accessDataSet, comboTables.Text).Position + 1).ToString() + " of  " + Me.BindingContext(accessDataSet, comboTables.Text).Count.ToString()
        End Sub 'DataSet_PositionChanged

#End Region

#Region "Update Records in database"
        'This routine handles and perfroms the update
        'procedure to save changes in the main source
        Public Sub UpdateDataSet()
            ' Create a new dataset to hold the changes that have been made to the main dataset.
            Dim objDataSetChanges As New DataSet()
            ' Stop any current edits.
            Me.BindingContext(accessDataSet, comboTables.Text).EndCurrentEdit()
            ' Get the changes that have been made to the main dataset.
            objDataSetChanges = CType(accessDataSet.GetChanges(), DataSet)
            ' Check to see if any changes have been made.
            If Not (objDataSetChanges Is Nothing) Then
                UpdateDataSource(objDataSetChanges)
                If Me.accessConnection.State.ToString() <> "Closed" Then
                    Me.accessConnection.Close()
                End If
            End If
        End Sub 'UpdateDataSet

        Public Sub UpdateDataSource(ByVal ChangedRows As DataSet)
            ' The data source only needs to be updated if there are changes pending.
            If Not (ChangedRows Is Nothing) Then
                ' Open the connection.
                Me.accessConnection.Open()
                ' Attempt to update the data source.
                Try
                    accessDataAdapter.Update(ChangedRows)
                    accessDataSet.AcceptChanges()
                    'Catch all the erros and report them
                Catch e As System.Data.DBConcurrencyException
                    accessDataSet.AcceptChanges()
                    MessageBox.Show("Unfortunately this data file cannot be saved as " + "Concurrency violation, (please exit the " + "all data are rejected, please renter your data)", "Cannot Update File")
                Finally
                    Me.accessConnection.Close()
                End Try
            End If
        End Sub 'UpdateDataSource

#End Region

#Region "This Form's All Button Click Events"
        'This section holds all the click events for all
        'the controls on the form
        'Navigate Previous Routine
        Private Sub btnNavPrev_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNavPrev.Click
            Me.BindingContext(accessDataSet, comboTables.Text).Position = Me.BindingContext(accessDataSet, comboTables.Text).Position - 1
            Me.DataSet_PositionChanged()
        End Sub 'btnNavPrev_Click


        'Navigate Next Routine
        Private Sub btnNavNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNavNext.Click
            Me.BindingContext(accessDataSet, comboTables.Text).Position = Me.BindingContext(accessDataSet, comboTables.Text).Position + 1
            Me.DataSet_PositionChanged()
        End Sub 'btnNavNext_Click


        'Add Data Routine
        Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
            Try
                ' Clear out the current edits
                Me.BindingContext(accessDataSet, comboTables.Text).EndCurrentEdit()
                Me.BindingContext(accessDataSet, comboTables.Text).AddNew()
            Catch eEndEdit As System.Exception
                System.Windows.Forms.MessageBox.Show(eEndEdit.Message)
            End Try

            Me.DataSet_PositionChanged()
        End Sub 'btnAdd_Click


        'Delet Data Routine
        Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
            If Me.BindingContext(accessDataSet, comboTables.Text).Count > 0 Then
                Me.BindingContext(accessDataSet, comboTables.Text).RemoveAt(Me.BindingContext(accessDataSet, comboTables.Text).Position)
                Me.DataSet_PositionChanged()
            End If
        End Sub 'btnDelete_Click


        'Cancel Current Data Entry Routine
        Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
            Me.BindingContext(accessDataSet, comboTables.Text).CancelCurrentEdit()
            Me.DataSet_PositionChanged()
        End Sub 'btnCancel_Click


        'Update and Save Data Changes in the source file
        Private Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
            Try
                ' Attempt to update the datasource.
                Me.UpdateDataSet()
            Catch eUpdate As System.Data.OleDb.OleDbException
                ' Add your error handling code here.
                ' Display error message, if any.
                System.Windows.Forms.MessageBox.Show(eUpdate.Message)
            End Try

            Me.DataSet_PositionChanged()
        End Sub 'btnUpdate_Click


        'AutoNumber or Automatic Current Date Entry Routine
        Private Sub cMenuClick(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim tempItem As MenuItem = CType(sender, MenuItem)
            tempItem.Checked = Not tempItem.Checked
        End Sub 'cMenuClick


#End Region

#Region "removeMadeControls"
        'Here all the made controls are removed
        Private Sub removeMadeControls()
            Dim i As Integer
            Do While (i < Me.Controls.Count)
                If Me.Controls(i).GetType().Name = "Label" And Me.Controls(i).Name <> "lblNavLocation" Then
                    Me.Controls(i).Dispose()
                    i = 0
                Else
                    If Me.Controls(i).GetType().Name = "TextBox" Then
                        Me.Controls(i).Dispose()
                        i = 0
                    End If
                End If
                i += 1
            Loop
        End Sub 'removeMadeControls
#End Region

#Region "comboTables_SelectedValueChanged"
        'ComboTables Value Changed Routine
        Private Sub comboTables_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles comboTables.SelectedValueChanged
            If ComboBoxText = comboTables.Text Then
                Return
            Else
                ComboBoxText = comboTables.Text
            End If
            removeMadeControls()
            LoadData(("SELECT * FROM [" + comboTables.Text + "]"))
        End Sub 'comboTables_SelectedValueChanged

#End Region

#Region "Form Closing and Resizing Event"
        Private Sub MainFrm_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
            If Check_If_Data_Changed() = True Then
                Dim r As DialogResult = MessageBox.Show("The database file changed, are sure you want exit without saving !!", "Exit Without Save", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)
                If r = DialogResult.Yes Then
                    Application.Exit()
                Else
                    e.Cancel = True
                End If
            Else
                Application.Exit()
            End If
        End Sub 'MainFrm_Closing

        Private Sub MainFrm_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
            dGrid.Height = Me.Height - GPostion
        End Sub 'MainFrm_Resize

#End Region

#Region "Data Grid's Event"
        'Show AutoNumber Incrementation or Automatic Date
        'Insertion menu creation routine 
        Private Sub dGrid_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dGrid.MouseDown
            'Runs for right mouse down
            Dim myGrid As DataGrid = CType(sender, DataGrid)
            Dim hti As System.Windows.Forms.DataGrid.HitTestInfo

            hti = myGrid.HitTest(e.X, e.Y)

            Select Case hti.Type
                Case System.Windows.Forms.DataGrid.HitTestType.ColumnHeader
                    If colType(hti.Column) = "System.Int32" Or colType(hti.Column) = "System.DateTime" Then
                        If e.Button <> System.Windows.Forms.MouseButtons.Right Then
                            Return
                        End If
                        AutoMenu(hti.Column).Show(dGrid, New Point(e.X, e.Y))
                    End If
            End Select
        End Sub 'dGrid_MouseDown


        'The Grid's update routine if any of the menue's are
        'checked
        Private Sub dGrid_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dGrid.CurrentCellChanged
            Me.DataSet_PositionChanged()
            Dim getRow As String() = lblNavLocation.Text.Split(" ")
            If getRow(0) <> getRow(3) Then
                Return
            End If
            Dim myGrid As DataGrid = CType(sender, DataGrid)
            Dim row As Integer = Convert.ToInt32(getRow(0))

            Dim i As Integer
            For i = 0 To (accessDataSet.Tables(comboTables.Text).Columns.Count) - 1
                If colType(i) = "System.Int32" AndAlso AutoMenu(i).MenuItems(0).Checked = True Then
                    If row > 1 Then
                        Try
                            UpdateTextBoxes(i, Convert.ToString((CInt(dGrid(dGrid.CurrentCell.RowNumber - 1, i)) + 1)))
                        Catch
                        End Try
                    End If
                End If
                If colType(i) = "System.DateTime" AndAlso AutoMenu(i).MenuItems(0).Checked = True Then
                    If (True) Then
                        UpdateTextBoxes(i, System.DateTime.Now.Date.ToString())
                    End If
                End If
            Next i
        End Sub 'dGrid_CurrentCellChanged


        'Menu Updates done in the textbox controls
        'after editing here
        Private Sub UpdateTextBoxes(ByVal Col As Integer, ByVal newValue As String)
            Dim i As Integer
            For i = 0 To (Me.Controls.Count) - 1
                If Me.Controls(i).Name = "cText" + Col.ToString() Then
                    If Me.Controls(i).Text <> "" Then
                        Return
                    End If
                    Me.Controls(i).Text = newValue
                End If
            Next i
        End Sub 'UpdateTextBoxes
#End Region

    End Class
End Namespace

⌨️ 快捷键说明

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