ListView

xiaoxiao2021-04-01  236

ListView

The ListView control is a more complex control. It can make a variety of view operations (such as large icons, small icons, detailed information, etc.) in its standard list box. Create a new form FORM1 Add a few controls on top: Add two imagelist controls, named Smallil and Largeil, set the Large Imagesize to 32,32, and add two icon to its images, one is Folder icon, one for the file icon. Two such icons are also added for Smallil. Then set the listView's SmallImageList for Smallil, LargeImageList to Largeil, View To Details Next Description ListView Shows Several Points When ListView is displayed as Details: ListView's option list view is an instance of a listviewItem class, then this Options have a sub-options Subitems, if ListView is in Details mode, the sub-option will appear. The difference between the sub-option and the primary option is that the primary option can have icon, and the sub-option is not. ListView's column header Columns is also equivalent to column names. To display a list of questions, add the ColumnHead's instance to the listView's columns. In the ListView in Details mode, columnsheader provides a title for ListView. You can now start programming.

We first write a method, create title private void createheader () // for listView Add column name {ColumnHeader CH = New ColumnHeader (); ch.text = "file name"; this.listview1.column.add (ch) CH = new color (); ch.text = "size"; this.listview1.columns.add (ch); ch = new colornheader (); ch.Text = "Modification date"; this.listview1.columns.add (ch);} and then write a method to add a tab private void createitem (string root) {ListViewItem lvi as listview; ListViewItem.ListViewSubItem lvsi; System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo (root); System. IO.DirectoryInfo [] DIRS = DIR.GETDIRECTORIES (); system.io.fileinfo [] files = dir.getfiles (); listview1.cle (); // Note This function is the name of all options in the listview Remove listview1.beginupdate (); foreach (system.io.directoryInfo di in dirs) {lvi = new listviewItem (); lvi.text = di.name; lvi.tag = di.fullname; lvi.imageIndex = 0; lvsi = New System.windows.Forms.listviewItem.ListViewSubitem (); lvsi.text = ""; lvi.subitems.add (lvsi); lvsi = new system.windows.Forms.listViewItem.ListViewSub Item (); lvsi.text = di.lastaccesstime.tostring (); lvi.subitems.add (lvsi); this.listview1.items.add (lvi);} foreach (system.io.fileinfo fi in files) // Add file information to the list of ListView {lvi = new listviewItem (); lvi.text = FI.NAME; lvi.imageIndex = 1; lvi.tag = fi.fullname; lvsi = new system.windows.Forms.listViewItem. ListViewSubItem (); lvsi.Text = fi.Length.ToString (); lvi.SubItems.Add (lvsi); lvsi = new System.Windows.Forms.ListViewItem.ListViewSubItem (); lvsi.Text = fi.LastAccessTime.ToString ( ); Lvi.subitems.add (lvsi); this.listview1.items.Add (LVI);

} This.listview1.endupdate (); then add: public form1 () {createtem (@ "c: /"); // Note that the order of these two functions cannot be reversed because CreateItem has A command listView1.clear () deletes all column names, if CreateHeader is in front, ListView is not column name. CREATEHEADER ();} Now run the program, you can see all the folders and file information in ListView, add four radio buttons to event handler Private Void Rdosmall_CheckedChanged (Object Sender, System) .EventArgs e) {if (this.rdosmall.Checked == true) {this.listView1.View = View.SmallIcon;}} private void rdolarge_CheckedChanged (object sender, System.EventArgs e) {if (this.rdolarge.Checked = = true) {this.listView1.View = View.LargeIcon;}} private void rdodetails_CheckedChanged (object sender, System.EventArgs e) {if (this.rdodetails.Checked == true) {this.listView1.View = View.Details }}}} Private void rdolist_checkedchanged (object sender, system.eventargs e) {if (this.rdolist.checked == true) {this.listview1.view = view.list;}} Now run the program, click the radio button, The display mode of the ListView can be converted. The following is another handler to the ListView's itemActivate event, and the purpose is when we select the option in ListView, if it is the folder, open this folder, display the information inside, if it is a file, open this file. However, we should pay attention to it, just when we open a new folder, we will use the back function.

转载请注明原文地址:https://www.9cbs.com/read-131132.html

New Post(0)