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

📄 contactsform.vb

📁 Mastering VBNet Include Source Code
💻 VB
📖 第 1 页 / 共 2 页
字号:
        Me.Label8.TabIndex = 18
        Me.Label8.Text = "&URL"
        '
        'txtAddress1
        '
        Me.txtAddress1.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte))
        Me.txtAddress1.Location = New System.Drawing.Point(304, 112)
        Me.txtAddress1.Name = "txtAddress1"
        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.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte))
        Me.txtAddress2.Location = New System.Drawing.Point(304, 152)
        Me.txtAddress2.Name = "txtAddress2"
        Me.txtAddress2.Size = New System.Drawing.Size(360, 22)
        Me.txtAddress2.TabIndex = 7
        Me.txtAddress2.Text = ""
        '
        '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(440, 336)
        Me.bttnEdit.Name = "bttnEdit"
        Me.bttnEdit.Size = New System.Drawing.Size(96, 32)
        Me.bttnEdit.TabIndex = 21
        Me.bttnEdit.Text = "Edit"
        '
        'bttnOK
        '
        Me.bttnOK.Font = New System.Drawing.Font("Verdana", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte))
        Me.bttnOK.Location = New System.Drawing.Point(568, 336)
        Me.bttnOK.Name = "bttnOK"
        Me.bttnOK.Size = New System.Drawing.Size(96, 32)
        Me.bttnOK.TabIndex = 27
        Me.bttnOK.Text = "OK"
        Me.bttnOK.Visible = False
        '
        'bttnAdd
        '
        Me.bttnAdd.Font = New System.Drawing.Font("Verdana", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte))
        Me.bttnAdd.Location = New System.Drawing.Point(304, 336)
        Me.bttnAdd.Name = "bttnAdd"
        Me.bttnAdd.Size = New System.Drawing.Size(96, 32)
        Me.bttnAdd.TabIndex = 20
        Me.bttnAdd.Text = "Add"
        '
        'ListBox1
        '
        Me.ListBox1.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.ListBox1.ItemHeight = 14
        Me.ListBox1.Location = New System.Drawing.Point(8, 8)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.Size = New System.Drawing.Size(280, 312)
        Me.ListBox1.Sorted = True
        Me.ListBox1.TabIndex = 29
        '
        'bttnCancel
        '
        Me.bttnCancel.Font = New System.Drawing.Font("Verdana", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte))
        Me.bttnCancel.Location = New System.Drawing.Point(304, 336)
        Me.bttnCancel.Name = "bttnCancel"
        Me.bttnCancel.Size = New System.Drawing.Size(96, 32)
        Me.bttnCancel.TabIndex = 28
        Me.bttnCancel.Text = "Cancel"
        Me.bttnCancel.Visible = False
        '
        'txtURL
        '
        Me.txtURL.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte))
        Me.txtURL.Location = New System.Drawing.Point(304, 296)
        Me.txtURL.Name = "txtURL"
        Me.txtURL.Size = New System.Drawing.Size(328, 22)
        Me.txtURL.TabIndex = 19
        Me.txtURL.Text = ""
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.AutoScroll = True
        Me.ClientSize = New System.Drawing.Size(680, 377)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.ListBox1, 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.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

    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
        ListBox1.Enabled = True
        currentContact = ListBox1.SelectedIndex
        If currentContact < 0 Then currentContact = 0
        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()
        ListBox1.Enabled = True
        ShowButtons()
    End Sub

    Private Sub bttnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnDelete.Click
        If currentContact > -1 Then
            ListBox1.Items.RemoveAt(currentContact)
            If currentContact = ListBox1.Items.Count Then currentContact = ListBox1.Items.Count - 1
            If currentContact = -1 Then
                ClearFields()
                MsgBox("There are no more contacts")
            Else
                ShowContact()
            End If
        Else
            MsgBox("Please select a contacts 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()
        ListBox1.Enabled = False
    End Sub

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.F10 Then
            MsgBox("There are " & ListBox1.Items.Count.ToString & _
                      " contacts in the database")
            e.Handled = True
        End If
    End Sub

    Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        ' If we're not in EDIT mode, reject keystrokes
        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()
        Dim AL As New ArrayList()
        Dim itm As Object
        For Each itm In ListBox1.Items
            AL.Add(itm)
        Next
        Formatter.Serialize(saveFile, AL)
        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()
        Dim AL As New ArrayList()
        ' IF THE STRICT OPTION WERE OFF, THE FOLLOWING STATEMENT 
        ' NEED NOT BE CASTED. IT COULD BE WRITTEN AS:
        '   AL = Formatter.Deserialize(readFile)
        AL = CType(Formatter.Deserialize(readFile), ArrayList)
        ListBox1.Items.Clear()
        Dim itm As Object
        For Each itm In AL
            ListBox1.Items.Add(itm)
        Next
        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
        ListBox1.Items.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 New Contact()
        contact.CompanyName = txtCompany.Text
        contact.ContactName = 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
            ListBox1.Items.Add(contact)
        Else
            ListBox1.Items(currentContact) = contact
            ListBox1.Items.RemoveAt(currentContact)
            ListBox1.Items.Add(contact)
        End If
    End Sub

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

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        currentContact = ListBox1.SelectedIndex
        ShowContact()
    End Sub

    ' Helper routines
    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
        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

    Sub ShowContact()
        Dim contact As New Contact()
        contact = CType(ListBox1.Items.Item(currentContact), Contact)
        txtCompany.Text = contact.CompanyName
        txtContact.Text = contact.ContactName
        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
        bttnOK.Visible = True
        bttnCancel.Visible = True
    End Sub

End Class

⌨️ 快捷键说明

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