Listview in C#

In this article, we will learn how to use listview and imagelist in windows form in C#.

Let's create windows form.

Step 1
In Visual Studio select "File" = > "New" = > "Project..." then select C# Windows Forms Application then click Ok.

create windows form

Step 2
Let's create listview then you go "View" = > select "ToolBox" (Crtl+Alt+X) = > then select "All Windows Forms" = > Choose "ListView".

Label
  1. First of all, we'll know about the "listview".

    ListView display a collection of items in one of five different views. We'll discuss about properties and function and events of listview write down.

    Form1.cs file:
    
                                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 WindowsFormsApplication4
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                //listview fill the whole form
                listView1.Dock = DockStyle.Fill;
                
                //let's create a imagelist
                ImageList il = new ImageList();
    
                
                //Images adding and image key 1 provide
                il.Images.Add("1",new Bitmap(@"C:\Users\subra\Desktop\Flowers\combos\1.jpg"));
    
                //Images adding and image key 2 provide
                il.Images.Add("2", new Bitmap(@"C:\Users\subra\Desktop\Flowers\combos\2.jpg"));
    
                //Images adding and image key 3 provide
                il.Images.Add("3", new Bitmap(@"C:\Users\subra\Desktop\Flowers\combos\3.jpg"));
    
                //Images adding and image key 4 provide
                il.Images.Add("4", new Bitmap(@"C:\Users\subra\Desktop\Flowers\combos\4.jpg"));
    
                //Images adding and image key 5 provide
                il.Images.Add("5", new Bitmap(@"C:\Users\subra\Desktop\Flowers\combos\5.jpg"));
    
                //adding image size which are show the user
                il.ImageSize = new System.Drawing.Size(100, 100);
    
                //adding the image list in listview largeimagelist
                //when the listview display items the large icon view
                listView1.LargeImageList = il;
    
                //adding the image list in listview smallimagelist
                //when the listview display items the small icon view
                listView1.SmallImageList = il;
    
                //loop continues to adding items 
                for (int i = 1; i <= il.Images.Count; i++)
                {
                    listView1.Items.Add("Chandan" + i.ToString(), i.ToString());
                }
                
            }
        }
    }
    
    Output :
    create windows form

    "BackColor" changes the background color of listview control.

    listView1.BackColor = Color.Blue;
    

    "BorderStyle" changes the style of listView control border.

    listView1.BorderStyle = BorderStyle.FixedSingle;
    

    "Click" when user mouse click on listView control.

    listView1.Click+=new EventHandler(listView1_Click);
    private void listView1_Click(object sender, EventArgs e)
    {
    }
    

    "Cursor" when user mouse hover on listView1 control then cursor style changed.

    listView1.Cursor = Cursors.Hand;
    

    "Dispose()" released all resources used by the System.ComponentModel.Component.

    listView1.Dispose();
    

    "DoubleClick" when user mouse double click on listView control.

    listView1.DoubleClick+=new EventHandler(listView1_DoubleClick);
    private void listView1_DoubleClick(object sender, EventArgs e)
    {
    }
    

    "Enabled" changes the listView control are enable or not.

    listView1.Enabled = true;
    

    "Focus()" focusing the listView control.

    listView1.Focus();
    

    "Font" changes the style of the text of listView control.

    listView1.Font = new System.Drawing.Font("arial", 14.0F, FontStyle.Bold);
    

    "ForeColor" changes the font color of listView control.

    listView.ForeColor = Color.AliceBlue;
    

    "Height" set the height of listView control.

    listView.Height=400;
    

    "Hide()" hides the listView control.

    listView1.Hide();
    

    "MouseClick" when user click the listView by mouse.

    listView1.MouseClick+=new MouseEventHandler(listView1_MouseClick);
    private void listView1_MouseClick(object sender, MouseEventArgs e)
    {
    }
    

    "MouseDoubleClick" when user double click the by mouse.

    listView1.MouseDoubleClick += new MouseEventHandler(listView1_MouseDoubleClick);
    private void listView1_MousuDoubleClick(object sender, KeyPressEventArgs e)
    {
    }
    

    "MouseEnter" when mouse pointer enters the listView control.

    listView1.MouseEnter+=new EventHandler(listView1_MouseEnter); 
    private void listView1_MouseEnter(object sender, EventArgs e) 
    { 
    }
    

    "MouseHover" when mouse pointer rests/hover on the listView control.

    listView1.MouseHover+=new EventHandler(listView1_MouseHover);
    private void listView1_MouseHover(object sender, EventArgs e)
    {
    }
    

    "MouseLeave" when mouse pointer leaves the listView control.

    listView1.MouseLeave+=new EventHandler(listView1_MouseLeave);
    private void listView1_MouseLeave(object sender, EventArgs e)
    {
    }
    

    "MouseMove" when mouse pointer moves the listView1 control.

    listView1.MouseMove+=new MouseEventHandler(listView1_MouseMove);
    private void listView1_MouseMove(object sender, MouseEventArgs e)
    {
    }
    

    "ResetText()" resets all properties of listView to its default values.

    listView1.ResetText();
    

    "Show()" displays the listView control.

    listView1.Show();
    

    "Items.Add()" adding items in listview.

    listView1.Items.Add("Items");
    

    "Visible" gets or sets the listView control visible or not.

    listView1.Visible = true;
    


Write a comment


Name Email Message