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

📄 form1.vb

📁 Mastering VBNet Include Source Code
💻 VB
📖 第 1 页 / 共 2 页
字号:
        Me.txtZIP.TabIndex = 13
        Me.txtZIP.Text = ""
        '
        'txtAddress1
        '
        Me.txtAddress1.Font = New System.Drawing.Font("Verdana", 9!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte))
        Me.txtAddress1.Location = New System.Drawing.Point(120, 76)
        Me.txtAddress1.Name = "txtAddress1"
        Me.txtAddress1.ReadOnly = True
        Me.txtAddress1.Size = New System.Drawing.Size(360, 22)
        Me.txtAddress1.TabIndex = 5
        Me.txtAddress1.Text = ""
        '
        'txtAddress2
        '
        Me.txtAddress2.Font = New System.Drawing.Font("Verdana", 9!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte))
        Me.txtAddress2.Location = New System.Drawing.Point(120, 106)
        Me.txtAddress2.Name = "txtAddress2"
        Me.txtAddress2.ReadOnly = True
        Me.txtAddress2.Size = New System.Drawing.Size(360, 22)
        Me.txtAddress2.TabIndex = 7
        Me.txtAddress2.Text = ""
        '
        'bttnLast
        '
        Me.bttnLast.Font = New System.Drawing.Font("Verdana", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte))
        Me.bttnLast.Location = New System.Drawing.Point(406, 304)
        Me.bttnLast.Name = "bttnLast"
        Me.bttnLast.Size = New System.Drawing.Size(72, 32)
        Me.bttnLast.TabIndex = 26
        Me.bttnLast.Text = ">>"
        '
        'txtCompany
        '
        Me.txtCompany.Font = New System.Drawing.Font("Verdana", 9!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte))
        Me.txtCompany.Location = New System.Drawing.Point(120, 16)
        Me.txtCompany.Name = "txtCompany"
        Me.txtCompany.ReadOnly = True
        Me.txtCompany.Size = New System.Drawing.Size(320, 22)
        Me.txtCompany.TabIndex = 1
        Me.txtCompany.Text = ""
        '
        'txtURL
        '
        Me.txtURL.Font = New System.Drawing.Font("Verdana", 9!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte))
        Me.txtURL.Location = New System.Drawing.Point(120, 226)
        Me.txtURL.Name = "txtURL"
        Me.txtURL.ReadOnly = True
        Me.txtURL.Size = New System.Drawing.Size(328, 22)
        Me.txtURL.TabIndex = 19
        Me.txtURL.Text = ""
        '
        'txtState
        '
        Me.txtState.Font = New System.Drawing.Font("Verdana", 9!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte))
        Me.txtState.Location = New System.Drawing.Point(288, 136)
        Me.txtState.Name = "txtState"
        Me.txtState.ReadOnly = True
        Me.txtState.Size = New System.Drawing.Size(40, 22)
        Me.txtState.TabIndex = 11
        Me.txtState.Text = ""
        '
        'bttnDelete
        '
        Me.bttnDelete.Font = New System.Drawing.Font("Verdana", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte))
        Me.bttnDelete.Location = New System.Drawing.Point(408, 264)
        Me.bttnDelete.Name = "bttnDelete"
        Me.bttnDelete.Size = New System.Drawing.Size(72, 32)
        Me.bttnDelete.TabIndex = 22
        Me.bttnDelete.Text = "Delete"
        '
        'bttnEdit
        '
        Me.bttnEdit.Font = New System.Drawing.Font("Verdana", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte))
        Me.bttnEdit.Location = New System.Drawing.Point(256, 264)
        Me.bttnEdit.Name = "bttnEdit"
        Me.bttnEdit.Size = New System.Drawing.Size(72, 32)
        Me.bttnEdit.TabIndex = 21
        Me.bttnEdit.Text = "Edit"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.AutoScroll = True
        Me.ClientSize = New System.Drawing.Size(488, 341)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.bttnEdit, Me.Label10, Me.Label9, Me.Label8, Me.Label7, Me.Label6, Me.Label5, Me.Label4, Me.Label3, Me.Label2, Me.Label1, Me.bttnCancel, Me.bttnOK, Me.bttnDelete, Me.bttnAdd, Me.bttnLast, Me.bttnNext, Me.bttnPrevious, Me.bttnFirst, Me.txtURL, Me.txtEMail, Me.txtTel, Me.txtZIP, Me.txtState, Me.txtCity, Me.txtAddress2, Me.txtAddress1, Me.txtContact, Me.txtCompany})
        Me.KeyPreview = True
        Me.Menu = Me.MainMenu1
        Me.Name = "Form1"
        Me.Text = "Contacts Demo"
        Me.ResumeLayout(False)

    End Sub

