The ListView control for Windows application advanced controls

Source: Internet
Author: User

ListView Control---List view control


Purpose: Displays a list of items with icons, which can display large icons, small icons, and data


Common properties of the ListView control:

View Properties: Sets how items are displayed in the control, and the value of the View property has the following

Details each item is displayed on a different line

LargeIcon each item is displayed as one of the largest icons, labeled below, is the default view mode

List each item is displayed as a small icon, with a label on the right, items arranged in columns, no list header

SmallIcon each item appears as a small icon with a label on the right

Title Displays full-size icons with item labels and subkey information on the right


1. Add Item: The Add method of the Item property of the ListView control

Public virtual ListViewItem Add (String text,int imageindex)


Instance Part code:

        private void Button1_Click (object sender, EventArgs e)        {            if (TextBox1.Text = = "")            {                MessageBox.Show (" Item cannot be empty ");            }            else            {                listView1.Items.Add (TextBox1.Text.Trim ());            }        }

Run:





2. Remove items (this is similar to the ImageList control in the previous blog)

The RemoveAt method is used to remove the item at the specified index in the collection

The clear method is used to remove all items from the collection


Instance Part code:

private void Button1_Click (object sender, EventArgs e) {if (TextBox1.Text = = "") {            MessageBox.Show ("Project cannot be empty");                } else {ListView1.Items.Add (TextBox1.Text.Trim ());            TextBox1.Text = ""; }} private void Button3_Click (object sender, EventArgs e) {if (ListView1.Items.Count = =            0) {MessageBox.Show ("No Project in Project");            } else {listView1.Items.Clear (); }} private void Button2_Click (object sender, EventArgs e) {if (listview1.selecteditems.c            Ount = = 0) {MessageBox.Show ("Please select the item to be deleted"); } else {listView1.Items.RemoveAt (listview1.selecteditems[0].                Index);            ListView1.SelectedItems.Clear (); }        }


Run:


Use the RemoveAt () method to remove the Minority Report:




Remove all from clear ():







3. Select an item in the ListView control

Use its selected property to set or get a value that specifies whether this item is selected.

LISTVIEW1.ITEMS[2]. selected=true;//use the Selected method to select item 3rd





4. Add an icon to an item in the ListView control


This is going to use the ImageList control that was previously spoken.

List view, details view, and SmallIcon view display images from the image list specified in the SmallImageList property

LargeIcon view displays images from the image list specified in the LargeImageList property

List view displays a set of additional icons in the StateImageList property next to a large icon or small icon


Implementation steps:

(1) set the corresponding attribute (SmallImageList, largeimagelist, or StateImageList) to the existing ImageList control you want to use

(2) Set the ImageIndex property or the Stateimageindex property for each list item that has an associated icon, which can be set in code again or in the ListViewItem Collection editor.


Instance code:

(1) Where the LargeImageList property of the ListView control is set and the SmallImageList property is ImageList1 control

(2) Add an image to the ImageList control

(3) Finally add two items to the ListView control

(4) Set the ImageIndex properties of these two items to 0 and 1, respectively


        private void Form1_Load (object sender, EventArgs e)        {            listview1.largeimagelist = imageList1;            Imagelist1.imagesize = new Size (37,36);            IMAGELIST1.IMAGES.ADD (Image.FromFile ("01.png"));            IMAGELIST1.IMAGES.ADD (Image.FromFile ("02.png"));            Listview1.smallimagelist = ImageList1;            LISTVIEW1.ITEMS.ADD ("Mission Spy");            LISTVIEW1.ITEMS.ADD ("World War");            <span style= "color: #ff0000;" >listview1.items[0]. ImageIndex = 0;            LISTVIEW1.ITEMS[1]. ImageIndex = 1;</span>        }


Run:





5. Enable tiled view in the ListView control

The ListView control's View property is set to tile, and the tile size can also be adjusted by the Tilesize property


Instance code:

private void Form1_Load (object sender, EventArgs e) {Listview1.view = view.tile;//Setting the View property of the ListView1 control            Sets the LargeImageList property of the control whose large icon is selected in the ImageList1 control listview1.largeimagelist = ImageList1;            Add two pictures to the ImageList1 control ImageList1.Images.Add (Image.FromFile ("1.bmp"));            IMAGELIST1.IMAGES.ADD (Image.FromFile ("2.bmp"));            Add Project ListView1.Items.Add to the control ("Mission Spy");            LISTVIEW1.ITEMS.ADD ("World War");            ListView1.Items.Add ("Tomorrow's Edge");            LISTVIEW1.ITEMS.ADD ("Minority Report");            LISTVIEW1.ITEMS.ADD ("The Last Samurai"); Sets the icon for the item in the control Listview1.items[0].            ImageIndex = 0; LISTVIEW1.ITEMS[1].            ImageIndex = 1; LISTVIEW1.ITEMS[2].            ImageIndex = 0; LISTVIEW1.ITEMS[3].            ImageIndex = 1; LISTVIEW1.ITEMS[4].            ImageIndex = 0; Listview1.tilesize = new Size (200, 50);//Set the Tilesize property of the ListView1 control}


Run:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.