There never enough controls... I need a multi column, multi checkbox listview

  • Thread starter Thread starter Steve
  • Start date Start date
S

Steve

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!
 
I would have thought datagrid ideal for this since you can customise
the type and the control for each column.

But otherwise I guess you could write your own control to do it as
well.
 
You could write your own Control to do this in a couple of hours. Start by
inheriting UserControl.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

Big thicks are made up of lots of little thins.
 
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_SUBITEMIMAGES 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_SUBITEMIMAGES = 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.SystemDefaultCharSize
== 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.SystemDefaultCharSize
== 1) ? LVM_SETITEMA : LVM_SETITEMW;
private const int LVM_SETEXTENDEDLISTVIEWSTYLE = LVM_FIRST + 54;

[DllImport("User32.dll", CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hwnd, int msg, IntPtr
wParam, ref LVITEM lParam);
[DllImport("User32.dll", CharSet=CharSet.Auto)]
private static extern int SendMessage(IntPtr hwnd, int msg, IntPtr
wParam, IntPtr lParam);

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
private struct LVITEM
{
public int mask;
public int iItem;
public int subItem;
public int state;
public int stateMask ;
[MarshalAs(UnmanagedType.LPTStr)]
public string lpszText;
public int cchTextMax;
public int iImage;
public IntPtr lParam;
public int iIndent;
}

private bool ListView_SetItem(IntPtr hwnd, ref LVITEM lvi)
{
int result = SendMessage(hwnd, LVM_SETITEM, IntPtr.Zero, ref lvi);
return (result == 0) ? false : true;
}

private bool ListView_GetItem(IntPtr hwnd, ref LVITEM lvi)
{
int result = SendMessage(hwnd, LVM_GETITEM, IntPtr.Zero, ref lvi);
return (result == 0) ? false : true;
}

private void ListView_SetExtendedListViewStyleEx(IntPtr hwnd, int mask,
int style)
{
SendMessage(hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE, new IntPtr(mask),
new IntPtr(style));
}

private void ListView_SetSubItemImageIndex(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_SetItem(hwnd, ref lvi);
}

private int ListView_GetSubItemImageIndex(IntPtr hwnd, int index, int
subItemIndex)
{
LVITEM lvi = new LVITEM();
lvi.iItem = index;
lvi.subItem = subItemIndex;
lvi.mask = LVIF_IMAGE;
if (ListView_GetItem(hwnd, ref lvi))
return lvi.iImage;
else
return -1;
}

protected override void OnCreateControl()
{
base.OnCreateControl();
ListView_SetExtendedListViewStyleEx(this.Handle,
LVS_EX_SUBITEMIMAGES, LVS_EX_SUBITEMIMAGES);
}

public void SetSubItemImageIndex(int index, int subItemIndex, int
imageIndex)
{
ListView_SetSubItemImageIndex(this.Handle, index, subItemIndex,
imageIndex);
}

public int GetSubItemImageIndex(int index, int subItemIndex)
{
return ListView_GetSubItemImageIndex(this.Handle, index,
subItemIndex);
}
}

/claes

Steve said:
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!
 
Hi Steve,

If licensing money isn't an problem, consider going for a 3rd party
grid component. In our company we make extensive use of the VSFlexGrid
control by ComponentOne. It is fast, easy to program to and feature
rich and will handle your requirements quite easily.

Regards,
Pieter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top