#End Region

    ' A simple application that demonstrates how the design of a functional
    ' data browsing and editing form.
    ' The data resides in an ArrayList and the form displays one record at a time.
    ' Fields can be edited only with the edit buttons (Edit/Add/Delete) and the
    ' corresponding operation must end by clicking the Ok/Cancel button.

    ' The project also demonstrates how to serialize and deserialize an ArrayList
    ' of structures. This topic of serialization is discussed in detail in Chapter 11.

    ' IN CHAPTER 8 YOU WILL FIND A DIFFERENT IMPLEMENTATION OF THE SAME APPLICATION
    ' THAT USES A CLASS AND STORES EACH CONTACT ON A LISTBOX CONTROL. THE LISTBOX
    ' CONTROL IS USED AS A MORE FUNCTIONAL NAVIGATIONAL TOOL

    <Serializable()> Structure Contact
        Dim Company As String
        Dim Name As String
        Dim Address1 As String
        Dim Address2 As String
        Dim City As String
        Dim State As String
        Dim ZIP As String
        Dim Tel As String
        Dim EMail As String
        Dim URL As String
    End Structure

    Dim MyContacts As New ArrayList()
    Dim currentContact As Integer
    Dim adding As Boolean

    Private Sub bttnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnCancel.Click
        ShowContact()
        ShowButtons()
    End Sub

    Private Sub bttnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnOK.Click
        Dim contact As New Contact()
        SaveContact()
        ShowButtons()
    End Sub

    Private Sub bttnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnDelete.Click
        If currentContact > -1 Then
            MyContacts.RemoveAt(currentContact)
            If currentContact = MyContacts.Count Then currentContact = MyContacts.Count - 1
            If currentContact = -1 Then
                ClearFields()
                MsgBox("There are no more contacts")
            Else
                ShowContact()
            End If
        Else
            MsgBox("No selected contact to delete")
        End If
    End Sub

    Private Sub bttnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bttnAdd.Click
        adding = True
        ClearFields()
        HideButtons()
    End Sub

    Private Sub bttnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnFirst.Click
        currentContact = 0
        ShowContact()
    End Sub

    Private Sub bttnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnLast.Click
        currentContact = MyContacts.Count - 1
        ShowContact()
    End Sub

    Private Sub bttnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnPrevious.Click
        If currentContact > 0 Then
            currentContact -= 1
        End If
        ShowContact()
    End Sub

    Private Sub bttnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnNext.Click
        If currentContact < MyContacts.Count - 1 Then
            currentContact += 1
        End If
        ShowContact()
    End Sub

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        ' F10 displays the number of contacts
        If e.KeyCode = Keys.F10 Then
            MsgBox("There are " & MyContacts.Count.ToString & _
                      " contacts in the database")
            e.Handled = True
        End If
        ' ALT+ takes you to next contact, ALT- takes you to previous contact
        If e.KeyCode = Keys.Subtract And e.Modifiers = Keys.Alt Then
            bttnPrevious_Click(sender, e)
        End If
        If e.KeyCode = Keys.Add And e.Modifiers = Keys.Alt Then
            bttnNext_Click(sender, e)
        End If
    End Sub

    Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        If Not bttnOK.Visible Then
            e.Handled = True
        End If
    End Sub

    Private Sub FileSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileSave.Click
        Dim saveFile As FileStream
        saveFile = File.Create("CONTACTS.BIN")
        saveFile.Seek(0, SeekOrigin.End)
        Dim Formatter As BinaryFormatter = New BinaryFormatter()
        Formatter.Serialize(saveFile, MyContacts)
        saveFile.Close()
        Formatter = Nothing
    End Sub

    Private Sub MenuItemfileLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemfileLoad.Click
        Dim readFile As FileStream
        readFile = File.OpenRead("CONTACTS.BIN")
        Dim Formatter As New BinaryFormatter()
        MyContacts = CType(Formatter.Deserialize(readFile), ArrayList)
        readFile.Close()
        Formatter = Nothing
        currentContact = 0
        ShowContact()
    End Sub

    Private Sub FileNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileNew.Click
        MyContacts.Clear()
    End Sub

    Private Sub FileExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileExit.Click
        End
    End Sub

    Sub SaveContact()
        Dim contact As Contact
        contact = CType(MyContacts.Item(currentContact), Contact)
        contact.Company = txtCompany.Text
        contact.Name = txtContact.Text
        contact.Address1 = txtAddress1.Text
        contact.Address2 = txtAddress2.Text
        contact.City = txtCity.Text
        contact.State = txtState.Text
        contact.ZIP = txtZIP.Text
        contact.Tel = txtTel.Text
        contact.EMail = txtEMail.Text
        contact.URL = txtURL.Text
        If adding Then
            currentContact = MyContacts.Add(contact)
        Else
            MyContacts.Item(currentContact) = contact
        End If
    End Sub

    Private Sub bttnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnEdit.Click
        If currentContact <= 0 Then
            MsgBox("Please select a contact to edit")
            Exit Sub
        End If
        adding = False
        HideButtons()
    End Sub

    ' Helper routines to show contact, show/hide editing buttons 
    Sub ShowContact()
        Dim contact As New Contact()
        contact = CType(MyContacts(currentContact), Contact)
        txtCompany.Text = contact.Company
        txtContact.Text = contact.Name
        txtAddress1.Text = contact.Address1
        txtAddress2.Text = contact.Address2
        txtCity.Text = contact.City
        txtState.Text = contact.State
        txtZIP.Text = contact.ZIP
        txtTel.Text = contact.Tel
        txtEMail.Text = contact.EMail
        txtURL.Text = contact.URL
    End Sub

    Sub HideButtons()
        UnlockControls()
        bttnAdd.Visible = False
        bttnDelete.Visible = False
        bttnEdit.Visible = False
        bttnFirst.Visible = False
        bttnLast.Visible = False
        bttnPrevious.Visible = False
        bttnNext.Visible = False
        bttnOK.Visible = True
        bttnCancel.Visible = True
    End Sub

    Sub LockControls()
        txtCompany.ReadOnly = True
        txtContact.ReadOnly = True
        txtAddress1.ReadOnly = True
        txtAddress2.ReadOnly = True
        txtCity.ReadOnly = True
        txtState.ReadOnly = True
        txtZIP.ReadOnly = True
        txtTel.ReadOnly = True
        txtEMail.ReadOnly = True
        txtURL.ReadOnly = True
    End Sub

    Sub UnlockControls()
        txtCompany.ReadOnly = False
        txtContact.ReadOnly = False
        txtAddress1.ReadOnly = False
        txtAddress2.ReadOnly = False
        txtCity.ReadOnly = False
        txtState.ReadOnly = False
        txtZIP.ReadOnly = False
        txtTel.ReadOnly = False
        txtEMail.ReadOnly = False
        txtURL.ReadOnly = False
    End Sub

    Sub ShowButtons()
        LockControls()
        bttnAdd.Visible = True
        bttnDelete.Visible = True
        bttnEdit.Visible = True
        bttnFirst.Visible = True
        bttnLast.Visible = True
        bttnPrevious.Visible = True
        bttnNext.Visible = True
        bttnOK.Visible = False
        bttnCancel.Visible = False
    End Sub

    Sub ClearFields()
        txtCompany.Text = ""
        txtContact.Text = ""
        txtAddress1.Text = ""
        txtAddress2.Text = ""
        txtCity.Text = ""
        txtZIP.Text = ""
        txtState.Text = ""
        txtTel.Text = ""
        txtEMail.Text = ""
        txtURL.Text = ""
    End Sub

End Class

⌨️ 快捷键说明

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