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