NetCF: How to remove checkboxes from some of the nodes of a TreeView control at run time ...

R

Rodrigus Makon

Hello,

I have a piece of code that removes the check boxes from a treeview control
at run time for the .NET 2.0 of Desktop. I have changed the dll reference
from user32.dll to coredll.dll and compiled it and trying to use it in my
CF 2.0 application, but it does not work (does not remove the check boxes
for the nodes that I am trying to remove, does not give any error either).
I am not familier with interop services, so can some one with knowledge in
that area, please help me with this code.

Here is the code that works for the Desktop .NET 2.0.

using System;
using System.Runtime.InteropServices;

namespace tempnamespace
{
/// <summary>
/// Summary description for TreeViewExtender.
/// </summary>
public class TreeViewExtender
{
private const int TVIS_STATEIMAGEMASK = 0xF000;
private const int TVIF_TEXT = 0x0001,
TVIF_IMAGE = 0x0002,
TVIF_STATE = 0x0008,
TVIF_HANDLE = 0x0010,
TVIF_SELECTEDIMAGE = 0x0020,
TVM_SETITEM = 0x110D;


[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private class TVITEM
{
public int mask;
public System.IntPtr hItem; // HTREEITEM
public int state;
public int stateMask;
public System.IntPtr pszText; // string
public int cchTextMax;
public int iImage;
public int iSelectedImage;
public int cChildren;
public System.IntPtr lParam;
}

static public void RemoveCheckBoxFromNode(System.Windows.Forms.TreeNode
node)
{
//
// It is important that the TreeView.Handle is accessed before
// the TreeNode Handle. If it is not, the results of TreeNode.Handle

// are non-deterministic.
//

System.IntPtr treeViewHandle = node.TreeView.Handle;

TVITEM tvi = new TVITEM();
tvi.hItem = node.Handle;
tvi.mask = TVIF_HANDLE | TVIF_STATE;
tvi.stateMask = TVIS_STATEIMAGEMASK;
tvi.state = 0;

User32.SendMessage(treeViewHandle, TVM_SETITEM, System.IntPtr.Zero,
tvi);
}

public class User32
{
[DllImport("coredll.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
internal static extern System.IntPtr SendMessage(IntPtr hWnd, int
msg, IntPtr wParam, [In, Out, MarshalAs(UnmanagedType.AsAny)] Object lParam);
}
}


I have already changed the dllimport to coredll.dll above.

Here is a sample code that uses the above code.

// TreeView tv = new TreeView.....
// assigned imagelist ...

TreeNode tn = tv.Nodes.Add("First");

TreeNode prefix = tn.Nodes.Add("Id Prefix - " );
prefix.SelectedImageIndex = 1;
prefix.ImageIndex = 1;
TreeViewExtender.RemoveCheckBoxFromNode(prefix);


Any help please.
Regards
 
G

Guest

Not sure if that's a supported option in CE. See what GetLastWin32Error is
after the SendMessage call.

-Chris
 
R

Rodrigus Makon

Hello Chris,

Thank you for your reply. I really appreciate it.

I have added the below line to my code right after the send message.

int rc = System.Runtime.InteropServices.Marshal.GetLastWin32Error();

The value of rc : -2147483643


Regards

Not sure if that's a supported option in CE. See what
GetLastWin32Error is after the SendMessage call.

-Chris

Hello,

I have a piece of code that removes the check boxes from a treeview
control at run time for the .NET 2.0 of Desktop. I have changed the
dll reference from user32.dll to coredll.dll and compiled it and
trying to use it in my CF 2.0 application, but it does not work (does
not remove the check boxes for the nodes that I am trying to remove,
does not give any error either). I am not familier with interop
services, so can some one with knowledge in that area, please help me
with this code.

Here is the code that works for the Desktop .NET 2.0.

using System;
using System.Runtime.InteropServices;
namespace tempnamespace
{
/// <summary>
/// Summary description for TreeViewExtender.
/// </summary>
public class TreeViewExtender
{
private const int TVIS_STATEIMAGEMASK = 0xF000;
private const int TVIF_TEXT = 0x0001,
TVIF_IMAGE = 0x0002,
TVIF_STATE = 0x0008,
TVIF_HANDLE = 0x0010,
TVIF_SELECTEDIMAGE = 0x0020,
TVM_SETITEM = 0x110D;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private class TVITEM
{
public int mask;
public System.IntPtr hItem; // HTREEITEM public int state;
public int stateMask;
public System.IntPtr pszText; // string public int
cchTextMax;
public int iImage;
public int iSelectedImage;
public int cChildren;
public System.IntPtr lParam;
}
static public void
RemoveCheckBoxFromNode(System.Windows.Forms.TreeNode node)
{
// // It is important that the TreeView.Handle is accessed
before // the TreeNode Handle. If it is not, the results of
TreeNode.Handle
// are non-deterministic. //
System.IntPtr treeViewHandle = node.TreeView.Handle;
TVITEM tvi = new TVITEM();
tvi.hItem = node.Handle;
tvi.mask = TVIF_HANDLE | TVIF_STATE;
tvi.stateMask = TVIS_STATEIMAGEMASK;
tvi.state = 0;
User32.SendMessage(treeViewHandle, TVM_SETITEM,
System.IntPtr.Zero, tvi);
}
public class User32
{
[DllImport("coredll.dll", CharSet =
System.Runtime.InteropServices.CharSet.Auto)]
internal static extern System.IntPtr SendMessage(IntPtr hWnd, int
msg, IntPtr wParam, [In, Out, MarshalAs(UnmanagedType.AsAny)] Object
lParam);
}
}
I have already changed the dllimport to coredll.dll above.

Here is a sample code that uses the above code.

// TreeView tv = new TreeView.....
// assigned imagelist ...
TreeNode tn = tv.Nodes.Add("First");

TreeNode prefix = tn.Nodes.Add("Id Prefix - " );
prefix.SelectedImageIndex = 1;
prefix.ImageIndex = 1;
TreeViewExtender.RemoveCheckBoxFromNode(prefix);
Any help please. Regards.
 
R

Rodrigus Makon

Hello Chris,

Thank you for your reply. I really appreciate it.

I have added the below line to my code right after the send message.

int rc = System.Runtime.InteropServices.Marshal.GetLastWin32Error();

The value of rc : -2147483643


Regards

Not sure if that's a supported option in CE. See what
GetLastWin32Error is after the SendMessage call.

-Chris

Hello,

I have a piece of code that removes the check boxes from a treeview
control at run time for the .NET 2.0 of Desktop. I have changed the
dll reference from user32.dll to coredll.dll and compiled it and
trying to use it in my CF 2.0 application, but it does not work (does
not remove the check boxes for the nodes that I am trying to remove,
does not give any error either). I am not familier with interop
services, so can some one with knowledge in that area, please help me
with this code.

Here is the code that works for the Desktop .NET 2.0.

using System;
using System.Runtime.InteropServices;
namespace tempnamespace
{
/// <summary>
/// Summary description for TreeViewExtender.
/// </summary>
public class TreeViewExtender
{
private const int TVIS_STATEIMAGEMASK = 0xF000;
private const int TVIF_TEXT = 0x0001,
TVIF_IMAGE = 0x0002,
TVIF_STATE = 0x0008,
TVIF_HANDLE = 0x0010,
TVIF_SELECTEDIMAGE = 0x0020,
TVM_SETITEM = 0x110D;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private class TVITEM
{
public int mask;
public System.IntPtr hItem; // HTREEITEM public int state;
public int stateMask;
public System.IntPtr pszText; // string public int
cchTextMax;
public int iImage;
public int iSelectedImage;
public int cChildren;
public System.IntPtr lParam;
}
static public void
RemoveCheckBoxFromNode(System.Windows.Forms.TreeNode node)
{
// // It is important that the TreeView.Handle is accessed
before // the TreeNode Handle. If it is not, the results of
TreeNode.Handle
// are non-deterministic. //
System.IntPtr treeViewHandle = node.TreeView.Handle;
TVITEM tvi = new TVITEM();
tvi.hItem = node.Handle;
tvi.mask = TVIF_HANDLE | TVIF_STATE;
tvi.stateMask = TVIS_STATEIMAGEMASK;
tvi.state = 0;
User32.SendMessage(treeViewHandle, TVM_SETITEM,
System.IntPtr.Zero, tvi);
}
public class User32
{
[DllImport("coredll.dll", CharSet =
System.Runtime.InteropServices.CharSet.Auto)]
internal static extern System.IntPtr SendMessage(IntPtr hWnd, int
msg, IntPtr wParam, [In, Out, MarshalAs(UnmanagedType.AsAny)] Object
lParam);
}
}
I have already changed the dllimport to coredll.dll above.

Here is a sample code that uses the above code.

// TreeView tv = new TreeView.....
// assigned imagelist ...
TreeNode tn = tv.Nodes.Add("First");

TreeNode prefix = tn.Nodes.Add("Id Prefix - " );
prefix.SelectedImageIndex = 1;
prefix.ImageIndex = 1;
TreeViewExtender.RemoveCheckBoxFromNode(prefix);
Any help please. Regards.
 
G

Guest

-2147483643 == 0x80000005 == Invalid Pointer

Your handle is probably bad.

-Chris




Rodrigus Makon said:
Hello Chris,

Thank you for your reply. I really appreciate it.

I have added the below line to my code right after the send message.
int rc = System.Runtime.InteropServices.Marshal.GetLastWin32Error();

The value of rc : -2147483643


Regards

Not sure if that's a supported option in CE. See what
GetLastWin32Error is after the SendMessage call.

-Chris

Hello,

I have a piece of code that removes the check boxes from a treeview
control at run time for the .NET 2.0 of Desktop. I have changed the
dll reference from user32.dll to coredll.dll and compiled it and
trying to use it in my CF 2.0 application, but it does not work (does
not remove the check boxes for the nodes that I am trying to remove,
does not give any error either). I am not familier with interop
services, so can some one with knowledge in that area, please help me
with this code.

Here is the code that works for the Desktop .NET 2.0.

using System;
using System.Runtime.InteropServices;
namespace tempnamespace
{
/// <summary>
/// Summary description for TreeViewExtender.
/// </summary>
public class TreeViewExtender
{
private const int TVIS_STATEIMAGEMASK = 0xF000;
private const int TVIF_TEXT = 0x0001,
TVIF_IMAGE = 0x0002,
TVIF_STATE = 0x0008,
TVIF_HANDLE = 0x0010,
TVIF_SELECTEDIMAGE = 0x0020,
TVM_SETITEM = 0x110D;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private class TVITEM
{
public int mask;
public System.IntPtr hItem; // HTREEITEM public int state;
public int stateMask;
public System.IntPtr pszText; // string public int
cchTextMax;
public int iImage;
public int iSelectedImage;
public int cChildren;
public System.IntPtr lParam;
}
static public void
RemoveCheckBoxFromNode(System.Windows.Forms.TreeNode node)
{
// // It is important that the TreeView.Handle is accessed
before // the TreeNode Handle. If it is not, the results of
TreeNode.Handle
// are non-deterministic. //
System.IntPtr treeViewHandle = node.TreeView.Handle;
TVITEM tvi = new TVITEM();
tvi.hItem = node.Handle;
tvi.mask = TVIF_HANDLE | TVIF_STATE;
tvi.stateMask = TVIS_STATEIMAGEMASK;
tvi.state = 0;
User32.SendMessage(treeViewHandle, TVM_SETITEM,
System.IntPtr.Zero, tvi);
}
public class User32
{
[DllImport("coredll.dll", CharSet =
System.Runtime.InteropServices.CharSet.Auto)]
internal static extern System.IntPtr SendMessage(IntPtr hWnd, int
msg, IntPtr wParam, [In, Out, MarshalAs(UnmanagedType.AsAny)] Object
lParam);
}
}
I have already changed the dllimport to coredll.dll above.

Here is a sample code that uses the above code.

// TreeView tv = new TreeView.....
// assigned imagelist ...
TreeNode tn = tv.Nodes.Add("First");

TreeNode prefix = tn.Nodes.Add("Id Prefix - " );
prefix.SelectedImageIndex = 1;
prefix.ImageIndex = 1;
TreeViewExtender.RemoveCheckBoxFromNode(prefix);
Any help please. Regards.
 
R

Rodrigus Makon

Hello Chris,

I have captured the node.TreeView.Handle and node.Handle and both have non-zero
values for all the nodes that I checked.

node.TreeView.Handle node.Handle
2080903408 366192
2080903408 265104
2080903408 2605824

The Handles are having good values I believe.

Regards
-2147483643 == 0x80000005 == Invalid Pointer

Your handle is probably bad.

-Chris

Hello Chris,

Thank you for your reply. I really appreciate it.

I have added the below line to my code right after the send message.
int rc = System.Runtime.InteropServices.Marshal.GetLastWin32Error();

The value of rc : -2147483643

Regards
Not sure if that's a supported option in CE. See what
GetLastWin32Error is after the SendMessage call.

-Chris


Hello,

I have a piece of code that removes the check boxes from a treeview
control at run time for the .NET 2.0 of Desktop. I have changed the
dll reference from user32.dll to coredll.dll and compiled it and
trying to use it in my CF 2.0 application, but it does not work
(does not remove the check boxes for the nodes that I am trying to
remove, does not give any error either). I am not familier with
interop services, so can some one with knowledge in that area,
please help me with this code.

Here is the code that works for the Desktop .NET 2.0.

using System;
using System.Runtime.InteropServices;
namespace tempnamespace
{
/// <summary>
/// Summary description for TreeViewExtender.
/// </summary>
public class TreeViewExtender
{
private const int TVIS_STATEIMAGEMASK = 0xF000;
private const int TVIF_TEXT = 0x0001,
TVIF_IMAGE = 0x0002,
TVIF_STATE = 0x0008,
TVIF_HANDLE = 0x0010,
TVIF_SELECTEDIMAGE = 0x0020,
TVM_SETITEM = 0x110D;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private class TVITEM
{
public int mask;
public System.IntPtr hItem; // HTREEITEM public int state;
public int stateMask;
public System.IntPtr pszText; // string public int
cchTextMax;
public int iImage;
public int iSelectedImage;
public int cChildren;
public System.IntPtr lParam;
}
static public void
RemoveCheckBoxFromNode(System.Windows.Forms.TreeNode node)
{
// // It is important that the TreeView.Handle is accessed
before // the TreeNode Handle. If it is not, the results of
TreeNode.Handle
// are non-deterministic. //
System.IntPtr treeViewHandle = node.TreeView.Handle;
TVITEM tvi = new TVITEM();
tvi.hItem = node.Handle;
tvi.mask = TVIF_HANDLE | TVIF_STATE;
tvi.stateMask = TVIS_STATEIMAGEMASK;
tvi.state = 0;
User32.SendMessage(treeViewHandle, TVM_SETITEM,
System.IntPtr.Zero, tvi);
}
public class User32
{
[DllImport("coredll.dll", CharSet =
System.Runtime.InteropServices.CharSet.Auto)]
internal static extern System.IntPtr SendMessage(IntPtr hWnd, int
msg, IntPtr wParam, [In, Out, MarshalAs(UnmanagedType.AsAny)]
Object
lParam);
}
}
I have already changed the dllimport to coredll.dll above.
Here is a sample code that uses the above code.

// TreeView tv = new TreeView.....
// assigned imagelist ...
TreeNode tn = tv.Nodes.Add("First");
TreeNode prefix = tn.Nodes.Add("Id Prefix - " );
prefix.SelectedImageIndex = 1;
prefix.ImageIndex = 1;
TreeViewExtender.RemoveCheckBoxFromNode(prefix);
Any help please. Regards.
 

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