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

📄 frmlist.vb

📁 对现代企业来说
💻 VB
字号:
Public Class frmList
    Inherits System.Windows.Forms.Form
    Friend WithEvents BtnClose As System.Windows.Forms.Button
    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu

#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)
        MyBase.Dispose(disposing)
    End Sub

    '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 MenuItem1 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
    Friend WithEvents DGAddress As System.Windows.Forms.DataGrid
    Friend WithEvents BtnInsertNew As System.Windows.Forms.Button
    Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(frmList))
        Me.MainMenu1 = New System.Windows.Forms.MainMenu
        Me.MenuItem1 = New System.Windows.Forms.MenuItem
        Me.MenuItem2 = New System.Windows.Forms.MenuItem
        Me.BtnClose = New System.Windows.Forms.Button
        Me.DGAddress = New System.Windows.Forms.DataGrid
        Me.BtnInsertNew = New System.Windows.Forms.Button
        '
        'MainMenu1
        '
        Me.MainMenu1.MenuItems.Add(Me.MenuItem1)
        '
        'MenuItem1
        '
        Me.MenuItem1.MenuItems.Add(Me.MenuItem2)
        Me.MenuItem1.Text = "Menu"
        '
        'MenuItem2
        '
        Me.MenuItem2.Text = "Initialize Database"
        '
        'BtnClose
        '
        Me.BtnClose.Location = New System.Drawing.Point(152, 224)
        Me.BtnClose.Text = "Close"
        '
        'DGAddress
        '
        Me.DGAddress.Location = New System.Drawing.Point(16, 16)
        Me.DGAddress.Size = New System.Drawing.Size(208, 200)
        Me.DGAddress.Text = "DGAddress"
        '
        'BtnInsertNew
        '
        Me.BtnInsertNew.Location = New System.Drawing.Point(16, 224)
        Me.BtnInsertNew.Text = "Insert New"
        '
        'frmList
        '
        Me.Controls.Add(Me.BtnInsertNew)
        Me.Controls.Add(Me.DGAddress)
        Me.Controls.Add(Me.BtnClose)
        Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
        Me.Text = "frmList"

    End Sub

#End Region

    Private AddressBook As New AddressBookApplication
    Private InitializationFlg As Boolean = True
    Private ClassName As String
    Private ErrLoc As String

#Region " DataSet Functions "
    REM =================================================================================== 
    REM Function to Insert columns into a one column dataset 
    REM =================================================================================== 
    Public Function AddressDSAddCols(ByVal dTable As DataTable, ByVal dDataSet As DataSet) As DataSet
        Try
            dTable.Columns.Add("ADDRESS_ID", System.Type.GetType("System.String"))
            dTable.Columns.Add("NAME", System.Type.GetType("System.String"))

            dDataSet.Tables.Add(dTable)
            Return dDataSet
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Function


    REM =================================================================================== 
    REM Function to Insert a row into a one column dataset 
    REM =================================================================================== 
    Public Function AddressDSAddRows(ByVal dTable As DataTable, ByVal Name As String, ByVal Address_ID As Integer) As DataTable
        Dim ds As DataSet
        Try
            Dim dr As DataRow = dTable.NewRow()
            dr(0) = CStr(Address_ID)
            dr(1) = CStr(Name)

            dTable.Rows.Add(dr)
            Return dTable
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Function
#End Region

    Private Sub frmList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ClassName = Me.GetType.Name
        Dim ErrLoc As String = ClassName + ".frmList_Load"
        Try
            If AddressBook.DatabaseExists Then
                DGAddressRefresh()
            Else
                MessageBox.Show("Error: Cannot initialize database")
            End If
        Catch ex As Exception
            UnHandledError(ex.ToString(), ErrLoc)
        End Try
    End Sub

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

    Private Sub BtnInsertNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnInsertNew.Click
        Dim ErrLoc As String = ClassName + ".BtnInsertNew_Click"
        Try
            Dim ifrmAddressItem As New frmAddressItem
            ifrmAddressItem.SelectedAddressID = -1 ' This indicates that we want a new record
            ifrmAddressItem.ShowDialog()
            ifrmAddressItem.Dispose()
            DGAddressRefresh()
        Catch ex As Exception
            UnHandledError(ex.ToString(), ErrLoc)
        End Try
    End Sub

    Private Sub DGAddress_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DGAddress.Click
        Dim ErrLoc As String = ClassName + ".DGAddress_Click"
        Try
            Dim ifrmAddressItem As New frmAddressItem
            ifrmAddressItem.SelectedAddressID = DGAddress.Item(DGAddress.CurrentRowIndex, 0)
            ifrmAddressItem.ShowDialog()
            ifrmAddressItem.Dispose()
            DGAddressRefresh()
        Catch ex As Exception
            UnHandledError(ex.ToString(), ErrLoc)
        End Try

    End Sub

    Private Sub DGAddressRefresh()
        Dim ErrLoc As String = ClassName + ".DGAddressRefresh"
        Try

            Dim ds As DataSet = New DataSet("MyDataSet")
            Dim dTable As New DataTable("Adr_Address")
            ds = AddressDSAddCols(dTable, ds)

            Dim dsSearchResult As New DataSet
            dsSearchResult = AddressBook.AddressDS

            Dim i As Integer
            Dim r As DataRow
            Dim c As DataColumn
            Dim Address_ID As String
            Dim Name As String


            For Each r In dsSearchResult.Tables("Adr_Address").Rows
                If Not r("Address_ID") Is DBNull.Value And Not r("Name") Is DBNull.Value Then
                    Address_ID = r("Address_ID")
                    Name = r("Name")
                    dTable = AddressDSAddRows(dTable, Name, Address_ID)
                End If
            Next

            DGAddress.DataSource = dTable

            If InitializationFlg Then
                InitializationFlg = False

                Dim myGridTableStyle As New DataGridTableStyle
                myGridTableStyle.MappingName = dTable.TableName
                DGAddress.TableStyles.Add(myGridTableStyle)

                myGridTableStyle = DGAddress.TableStyles.Item("Adr_Address")

                Dim MyColumnStyle As DataGridColumnStyle
                MyColumnStyle = myGridTableStyle.GridColumnStyles.Item("ADDRESS_ID")
                MyColumnStyle.Width = 0

                MyColumnStyle = myGridTableStyle.GridColumnStyles.Item("NAME")
                MyColumnStyle.Width = 180
            End If
            DGAddress.Refresh()
        Catch ex As Exception
            UnHandledError(ex.ToString(), ErrLoc)
        End Try

    End Sub

    Public Function UnHandledError(ByVal Exception As String, ByVal Location As String) As String
        MsgBox("[-->" + Location + "<--]" + Exception)
    End Function



End Class

⌨️ 快捷键说明

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