sourcecode attached in zip
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//check if the first or last name textbox is empty if so produce a error message
if (tbFirst.Text.Trim().Equals("") || tbLast.Text.Trim().Equals(""))
{
MessageBox.Show("Error: You can't not have any blank fields when adding to the database");
}
else
{
/*Create a new ListViewItem to add a row to ListView
*The initialization is the first name and the 2 subitems are last name and age
*/
ListViewItem row = new ListViewItem(tbFirst.Text.Trim());
row.SubItems.Add(tbLast.Text.Trim());
row.SubItems.Add(numberCounter.Value.ToString());
listView1.Items.Add(row);
//clear input data to default
tbFirst.Clear();
tbLast.Clear();
numberCounter.Value = 18;
}
}
private void button2_Click(object sender, EventArgs e)
{
//Clear the listbox of any contents
lbResults.Items.Clear();
//loop through list view and if it is a selected item add the data to the listbox;
for (int x = 0; x < listView1.Items.Count; x++)
{
if (listView1.Items[x].Selected == true)
lbResults.Items.Add("First Name: " + listView1.Items[x].Text + ", Last name: " + listView1.Items[x].SubItems[1].Text + " Age:" + listView1.Items[x].SubItems[2].Text);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
}
private void groupBox2_Enter(object sender, EventArgs e)
{
}
private void button1_Click_1(object sender, EventArgs e)
{
//loop through list view and if it is a selected item remove it from the listview
for (int x = 0; x < listView1.Items.Count; x++)
{
if (listView1.Items[x].Selected == true)
listView1.Items[x].Remove();
}
}
private void bnDeletef_Click(object sender, EventArgs e)
{
//Check if there is text in the firstname textbox
if (tbFirst.Text.Trim().Equals(""))
MessageBox.Show("Error: You must have a firstname in the first name textbox");
else
{
//loop through list view and if the firstname in the listview equals the textbox firstname remove it
for (int x = 0; x < listView1.Items.Count; x++)
{
if (listView1.Items[x].Text.ToUpper().Equals(tbFirst.Text.ToUpper())) //ignores cases sensitivity
listView1.Items[x].Remove();
}
}
}
private void button1_Click_2(object sender, EventArgs e)
{
//Check if there is text in the lastname textbox
if (tbLast.Text.Trim().Equals(""))
MessageBox.Show("Error: You must have a last name in the last name textbox");
else
{
//loop through list view and if the lastname in the listview equals the textbox lastname remove it
for (int x = 0; x < listView1.Items.Count; x++)
{
if (listView1.Items[x].SubItems[1].Text.ToUpper().Equals(tbLast.Text.ToUpper())) //ignores cases sensitivity
listView1.Items[x].Remove();
}
}
}
private void bnDeleteA_Click(object sender, EventArgs e)
{
//loop through list view and if the age in the listview equals the numberbox age remove it
for (int x = 0; x < listView1.Items.Count; x++)
{
if (listView1.Items[x].SubItems[2].Text.Equals(numberCounter.Value.ToString()))
listView1.Items[x].Remove();
}
}
private void bnFindfirst_Click(object sender, EventArgs e)
{
//Check if there is text in the firstname textbox
if (tbFirst.Text.Trim().Equals(""))
MessageBox.Show("Error: You must have a firstname in the first name textbox");
else
{
//change result group box caption
gbResults.Text = "Search Results by First Name:";
//clear listbox
lbResults.Items.Clear();
//Find people in the listview by first name
for (int x = 0; x < listView1.Items.Count; x++)
{
if (listView1.Items[x].Text.ToUpper().Equals(tbFirst.Text.ToUpper())) //ignores cases sensitivity
lbResults.Items.Add("First Name: " + listView1.Items[x].Text + ", Last name: " + listView1.Items[x].SubItems[1].Text + " Age:" + listView1.Items[x].SubItems[2].Text);
}
}
}
private void bnFindlast_Click(object sender, EventArgs e)
{
//Check if there is text in the lastname textbox
if (tbLast.Text.Trim().Equals(""))
MessageBox.Show("Error: You must have a lastname in the last name textbox");
else
{
//change result group box caption
gbResults.Text = "Search Results by Last Name:";
//clear listbox
lbResults.Items.Clear();
//Find people in the listview by last name
for (int x = 0; x < listView1.Items.Count; x++)
{
if (listView1.Items[x].SubItems[1].Text.ToUpper().Equals(tbLast.Text.ToUpper())) //ignores cases sensitivity
lbResults.Items.Add("First Name: " + listView1.Items[x].Text + ", Last name: " + listView1.Items[x].SubItems[1].Text + " Age:" + listView1.Items[x].SubItems[2].Text);
}
}
}
private void bnFindage_Click(object sender, EventArgs e)
{
//change result group box caption
gbResults.Text = "Search Results by Age:";
//clear listbox
lbResults.Items.Clear();
//Find people in the listview by age
for (int x = 0; x < listView1.Items.Count; x++)
{
if (listView1.Items[x].SubItems[2].Text.Equals(numberCounter.Value.ToString()))
lbResults.Items.Add("First Name: " + listView1.Items[x].Text + ", Last name: " + listView1.Items[x].SubItems[1].Text + " Age:" + listView1.Items[x].SubItems[2].Text);
}
}
}
}