Use of ListView and ContextMenuStrip in C#winform

Source: Internet
Author: User

1. Create a new Windows Forms application

2. Add folder and picture Resources

folder Pictures (folder.png), file Pictures (file.png)

3. Adding controls in design mode

namely:1 ListView (lvwdata), 2 ImageList (imglarge,imgsmall), 1 ContextMenuStrip ( Cmsstyle), as shown below:

4. Setting individual Control detail properties

2 x ImageList (Imglarge,imgsmall) Add Images, set ImageSize(imglarge--32*32, imgsmall-- 16*16), as follows:

ContextMenuStrip (cmsstyle) Add Items, as follows:

ListView (Lvwdata) to add Items,Columns, Bindings contextmenustrip(cmsstyle),largeimagelist and smallimagelist


It's a good habit to lock all the controls after the interface design is complete ~ ~

5. Add event-handling logic for each subkey of ContextMenuStrip (Cmsstyle)

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Drawing;6 usingSystem.Linq;7 usingSystem.Text;8 usingSystem.Windows.Forms;9 Ten namespaceControls One { A      Public Partial classMainform:form -     { -         /// <summary> the         ///default constructor -         /// </summary> -          Publicmainform () -         { + InitializeComponent (); -  +              This. Large Icon Toolstripmenuitem.click + =NewSystem.EventHandler ( This. Toolstripmenuitem_click); A              This. more information Toolstripmenuitem.click + =NewSystem.EventHandler ( This. Toolstripmenuitem_click); at              This. tiled Toolstripmenuitem.click + =NewSystem.EventHandler ( This. Toolstripmenuitem_click); -              This. Small icon Toolstripmenuitem.click + =NewSystem.EventHandler ( This. Toolstripmenuitem_click); -              This. List Toolstripmenuitem.click + =NewSystem.EventHandler ( This. Toolstripmenuitem_click); -         } -  -         #regionright mouse button tick event handling in         /// <summary> -         ///Set the ListView list display style by setting the right-click menu Check Option to         /// </summary> +         /// <param name= "Sender" ></param> -         /// <param name= "E" ></param> the         Private voidToolstripmenuitem_click (Objectsender, EventArgs e) *         { $ToolStripMenuItem Tsmenumitem = Sender asToolStripMenuItem;Panax Notoginseng             if(tsmenumitem.checked) -             { the                 return;//returned if selected +             } A             Else the             { +                 //clear the Checkmark right-click menu item - clearcheckstate (cmsstyle); $                 //tick the mouse-selected right-click menu item $tsmenumitem.checked =true; -             } -             //set ListView list Display style the Setdisplaystyle (tsmenumitem.text); -         }Wuyi  the         /// <summary> -         ///clear the Checkmark right-click menu item Wu         /// </summary> -         /// <param name= "CMS" >Right-click menu</param> About         Private voidclearcheckstate (ContextMenuStrip cms) $         { - ToolStripMenuItem tsmenumitemtemp; -              for(inti =0; I < CMS. Items.Count; i++) -             { A                 if(! (CMS. Items[i] isToolStripMenuItem)) +                 { the                     Continue; -                 } $Tsmenumitemtemp = cms. Items[i] asToolStripMenuItem; the                 if(tsmenumitemtemp.checked) the                 { thetsmenumitemtemp.checked =false; the                 } -             } in         } the  the         /// <summary> About         ///set ListView list Display style the         /// </summary> the         /// <param name= "name" ></param> the         Private voidSetdisplaystyle (stringname) +         { -             foreach(ListViewItem LVIinchlistdata.items) the             {Bayi                 if(Name = ="More Information") the                 { theLvi. ImageIndex =1; -                 } -                 Else the                 { theLvi. ImageIndex =0; the                 } the             } -             Switch(name) the             { the                  Case "Large Icons": theListdata.view =View.LargeIcon;94                      Break; the                  Case "More Information": theListdata.view =View.Details; the                      Break;98                  Case "Small Icons": AboutListdata.view =View.SmallIcon; -                      Break;101                  Case "List":102Listdata.view =view.list;103                      Break;104                  Case "tiled": theListdata.view =View.tile;106                      Break;107             }108         }109         #endregion the     }111}

6. The operating effect is as follows:

Use of ListView and ContextMenuStrip in C#winform

Related Article

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.