how can i get an icon or image placed on the column header after sorted. I got the sort routine... i just can't seem to figure out how to place an icon on the column header.
Printable View
how can i get an icon or image placed on the column header after sorted. I got the sort routine... i just can't seem to figure out how to place an icon on the column header.
ok i'm using this project as a reference http://www.codeproject.com/cs/miscctrl/customheader.asp
but i'm still having problems... when i click on the column header the text completely disappears and still no icon
here's my code... also yes i know it's in C# but i can convert between the two
This is the library that holds the functionality
This is in the form.PHP Code:/*
* Created by Derek J. Klingman
* User: derekklingman
* Date: 9/13/2005
* Time: 10:27 AM
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Collections;
using WebHostRemote;
using WebHostRemote.Configuration;
using WebHostRemote.WHRExceptionHandler;
namespace WebHostRemote
{
namespace WHRStandardProvider
{
/// <summary>
/// Description of StandardTemplate.
/// </summary>
public class StandardTemplate : System.IDisposable
{
public StandardTemplate()
{
//
}
public void Dispose()
{
//
}
#region ColumnHeader Creation
public void LoadListViewColumns(System.Windows.Forms.ListView lv,
string section){
foreach(WebHostRemote.Configuration.ConfigureSection sec in
Configure.GetConfigSection(section))
{
System.Windows.Forms.ColumnHeader ch = new
System.Windows.Forms.ColumnHeader();
ch.Text = sec.Key.Replace("_", " ");
//Determine if the column header is visiable
if(Convert.ToBoolean(Convert.ToInt32(sec.Value))) /*Visiable*/{
ch.Width = -2; /*Causes Autosize ColumnHeader*/
}
else{
ch.Width = 0;
}
lv.Columns.Add(ch);
}
}
#endregion
#region Place Sort Icon In ColumnHeader
private const int LVM_FIRST = 0x1000;
private const int LVM_GETHEADER = (LVM_FIRST + 31);
private const int HDI_BITMAP = 0x0010;
private const int HDI_IMAGE = 0x0020;
private const int HDI_FORMAT = 0x0004;
private const int HDI_TEXT = 0x0002;
private const int HDF_BITMAP_ON_RIGHT = 0x1000;
private const int HDF_BITMAP = 0x2000;
private const int HDF_IMAGE = 0x0800;
private const int HDF_STRING = 0x4000;
private const int HDM_FIRST = 0x1200;
private const int HDM_SETITEM = (HDM_FIRST + 12);
private const int HDM_SETIMAGELIST = (HDM_FIRST + 8);
private const int HDM_GETIMAGELIST = (HDM_FIRST + 9);
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]
public struct HD_ITEM
{
public int mask;
public int cxy;
public string pszText;
public IntPtr hbm;
public int cchTextMax;
public int fmt;
public int lParam;
public int iImage;
public int iOrder;
public uint type;
public IntPtr pvFilter;
}
[DllImport("user32.dll", CharSet=CharSet.Auto)]
protected static extern int SendMessage(IntPtr hWnd, int msg,
int wParam, ref HD_ITEM HD);
[DllImport("user32.dll", CharSet=CharSet.Auto)]
protected static extern IntPtr SendMessage(IntPtr hWnd, int msg,
int wParam, int lParam);
public void ShowHeaderIcon(System.Windows.Forms.ListView lv, int colNo,
int imgIconNo, bool showImage)
{
IntPtr hHeader;
HD_ITEM HD = new HD_ITEM();
//Get a handle to the listview header component
hHeader = SendMessage(lv.Handle, LVM_GETHEADER, 0, 0);
//Set up the required structure members
HD.mask = HDI_IMAGE | HDI_FORMAT;
HD.pszText = lv.Columns[colNo].Text;
if(showImage){
HD.fmt = HDF_STRING | HDF_IMAGE | HDF_BITMAP_ON_RIGHT;
HD.iImage = imgIconNo;
}
else{
HD.fmt = HDF_STRING;
}
try{
//Modify the header
SendMessage(hHeader, HDM_SETITEM, colNo, ref HD);
}
catch(Exception ex){
WebHostRemoteExceptionHandler _err = new WebHostRemoteExceptionHandler();
_err.OnUnHandledException(ex);
_err.Dispose();
_err = null;
}
}
#endregion
}
/// <summary>
/// This class is an implementation of the 'IComparer' interface.
/// </summary>
public class ListViewColumnSorter : IComparer
{
/// <summary>
/// Specifies the column to be sorted
/// </summary>
private int ColumnToSort;
/// <summary>
/// Specifies the order in which to sort (i.e. 'Ascending').
/// </summary>
private SortOrder OrderOfSort;
/// <summary>
/// Case insensitive comparer object
/// </summary>
private CaseInsensitiveComparer ObjectCompare;
/// <summary>
/// Class constructor. Initializes various elements
/// </summary>
public ListViewColumnSorter()
{
// Initialize the column to '0'
ColumnToSort = 0;
// Initialize the sort order to 'none'
OrderOfSort = SortOrder.None;
// Initialize the CaseInsensitiveComparer object
ObjectCompare = new CaseInsensitiveComparer();
}
/// <summary>
/// This method is inherited from the IComparer interface. It compares the two objects passed using a case insensitive comparison.
/// </summary>
/// <param name="x">First object to be compared</param>
/// <param name="y">Second object to be compared</param>
/// <returns>The result of the comparison. "0" if equal, negative if 'x' is less than 'y' and positive if 'x' is greater than 'y'</returns>
public int Compare(object x, object y)
{
int compareResult;
ListViewItem listviewX, listviewY;
// Cast the objects to be compared to ListViewItem objects
listviewX = (ListViewItem)x;
listviewY = (ListViewItem)y;
// Compare the two items
compareResult = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text,listviewY.SubItems[ColumnToSort].Text);
// Calculate correct return value based on object comparison
if (OrderOfSort == SortOrder.Ascending)
{
// Ascending sort is selected, return normal result of compare operation
return compareResult;
}
else if (OrderOfSort == SortOrder.Descending)
{
// Descending sort is selected, return negative result of compare operation
return (-compareResult);
}
else
{
// Return '0' to indicate they are equal
return 0;
}
}
/// <summary>
/// Gets or sets the number of the column to which to apply the sorting operation (Defaults to '0').
/// </summary>
public int SortColumn
{
set
{
ColumnToSort = value;
}
get
{
return ColumnToSort;
}
}
/// <summary>
/// Gets or sets the order of sorting to apply (for example, 'Ascending' or 'Descending').
/// </summary>
public SortOrder Order
{
set
{
OrderOfSort = value;
}
get
{
return OrderOfSort;
}
}
}
}
}
PHP Code://this is in the Window Form Generated code
this.lvInvNew.ListViewItemSorter = this.lvwColumnSorter;
/******************************************/
private WebHostRemote.WHRStandardProvider.ListViewColumnSorter lvwColumnSorter = new ListViewColumnSorter();
void LvInvNewColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
{
// Determine if clicked column is already the column that is being sorted.
if ( e.Column == lvwColumnSorter.SortColumn )
{
// Reverse the current sort direction for this column.
if (lvwColumnSorter.Order == SortOrder.Ascending)
{
lvwColumnSorter.Order = SortOrder.Descending;
}
else
{
lvwColumnSorter.Order = SortOrder.Ascending;
}
}
else
{
// Set the column number that is to be sorted; default to ascending.
lvwColumnSorter.SortColumn = e.Column;
lvwColumnSorter.Order = SortOrder.Ascending;
}
// Perform the sort with these new sort options.
this.lvInvNew.Sort();
StandardTemplate std = new StandardTemplate();
std.ShowHeaderIcon(this.lvInvNew, e.Column, (int)lvwColumnSorter.Order, true);
}