ListView and multiselect

S

Sebastian

Hello,

how can i make a listview that allows to select more than one item? I am
missing the property multiselect of the listview Control.

thanks in advance

sebastian
 
T

Tim Wilson

You could PInvoke to change the underlying control behavior or you could use
the ListView's Checkboxes property to simulate multiselection.
 
T

Tim Wilson

using System.Runtime.InteropServices;

private const int GWL_STYLE = -16;
private const int LVS_SINGLESEL = 0x0004;

[DllImport("coredll")]
private static extern IntPtr GetCapture();
[DllImport("coredll")]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("coredll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int
dwNewLong);

// To toggle to multiselect for "listView1":
this.listView1.Capture = true;
IntPtr hWnd = GetCapture();
this.listView1.Capture = false;
int style = GetWindowLong(hWnd, GWL_STYLE);
SetWindowLong(hWnd, GWL_STYLE, (style & ~LVS_SINGLESEL));

.... for more Win32 API support you should check out
http://www.opennetcf.org/winapi.asp
 
S

Sebastian

Thnaks for your help, but i cant get it work. i use vb.net and here my code

Private Const GWL_STYLE As Integer = -16

Private Const LVS_SINGELSEL As Integer = 4

Private Declare Function GetCapture Lib "Coredll" () As IntPtr

Private Declare Function GetWindowLong Lib "Coredll" (ByVal hWnd As IntPtr,
ByVal nIndex As Integer) As Integer

Private Declare Function SetWindowLong Lib "Coredll" (ByVal hWnd As IntPtr,
ByVal nIndex As Integer, ByVal dwNewLong As Integer)

Me.lvwFinden.Capture = True

Dim hwnd As IntPtr = GetCapture

Me.lvwFinden.Capture = False

Dim style As Integer = GetWindowLong(hwnd, GWL_STYLE)

SetWindowLong(hwnd, GWL_STYLE, (style And Not LVS_SINGELSEL))


my Problem is that i'm getting a notsupported exception after calling
setwindowlong.


thanks in advance

sebastian

Tim Wilson said:
using System.Runtime.InteropServices;

private const int GWL_STYLE = -16;
private const int LVS_SINGLESEL = 0x0004;

[DllImport("coredll")]
private static extern IntPtr GetCapture();
[DllImport("coredll")]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("coredll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int
dwNewLong);

// To toggle to multiselect for "listView1":
this.listView1.Capture = true;
IntPtr hWnd = GetCapture();
this.listView1.Capture = false;
int style = GetWindowLong(hWnd, GWL_STYLE);
SetWindowLong(hWnd, GWL_STYLE, (style & ~LVS_SINGLESEL));

... for more Win32 API support you should check out
http://www.opennetcf.org/winapi.asp

--
Tim Wilson
.Net Compact Framework MVP
{cf147fdf-893d-4a88-b258-22f68a3dbc6a}

Sebastian said:
How should i invoke the underlying control. i am an beginner of .net cf

could
use
I
 
S

Sebastian

I changed the const vars to
Private Const GWL_STYLE As Integer = (-16)

Private Const LVS_SINGELSEL As Integer = &H4



but this brings a notsupported exception too.

Tim Wilson said:
using System.Runtime.InteropServices;

private const int GWL_STYLE = -16;
private const int LVS_SINGLESEL = 0x0004;

[DllImport("coredll")]
private static extern IntPtr GetCapture();
[DllImport("coredll")]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("coredll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int
dwNewLong);

// To toggle to multiselect for "listView1":
this.listView1.Capture = true;
IntPtr hWnd = GetCapture();
this.listView1.Capture = false;
int style = GetWindowLong(hWnd, GWL_STYLE);
SetWindowLong(hWnd, GWL_STYLE, (style & ~LVS_SINGLESEL));

... for more Win32 API support you should check out
http://www.opennetcf.org/winapi.asp

--
Tim Wilson
.Net Compact Framework MVP
{cf147fdf-893d-4a88-b258-22f68a3dbc6a}

Sebastian said:
How should i invoke the underlying control. i am an beginner of .net cf

could
use
I
 
T

Tim Wilson

The code below works for me:

Private Const GWL_STYLE As Integer = -16
Private Const LVS_SINGLESEL As Integer = &H4

<DllImport("coredll")> _
Private Shared Function GetCapture() As IntPtr
End Function

<DllImport("coredll")> _
Private Shared Function GetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As
Integer) As Integer
End Function

<DllImport("coredll")> _
Private Shared Function SetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As
Integer, ByVal dwNewLong As Integer) As Integer
End Function

Private Sub EnableMultiSelect()
Me.ListView1.Capture = True
Dim hWnd As IntPtr = GetCapture()
Me.ListView1.Capture = False
Dim style As Integer = GetWindowLong(hWnd, GWL_STYLE)
SetWindowLong(hWnd, GWL_STYLE, (style And Not LVS_SINGLESEL))
End Sub

--
Tim Wilson
..Net Compact Framework MVP
{cf147fdf-893d-4a88-b258-22f68a3dbc6a}

Sebastian said:
I changed the const vars to
Private Const GWL_STYLE As Integer = (-16)

Private Const LVS_SINGELSEL As Integer = &H4



but this brings a notsupported exception too.

Tim Wilson said:
using System.Runtime.InteropServices;

private const int GWL_STYLE = -16;
private const int LVS_SINGLESEL = 0x0004;

[DllImport("coredll")]
private static extern IntPtr GetCapture();
[DllImport("coredll")]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("coredll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int
dwNewLong);

// To toggle to multiselect for "listView1":
this.listView1.Capture = true;
IntPtr hWnd = GetCapture();
this.listView1.Capture = false;
int style = GetWindowLong(hWnd, GWL_STYLE);
SetWindowLong(hWnd, GWL_STYLE, (style & ~LVS_SINGLESEL));

... for more Win32 API support you should check out
http://www.opennetcf.org/winapi.asp

--
Tim Wilson
.Net Compact Framework MVP
{cf147fdf-893d-4a88-b258-22f68a3dbc6a}

Sebastian said:
How should i invoke the underlying control. i am an beginner of .net cf

You could PInvoke to change the underlying control behavior or you could
use
the ListView's Checkboxes property to simulate multiselection.

--
Tim Wilson
.Net Compact Framework MVP
{cf147fdf-893d-4a88-b258-22f68a3dbc6a}
Hello,

how can i make a listview that allows to select more than one
item?
 
C

chora

If you can spare the space, why don't you add a check box column and
multi select by checking each row you want.

Then you can go through and get the checked rows
 
J

Jason Bei

Hi Tim,

Would you please tell me how to use your code successfully. I hope to
make a listview multiselectable in pocket PC. I have tried your example
as below, but it doesn't give me the expected result. How to call the
function? I put it in the form's sub new.

Thanks in advance

Jason
 
G

Geoff Schwab [MSFT]

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

Top