473,387 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help with TreeView custom sort callback marshalling?

I need to do a custom sort on a TreeView. I have various object types
associated with the TreeNode Tag property. I want to sort objects of
the same type at the top of the list, other objects at the bottom. I
found some scraps of code in various postings that I am trying to get to
work.

I can not seem to get any meaningful data back from the CompareFunc
callback. According to MSDN, The lParam1 and lParam2 parameters
correspond to the lParam member of the TVITEM structure for the two
items being compared. Does this correspond to the TreeNode Text or Tag
property?

Anyway, I am not certain what data types these are but I tried using
Marshal.PtrToStringAuto which returned null strings.

Thanks in advance for any tips or direction,

-Ed

using System.Runtime.InteropServices;

namespace TreeViewSort
{
public class TreeViewEx : TreeView
{
public const int TV_FIRST = 0x1100;
public const int TVM_SORTCHILDRENCB = TV_FIRST + 21;

public delegate int CompareFuncDelegate (IntPtr lParam1,
IntPtr lParam2,
IntPtr lParamSort);

[StructLayout(LayoutKind.Sequential)]
public struct TVSORTCB
{
public IntPtr hParent;
[MarshalAs(UnmanagedType.FunctionPtr)]
public CompareFuncDelegate lpfnCompare;
public IntPtr lParam ;
}

public TreeViewEx()
{
}

private int CompareFunc(IntPtr lParam1,
IntPtr lParam2,
IntPtr lParamSort)
{
// How do I convert lParams into something I can compare?
// Marshal.PtrToStringAuto returns null strings
string str1 = Marshal.PtrToStringAuto(lParam1);
string str2 = Marshal.PtrToStringAuto(lParam2);
return 0;
}

public void Sort()
{
// Build and send the sort message
TVSORTCB sort;
sort.hParent = base.Nodes[0].Handle;
sort.lpfnCompare = new CompareFuncDelegate(this.CompareFunc);
sort.lParam = IntPtr.Zero;

IntPtr ptrTVSORTCB = Marshal.AllocHGlobal(Marshal.SizeOf(sort));
Marshal.StructureToPtr(sort,ptrTVSORTCB,false) ;

Message msg = Message.Create(base.Handle,
TVM_SORTCHILDRENCB,
IntPtr.Zero,
ptrTVSORTCB);
base.WndProc(ref msg);

Marshal.FreeHGlobal(ptrTVSORTCB);
this.Refresh();
}
}
}

Nov 16 '05 #1
3 3033
Ed,
Does this correspond to the TreeNode Text or Tag property?


No I don't think it does. As far as I can tell, the winforms TreeView
control doesn't set the lParam value at all. Instead, it uses an
internal hashtable indexed by node handle to lookup the corresponding
TreeNode object.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
Mattias,

Thank you very much for your reply.
Does this correspond to the TreeNode Text or Tag property?

No I don't think it does. As far as I can tell, the winforms TreeView
control doesn't set the lParam value at all. Instead, it uses an
internal hashtable indexed by node handle to lookup the corresponding
TreeNode object.


It sounds like I am at a dead end and my approach to creating a custom
sortable TreeView can not work.

Does anyone have any alternative approaches for creating a custom
sortable TreeView?

Thanks again,

-Ed
Nov 16 '05 #3
Mayukh
1
declare a struct which will hold the tree node item.
public struct TVITEM
{
int mask;
public IntPtr hItem;
int state;
int stateMask;
IntPtr pszText;
IntPtr cchTextMax;
int iImage;
int iSelectedImage;
int cChildren;
int lParam;
}


inside your compare function you can use
TVITEM str1 = (TVITEM)Marshal.PtrToStructure(lParam1, typeof(TVITEM));
TVITEM str2 = (TVITEM)Marshal.PtrToStructure(lParam2, typeof(TVITEM));

Finally you can recover the treeview using
TreeView Tv = (TreeView) TreeView.FromHandle(yourtreeviewinstance.Handle)
Re-cover the treenode using TreeNode Tn = TreeNode.FromHandle(str1.hItem);

now you have the Tn.Text which is a string for comparision.
Jul 17 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: serge calderara | last post by:
Dear all, I have a function that I need to run in a thread due to the fact that it can takes long time to execute according to the amount of data to collect. This function is also populating a...
22
by: James Cane | last post by:
Here's an interesting problem that someone might have an answer to... Some time ago, I wrote a set of utility classes which wrap up the custom row source function needed to add arbitrary items to...
42
by: lauren quantrell | last post by:
So many postings on not to use the treeview control, but nothing recently. Is it safe to swim there yet with Access 2000-Access 2003?
2
by: Aaron Corcoran | last post by:
I posted this under the webcontrols area as well, however, I didn't receive any feedback, so please pardon my redundant post. Dear Group, My coworker has been working on a project that uses...
2
by: kevin | last post by:
I would like to remember the state of the nodes after the treeview gets disposed, but not necessarily after the app terminates so I don't need a disk file. I was thinking about using the tag...
8
by: kurtcobain1978 | last post by:
-------------------------------------------------------------------------------- I need to do the exactly same thing in VB.NET. Load a unmanaged C DLL dynamically and then call a function in...
2
by: casmang | last post by:
I am having the problem where when I change the font of a TreeNode in a TreeView to a bold property, the bounds or rectangle that draws the node does not expand so that the text ends up being cut...
7
by: Jonas | last post by:
Hi. I'm trying to develop a web custom control that uses a programmatically created treeview. My problem is that I get an exception when I try to render the control. With properties do I have to...
1
by: doemon | last post by:
Hi, I'm working on a pagination control and I need to dynamically rerender a treeview to display the next set of nodes depending on which page we're on. For example, page 1 will dispaly only...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.