The native ListView (i.e. the Win32 one) supports sub item images so it
should be pretty simple to add that support by inheriting ListView and doing
some P/Invoke stuff. Check out the LVS_EX_SUBITEMI MAGES style. Once you have
the images in place you just need to track the check state. The following
class has support for sub item images:
using System;
using System.Runtime. InteropServices ;
public class ListViewEx : System.Windows. Forms.ListView
{
private const int LVIF_IMAGE = 0x2;
private const int LVS_EX_SUBITEMI MAGES = 0x2;
private const int LVM_FIRST = 0x1000;
private const int LVM_GETITEMA = LVM_FIRST + 5;
private const int LVM_GETITEMW = LVM_FIRST + 75;
private static readonly int LVM_GETITEM = (Marshal.System DefaultCharSize
== 1) ? LVM_GETITEMA : LVM_GETITEMW;
private const int LVM_SETITEMA = LVM_FIRST + 6;
private const int LVM_SETITEMW = LVM_FIRST + 76;
private static readonly int LVM_SETITEM = (Marshal.System DefaultCharSize
== 1) ? LVM_SETITEMA : LVM_SETITEMW;
private const int LVM_SETEXTENDED LISTVIEWSTYLE = LVM_FIRST + 54;
[DllImport("User 32.dll", CharSet = CharSet.Auto)]
private static extern int SendMessage(Int Ptr hwnd, int msg, IntPtr
wParam, ref LVITEM lParam);
[DllImport("User 32.dll", CharSet=CharSet .Auto)]
private static extern int SendMessage(Int Ptr hwnd, int msg, IntPtr
wParam, IntPtr lParam);
[StructLayout(La youtKind.Sequen tial, CharSet=CharSet .Auto)]
private struct LVITEM
{
public int mask;
public int iItem;
public int subItem;
public int state;
public int stateMask ;
[MarshalAs(Unman agedType.LPTStr )]
public string lpszText;
public int cchTextMax;
public int iImage;
public IntPtr lParam;
public int iIndent;
}
private bool ListView_SetIte m(IntPtr hwnd, ref LVITEM lvi)
{
int result = SendMessage(hwn d, LVM_SETITEM, IntPtr.Zero, ref lvi);
return (result == 0) ? false : true;
}
private bool ListView_GetIte m(IntPtr hwnd, ref LVITEM lvi)
{
int result = SendMessage(hwn d, LVM_GETITEM, IntPtr.Zero, ref lvi);
return (result == 0) ? false : true;
}
private void ListView_SetExt endedListViewSt yleEx(IntPtr hwnd, int mask,
int style)
{
SendMessage(hwn d, LVM_SETEXTENDED LISTVIEWSTYLE, new IntPtr(mask),
new IntPtr(style));
}
private void ListView_SetSub ItemImageIndex( IntPtr hwnd, int index, int
subItemIndex, int imageIndex)
{
LVITEM lvi = new LVITEM();
lvi.iItem = index;
lvi.subItem = subItemIndex;
lvi.iImage = imageIndex;
lvi.mask = LVIF_IMAGE;
ListView_SetIte m(hwnd, ref lvi);
}
private int ListView_GetSub ItemImageIndex( IntPtr hwnd, int index, int
subItemIndex)
{
LVITEM lvi = new LVITEM();
lvi.iItem = index;
lvi.subItem = subItemIndex;
lvi.mask = LVIF_IMAGE;
if (ListView_GetIt em(hwnd, ref lvi))
return lvi.iImage;
else
return -1;
}
protected override void OnCreateControl ()
{
base.OnCreateCo ntrol();
ListView_SetExt endedListViewSt yleEx(this.Hand le,
LVS_EX_SUBITEMI MAGES, LVS_EX_SUBITEMI MAGES);
}
public void SetSubItemImage Index(int index, int subItemIndex, int
imageIndex)
{
ListView_SetSub ItemImageIndex( this.Handle, index, subItemIndex,
imageIndex);
}
public int GetSubItemImage Index(int index, int subItemIndex)
{
return ListView_GetSub ItemImageIndex( this.Handle, index,
subItemIndex);
}
}
/claes
"Steve" <sk**@skle.comw rote in message
news:%2******** *******@TK2MSFT NGP05.phx.gbl.. .
Hi all,
I don't want to use the datagrid if I don't have to. Is there a way to
setup a ListBox to have more than one checkbox column?
I need something like this
| Include || Set as Default || Other columns... |
[X] [ ] asdasdasd
[X] [X] digity-digity!
Quick scan on CodeProject came up with a not-so-pretty MFC control, but
obviously... I would like a c# one.
Thanks for any tips!