Enhancing the functionality of ListView controls in C #

Source: Internet
Author: User
Control is the first custom sort that implements the ListView control, and the Columnclick event that subscribes to the ListView control

private void Listview1_columnclick (object sender, Columnclickeventargs e)
{
if (This.listview1.columns[e.column). Tag = null)
This.listview1.columns[e.column]. Tag = true;
BOOL TABK = (bool) This.listview1.columns[e.column]. Tag;
if (TABK)
This.listview1.columns[e.column]. Tag = false;
Else
This.listview1.columns[e.column]. Tag = true;
This.listView1.ListViewItemSorter = new Listviewsort (E.column, This.listview1.columns[e.column). TAG);
Specifies the sequencer and routes the column index and ascending descending keyword
This.listView1.Sort ()//To list custom sort
}


Definition of Sort class:

///
Custom ListView Control Sort function
///

Class Listviewsort:icomparer
{
private int col;
private bool desck;

Public Listviewsort ()
{
col = 0;
}
public listviewsort (int column, Object Desc)
{
DESCK = (bool) Desc;
col = column; When the front row, 0,1,2 ..., parameters are passed by the Columnclick event of the ListView control
}
public int Compare (object x, Object y)
{
int tempint = String.Compare ((listviewitem) x). Subitems[col]. Text, ((ListViewItem) y). Subitems[col]. Text);
if (desck) Return-tempint;
else return tempint;
}
}
The custom arrangement of the above ListView control, which is sorted when you click the caption of the ListView control

The following implements the removal of the last column of the ListView control, which automatically adjusts the appropriate size

First write a function that adjusts the width of the ListView control column

///
Automatically resize the last column of the ListView control
///
///
///

private void Adjust LV column width ()
{
listview1.columnwidthchanged-= new Columnwidthchangedeventhandler (listview1_columnwidthchanged);
Note. AutoResize (columnheaderautoresizestyle.headersize);
Listview1.columnwidthchanged + = new Columnwidthchangedeventhandler (listview1_columnwidthchanged);
}
The note column above is the name of the last column of the ListView control, and the instance of the ListView control is named ListView1

Then subscribe to the Columnwidthchanged event for the ListView control, that is, automatically adjust column widths when column width changes

///
ListView Column Width Change event function
///
///
///

void Listview1_columnwidthchanged (object sender, ColumnWidthChangedEventArgs e)
{
Adjust LV column width ();
}


Subscribe to the Size_change event for the ListView control, which adjusts the column width when the window size is changed

void Listview1_sizechanged (object sender, EventArgs e)
{
Adjust LV column width ();
}
Finally, adjust the width of the ListView control in the shown event of the form, that is, adjust the column width the first time it is displayed

private void Commodity Management _shown (object sender, EventArgs e)
{
Adjust LV column width ();
}

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.