Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Example of how to use the ListView componet
01-02-2010, 10:16 AM (This post was last modified: 01-02-2010 10:19 AM by codecaine.)
Post: #1
Example of how to use the ListView componet
sourcecode attached in zip
[Image: wa1tl.png]
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);
            }
        }

    }
}


Attached File(s)
.zip  ListViewExamp.zip (Size: 45.43 KB / Downloads: 1)
Visit this user's website Find all posts by this user
Quote this message in a reply
01-04-2010, 01:17 AM
Post: #2
RE: Example of how to use the ListView componet
I think the list and tree view are the more advance vcl people don't take the time to really learn.
Visit this user's website Find all posts by this user
Quote this message in a reply
01-04-2010, 03:15 AM
Post: #3
RE: Example of how to use the ListView componet
Yeah i think the same. I was learning those in Delphi and they have loads of functions inside. You can easily develop what you need.
I haven't studied them in C#, but i think they have good solutions for them !

"I dont know with what weapons World War 3 will be fought with, but i know World War 4 will be fought with stones and sticks" - Albert Einstein
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: