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

📄 frmaddress.vb

📁 个人Contact管理程序 VB.NET
💻 VB
📖 第 1 页 / 共 3 页
字号:
        Me.txtSearchbyName.Location = New System.Drawing.Point(113, 22)
        Me.txtSearchbyName.Name = "txtSearchbyName"
        Me.txtSearchbyName.Size = New System.Drawing.Size(188, 20)
        Me.txtSearchbyName.TabIndex = 0
        Me.txtSearchbyName.Text = ""
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(16, 24)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(100, 16)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Search by Name:"
        '
        'frmPilgrim
        '
        Me.BackColor = System.Drawing.Color.AliceBlue
        Me.Controls.Add(Me.GroupBox1)
        Me.Controls.Add(Me.GroupBox2)
        Me.Controls.Add(Me.pnlData)
        Me.Name = "frmPilgrim"
        Me.Size = New System.Drawing.Size(784, 552)
        Me.GroupBox1.ResumeLayout(False)
        CType(Me.grdData, System.ComponentModel.ISupportInitialize).EndInit()
        Me.Panel1.ResumeLayout(False)
        Me.pnlData.ResumeLayout(False)
        Me.GroupBox2.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        btnCancel.Enabled = False
        btnSave.Enabled = False

        btnAdd.Enabled = True
        btnEdit.Enabled = True
        btnDelete.Enabled = True
        pnlData.Enabled = False

        txtSearchbyName.Text = ""

        LoadCurrentItem()
    End Sub

    Private Sub frmPilgrim_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadGrid()
        txtSearchbyName.Focus()
    End Sub

    Private Sub LoadGrid()
        Dim data As DataTable = AddressManager.GetAddresses(" status <> " & eStatus.Deleted & " AND name like '%" & txtSearchbyName.Text & "%'")
        grdData.DataSource = data

        Dim dsn As String = System.Configuration.ConfigurationSettings.AppSettings("dsn")
        Utils.PopulateCombo(cmbCategory, dsn, "Category", "CategoryName", "CategoryId", 1)

        LoadCurrentItem()

        If grdData.CurrentRowIndex = -1 Then
            btnCancel.Enabled = False
            btnEdit.Enabled = False
            btnSave.Enabled = False
            btnDelete.Enabled = False
        End If
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        pnlData.Enabled = True
        pageAction = "ADD"

        btnSave.Enabled = True
        btnCancel.Enabled = True

        btnAdd.Enabled = False
        btnEdit.Enabled = False
        btnDelete.Enabled = False

        ClearData()
        txtName.Focus()
    End Sub

    Public Function ClearData()
        txtName.Text = ""
        txtAddress.Text = ""
        txtEmail.Text = ""
        txtHomePhone.Text = ""
        txtCellPhone.Text = ""
        txtWorkPhone.Text = ""
        txtUrl.Text = ""
        txtRemarks.Text = ""

        cmbCategory.SelectedIndex = 0

        dtBirthDay.Value = DateTime.Today
        dtBirthDay.Checked = False
    End Function

    Private Sub LoadCurrentItem()
        If grdData.CurrentRowIndex() = -1 Then
            Return
        End If

        Dim Id As Int32 = Int32.Parse(grdData.Item(grdData.CurrentRowIndex(), 0).ToString())
        Dim ad As Address = AddressManager.GetAddress(Id)

        If ad Is Nothing Then
            Return
        End If

        txtName.Text = ad.Name
        txtAddress.Text = ad.Address
        txtEmail.Text = ad.Email
        txtUrl.Text = ad.URL
        txtHomePhone.Text = ad.HomePhone
        txtCellPhone.Text = ad.CellPhone
        txtWorkPhone.Text = ad.WorkPhone
        txtRemarks.Text = ad.Remarks

        cmbCategory.SelectedValue = ad.CategoryId

        ' Bad way of checking if date was saved or not... to be corrected later.
        ' BBut it works for now !!!
        If ad.DateOfBirth.Date.Year = "1899" Then
            dtBirthDay.Checked = False
        Else
            dtBirthDay.Checked = True
            dtBirthDay.Value = ad.DateOfBirth
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If pageAction = "ADD" Then
            Dim ad As New Address
            ad.ID = IdManager.GetNextId("Address", "ID")
            ad.Name = Utils.ReplaceEscapeChars(txtName.Text)
            ad.Address = Utils.ReplaceEscapeChars(txtAddress.Text)
            ad.Email = Utils.ReplaceEscapeChars(txtEmail.Text)
            ad.URL = Utils.ReplaceEscapeChars(txtUrl.Text)
            ad.HomePhone = Utils.ReplaceEscapeChars(txtHomePhone.Text)
            ad.CellPhone = Utils.ReplaceEscapeChars(txtCellPhone.Text)
            ad.WorkPhone = Utils.ReplaceEscapeChars(txtWorkPhone.Text)
            ad.Remarks = Utils.ReplaceEscapeChars(txtRemarks.Text)
            ad.Status = eStatus.Approved

            ad.CategoryId = cmbCategory.SelectedValue

            If dtBirthDay.Checked Then
                ad.DateOfBirth = dtBirthDay.Value
            Else
                ad.DateOfBirth = Date.MinValue
            End If

            AddressManager.CreateAddress(ad)

            MessageBox.Show("Saved Successfully", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            Dim Id As Int32 = Int32.Parse(grdData.Item(grdData.CurrentRowIndex(), 0).ToString())

            Dim ad As Address = AddressManager.GetAddress(Id)

            ad.Name = Utils.ReplaceEscapeChars(txtName.Text)
            ad.Address = Utils.ReplaceEscapeChars(txtAddress.Text)
            ad.Email = Utils.ReplaceEscapeChars(txtEmail.Text)
            ad.URL = Utils.ReplaceEscapeChars(txtUrl.Text)
            ad.HomePhone = Utils.ReplaceEscapeChars(txtHomePhone.Text)
            ad.CellPhone = Utils.ReplaceEscapeChars(txtCellPhone.Text)
            ad.WorkPhone = Utils.ReplaceEscapeChars(txtWorkPhone.Text)
            ad.Remarks = Utils.ReplaceEscapeChars(txtRemarks.Text)
            ad.Status = eStatus.Approved

            ad.CategoryId = cmbCategory.SelectedValue

            If dtBirthDay.Checked Then
                ad.DateOfBirth = dtBirthDay.Value
            Else
                ad.DateOfBirth = Date.MinValue
            End If

            AddressManager.UpdateAddress(ad)

            MessageBox.Show("Saved Successfully", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If

        btnCancel.Enabled = False
        btnSave.Enabled = False

        btnAdd.Enabled = True
        btnEdit.Enabled = True
        btnDelete.Enabled = True

        pnlData.Enabled = False

        txtSearchbyName.Text = ""
        LoadGrid()
    End Sub

    Private Sub txtSearchbyName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        LoadGrid()
    End Sub

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

    Private Sub grdData_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdData.CurrentCellChanged
        grdData.Select(grdData.CurrentRowIndex)
        LoadCurrentItem()
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        If grdData.CurrentRowIndex = -1 Then
            MessageBox.Show("You must select a record to edit.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Return
        End If

        pnlData.Enabled = True
        pageAction = "EDIT"

        btnAdd.Enabled = False
        btnCancel.Enabled = True
        btnEdit.Enabled = False
        btnSave.Enabled = True
        btnDelete.Enabled = False
    End Sub


    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If grdData.CurrentRowIndex = -1 Then
            MessageBox.Show("You must select a record to edit.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Return
        End If

        If (MessageBox.Show("Are you sure you want to delete current item ?", "Confirm delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = DialogResult.No) Then
            Return
        End If

        Dim Id As Int32 = Int32.Parse(grdData.Item(grdData.CurrentRowIndex(), 0).ToString())
        Dim ad As Address = AddressManager.GetAddress(Id)

        If ad Is Nothing Then
            Return
        End If

        ad.Status = eStatus.Deleted

        AddressManager.UpdateAddress(ad)
        LoadGrid()

        MessageBox.Show("Successfully Deleted.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub txtSearchbyName_TextChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearchbyName.TextChanged
        LoadGrid()
    End Sub
End Class

⌨️ 快捷键说明

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