C# MS Access Full Contact Mangement System → Our UI Code – Camposha

C# MS Access Full Contact Mangement System

C# Contact Management Syste,

Our UI Code

In this lesson we will look at the code behind our user interface for our project. This code makes up the view part of our system. The code will mostly be event handlers and user interface code.Note that this lesson is part of the our C# Contacts Management System development course.

FULL CODE:

/View/Manager.cs

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ContactManager.Common;
using ContactManager.Data.Model;
using ContactManager.Data.Repository;
namespace ContactManager
{
public partial class Manager : Form
{
private int id = -1;
private String selectedUser = "";
ContactsRepository contactsRepository=new ContactsRepository();
private Contact selectedContact;
/*
* Here's create our constructor
*/
public Manager()
{
InitializeComponent();
//DATAGRIDVIEW PROPERTIES
contactsDGView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
//SELECTION MODE
contactsDGView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
contactsDGView.MultiSelect = false;
new Paginator(bindingSource1, bindingNavigator1, contactsDGView,
contactsRepository.retrieve().Contacts);
}
/*
* We create a method to clear our edittexts
*/
private void clearTxts()
{
txtName.Text = "";
txtPhoneNo1.Text = "";
txtPhoneNo2.Text = "";
txtEmail.Text = "";
txtRemarks.Text = "";
}
/*
* Listen to save click events
*/
private void btnSave_Click(object sender, EventArgs e)
{
String name = txtName.Text;
String phone1 = txtPhoneNo1.Text;
String phone2 = txtPhoneNo2.Text;
String email = txtEmail.Text;
String remarks = txtRemarks.Text;
//INSERT NEW CONTACT
if (btnSave.Text == @"ADD" || btnSave.Text == @"CLONE")
{
ResultModel resultModel = contactsRepository.add(name,phone1,phone2,email,remarks);
String message = resultModel.Message;
MessageBox.Show(message, @"Contacts Manager", MessageBoxButtons.OK,
MessageBoxIcon.Information);
if (message == "SUCCESS")
{
clearTxts();
resultModel = contactsRepository.retrieve();
List<Contact> contacts =resultModel.Contacts;
if (message == "SUCCESS" && contacts.Count > 0)
{
new Paginator(bindingSource1, bindingNavigator1, contactsDGView, contacts);
}
}
}
//UPDATE EXISTING CONTACT
else if(btnSave.Text == @"UPDATE")
{
ResultModel resultModel = contactsRepository.update(id,name, phone1, phone2,
email, remarks);
String message = resultModel.Message;
MessageBox.Show(message, @"Contacts Manager", MessageBoxButtons.OK,
MessageBoxIcon.Information);
if (message == "SUCCESS")
{
resultModel = contactsRepository.retrieve();
List<Contact> contacts = resultModel.Contacts;
if (message == "SUCCESS" && contacts.Count > 0)
{
new Paginator(bindingSource1, bindingNavigator1, contactsDGView, contacts);
}
}
}
}
/*
* When user clicks a single item/cell in the datagridview
*/
private void contactsDGView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
try
{
int selectedIndex = e.RowIndex;
if (selectedIndex != -1)
{
//we set the selected cells to their respective textboxes
if (contactsDGView.SelectedRows[0].Cells[0].Value != null)
{
id = Convert.ToInt32(contactsDGView.SelectedRows[0].Cells[0].Value.
ToString());
string name = contactsDGView.SelectedRows[0].Cells[1].Value.ToString();
string phone1 = contactsDGView.SelectedRows[0].Cells[2].Value.ToString();
string phone2 = contactsDGView.SelectedRows[0].Cells[3].Value.ToString();
string email = contactsDGView.SelectedRows[0].Cells[4].Value.ToString();
string remarks = contactsDGView.SelectedRows[0].Cells[5].Value.ToString();
mTabs.SelectedTab = tabDetail;
detailName.Text = name;
detailPhone1.Text = phone1;
detailPhone2.Text = phone2;
detailEmail.Text = email;
detailRemarks.Text = remarks;
}
}
}
catch (ArgumentOutOfRangeException ex)
{
MessageBox.Show(ex.Message, @"Contacts Manager", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
/*
* Listen to context menu click events for the datagridview
*/
private void contextMenuDGView_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
string item = e.ClickedItem.Text;
//Open editing tab if user selects `Edit`
if (item == "Edit")
{
if (selectedContact != null)
{
iTabs.SelectedTab = tabManageContacts;
txtName.Text = selectedContact.Name;
txtPhoneNo1.Text = selectedContact.Phone1;
txtPhoneNo2.Text = selectedContact.Phone2;
txtEmail.Text = selectedContact.Email;
txtRemarks.Text = selectedContact.Remarks;
[email protected]"UPDATE";
gBoxContactsEdit.Text = @"Edit Contact";
}
}
//Also open editing page to clone a contact
else if (item == "Clone")
{
if (selectedContact != null)
{
iTabs.SelectedTab = tabManageContacts;
txtName.Text = selectedContact.Name;
txtPhoneNo1.Text = selectedContact.Phone1;
txtPhoneNo2.Text = selectedContact.Phone2;
txtEmail.Text = selectedContact.Email;
txtRemarks.Text = selectedContact.Remarks;
btnSave.Text = @"CLONE";
gBoxContactsEdit.Text = @"Clone Contact";
}
}
//Show user warning dialog before deleting
else if (item == "Delete")
{
if (MessageBox.Show(@"Are you sure to permanently delete "+selectedUser,
@"DELETE", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{
ResultModel resultModel = contactsRepository.delete(id);
String message = resultModel.Message;
MessageBox.Show(message);
if (message == "SUCCESS")
{
resultModel = contactsRepository.retrieve();
List<Contact> contacts = resultModel.Contacts;
if (message == "SUCCESS" && contacts.Count > 0)
{
new Paginator(bindingSource1, bindingNavigator1, contactsDGView,
contacts);
}
}
}
}
}
/*
* When user right clicks,mark the selected contact since we may pass it to another page/tab for editing/cloning etc
*/
private void contactsDGView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
try
{
int rowIndex = e.RowIndex;
if (rowIndex != -1)
{
//set selected contact to respective cells
if (contactsDGView.Rows[e.RowIndex].Cells[0].Value != null)
{
id = (int)contactsDGView.Rows[e.RowIndex].Cells[0].Value;
selectedUser = contactsDGView.Rows[e.RowIndex].Cells[1].Value.ToString();
selectedContact = new Contact
{
Id = id,
Name = contactsDGView.Rows[e.RowIndex].Cells[1].Value.ToString(),
Phone1 = contactsDGView.Rows[e.RowIndex].Cells[2].Value.ToString(),
Phone2 = contactsDGView.Rows[e.RowIndex].Cells[3].Value.ToString(),
Email = contactsDGView.Rows[e.RowIndex].Cells[4].Value.ToString(),
Remarks = contactsDGView.Rows[e.RowIndex].Cells[5].Value.ToString()
};
}
}
}
catch (ArgumentOutOfRangeException ex)
{
MessageBox.Show(ex.Message, @"Contacts Manager", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
/*
* When user clicks export button, export all contacts
*/
private void btnExportExcel_Click(object sender, EventArgs e)
{
contactsRepository=new ContactsRepository();
List<Contact> contacts = contactsRepository.retrieve().Contacts;
if (contacts.Count > 0)
{
String message = Utils.exportToExcel(Utils.GetDataSet(contacts),
Application.StartupPath);
MessageBox.Show(message,@"Contacts Manager",MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
/*
* Listen to tab selection events, anytime user moves to first tab, update the save
button and groupbox to their original
* state. We do this since we are re-using the same form to insert and update.
*/
private void iTabs_SelectedIndexChanged(object sender, EventArgs e)
{
if (iTabs.SelectedIndex == 0)
{
id = -1;
btnSave.Text = @"ADD";
gBoxContactsEdit.Text = @"Add New Contact";
clearTxts();
}
}
/*
* Listen to textbox change events and filter/search our database
*/
private void txtSearch_TextChanged(object sender, EventArgs e)
{
new Paginator(bindingSource1, bindingNavigator1, contactsDGView,
contactsRepository.filter(txtSearch.Text).Contacts);
}
}
}

//end

Leave a Reply

Your email address will not be published. Required fields are marked *

Join Us
X