Extends ListViewItem : ListView « GUI Windows Form « C# / C Sharp






Extends ListViewItem


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class Form1 : System.Windows.Forms.Form {
  private System.Windows.Forms.ListView lvCountries;
    private System.Windows.Forms.ImageList imgLarge;
    private System.Windows.Forms.ImageList imgSmall;
    private System.Windows.Forms.ComboBox cbView;
    private System.Windows.Forms.Label lblAbbreviation;
    private System.ComponentModel.IContainer components;

  public Form1() {
    InitializeComponent();
    lvCountries.Items.Add(new CountryItem("United States", "US", "Dollar"));
        lvCountries.Items[0].ImageIndex = 0;
        lvCountries.Items.Add(new CountryItem("Great Britain", "GB", "Pound"));
        lvCountries.Items[1].ImageIndex = 1;
        lvCountries.Items.Add(new CountryItem("Canada", "CA", "Dollar"));
        lvCountries.Items[2].ImageIndex = 2;
        lvCountries.Items.Add(new CountryItem("Japan", "JP", "Yen"));
        lvCountries.Items[3].ImageIndex = 3;
        lvCountries.Items.Add(new CountryItem("Germany", "GM", "Deutch Mark"));
        lvCountries.Items[4].ImageIndex = 4;

        cbView.Items.Add(View.LargeIcon);
        cbView.Items.Add(View.SmallIcon);
        cbView.Items.Add(View.List);
        cbView.Items.Add(View.Details);
        cbView.SelectedIndex = 0;

        lvCountries.Columns.Add("Country",100, HorizontalAlignment.Left);
        lvCountries.Columns.Add("Currency",100, HorizontalAlignment.Left);
    }
    private void InitializeComponent() {
      this.components = new System.ComponentModel.Container();
      this.lvCountries = new System.Windows.Forms.ListView();
      this.imgLarge = new System.Windows.Forms.ImageList(this.components);
      this.imgSmall = new System.Windows.Forms.ImageList(this.components);
      this.cbView = new System.Windows.Forms.ComboBox();
      this.lblAbbreviation = new System.Windows.Forms.Label();
      this.SuspendLayout();

      this.lvCountries.LargeImageList = this.imgLarge;
      this.lvCountries.Location = new System.Drawing.Point(24, 72);
      this.lvCountries.Name = "lvCountries";
      this.lvCountries.Size = new System.Drawing.Size(256, 248);
      this.lvCountries.SmallImageList = this.imgSmall;
      this.lvCountries.TabIndex = 0;
      this.lvCountries.MouseUp += new System.Windows.Forms.MouseEventHandler(this.lvCountries_MouseUp);

      this.imgLarge.ImageSize = new System.Drawing.Size(48, 48);
      this.imgLarge.TransparentColor = System.Drawing.Color.Transparent;

      this.imgSmall.ColorDepth = System.Windows.Forms.ColorDepth.Depth16Bit;
      this.imgSmall.ImageSize = new System.Drawing.Size(16, 16);
      this.imgSmall.TransparentColor = System.Drawing.Color.Transparent;
      // 
      // cbView
      // 
      this.cbView.Location = new System.Drawing.Point(32, 32);
      this.cbView.Name = "cbView";
      this.cbView.Size = new System.Drawing.Size(128, 21);
      this.cbView.TabIndex = 1;
      this.cbView.SelectedIndexChanged += new System.EventHandler(this.cbView_SelectedIndexChanged);
      // 
      // lblAbbreviation
      // 
      this.lblAbbreviation.Location = new System.Drawing.Point(176, 32);
      this.lblAbbreviation.Name = "lblAbbreviation";
      this.lblAbbreviation.Size = new System.Drawing.Size(48, 23);
      this.lblAbbreviation.TabIndex = 2;
      // 
      // Form1
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(312, 339);
      this.Controls.Add(this.lblAbbreviation);
      this.Controls.Add(this.cbView);
      this.Controls.Add(this.lvCountries);
      this.Name = "Form1";
      this.Text = "Form1";
      this.ResumeLayout(false);

    }
    static void Main() {
    Application.Run(new Form1());
  }

    private void cbView_SelectedIndexChanged(object sender, System.EventArgs e)
    {
      lvCountries.View = (View)cbView.SelectedItem;
    }

    private void lvCountries_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {
      if(lvCountries.View != View.Details)
        lblAbbreviation.Text = ((CountryItem)lvCountries.GetItemAt(e.X, e.Y)).CountryAbbreviation;
      else
        lblAbbreviation.Text = ((CountryItem)lvCountries.GetItemAt(5, e.Y)).CountryAbbreviation;
    }

    
  }
  public class CountryItem : System.Windows.Forms.ListViewItem {
    string countryName = "";
    string countryAbbrev = "";

    public CountryItem(string countryName, string countryAbbreviation, string currency)
    {
      countryName = countryName;
      countryAbbrev = countryAbbreviation;
            base.Text = countryName;
            base.SubItems.Add(currency);
    }

    public string CountryName
    {
      get  {return countryName;}
    }

    public string CountryAbbreviation
    {
      get  {return countryAbbrev;}
    }
  }


           
       








Related examples in the same category

1.Windows Explorer-Like Program: extends ListView
2.Dragging and dropping between ListView
3.ListView Item clicked eventListView Item clicked event
4.Folder Browser based on ListViewFolder Browser based on ListView
5.Set ColumnHeader for ListView
6.Sort a List View by Any ColumnSort a List View by Any Column
7.Add Data to a ListView
8.Use RadioButton to control the ListView display styleUse RadioButton to control the ListView display style
9.Display Directory info in a ListViewDisplay Directory info in a ListView
10.Add ListView column and insert ListView rowsAdd ListView column and insert ListView rows
11.Use ListViewItem to display file information
12.ListView ExampleListView Example
13.ListView Country: image and fontListView Country: image and font
14.Use ListView to display File info: name, size and dateUse ListView to display File info: name, size and date
15.Use ListView to display file name and double click the name to execute that fileUse ListView to display file name and double click the name to execute that file
16.Use ListView to diaplay folder info and double click to enter that directoryUse ListView to diaplay folder info and double click to enter that directory