Click here to Skip to main content
15,891,633 members
Home / Discussions / C#
   

C#

 
GeneralMDI Client Pin
Agent 8613-May-04 10:07
Agent 8613-May-04 10:07 
GeneralRe: MDI Client Pin
Dave Kreskowiak13-May-04 12:01
mveDave Kreskowiak13-May-04 12:01 
GeneralCreating Dataset Pin
MrJJKoolJ13-May-04 10:06
MrJJKoolJ13-May-04 10:06 
GeneralRe: Creating Dataset Pin
Heath Stewart13-May-04 11:07
protectorHeath Stewart13-May-04 11:07 
GeneralRe: Creating Dataset Pin
MrJJKoolJ13-May-04 11:12
MrJJKoolJ13-May-04 11:12 
GeneralRe: Creating Dataset Pin
Heath Stewart13-May-04 11:21
protectorHeath Stewart13-May-04 11:21 
GeneralSort TreeView items by different logic Pin
Gian13-May-04 5:13
Gian13-May-04 5:13 
GeneralRe: Sort TreeView items by different logic Pin
Heath Stewart13-May-04 8:56
protectorHeath Stewart13-May-04 8:56 
In the TreeView class, there is no such property or method for you to provide your own sorting algorithm. The Tree-View common control - which the TreeView class encapsulates - does, however. See the documentation for the TVM_SORTCHILDRENCB message in the Platform SDK.

You can fill TVSORTCB struct (which you must create in C#) with data from the parent tree node (use TreeNode.Handle), a callback function (shouldn't need to be pinned using the GCHandle since the SendMessage call would be synchronous), and any other data you want to pass (like an integer value that specifies whether to sort ascending or descending).

The struct and callback would look like this:
[StructLayout(LayoutKind.Sequential)]
private struct LVSORTCB
{
  public IntPtr TreeItemHandle; // Use TreeNode.Handle
  public TreeViewSortCallback Callback;
  public IntPtr ExtraData;
}
 
public delegate IntPtr TreeViewSortCallback(IntPtr node1, IntPtr node1,
  IntPtr extraData);
The return Type is an IntPtr because an unmanaged int is processor-dependent (32 bits on a 32-bit proc, 64 bits on a 64-bit proc). This makes it more portable.

To call it, you need to P/Invoke SendMessage:
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(
  IntPtr hWnd,
  [MarshalAs(UnmanagedType.U4)] int msg,
  IntPtr wParam,
  ref TVSORTCB sort);
 
public void Sort(TreeNode parent, TreeViewSortCallback callback, bool ascending)
{
  TVSORTCB sort = new TVSORTCB();
  sort.TreeNodeHandle = parent.Handle;
  sort.Callback = callback;
  sort.ExtraData = new IntPtr(ascending ? 0 : 1); // For exmample

  IntPtr hWnd = treeView1.Handle;
  SendMessage(hWnd, 4373, IntPtr.Zero, ref sort);
}
Create a new TreeViewSortCallback delegate that references your function and call such a method. You can use your sorting algorithms in there.

If you want to get really fancy, you could encapsulate this all into a derivative TreeView class, perhaps called SortableTreeView or something, and let the caller pass an IComparer implementation. With a good design, you could couple the interface (common throughout the .NET FCL) with the callback (making the callback private since IComparer is the typically sorting implementation in .NET - consistency is important).

Of course, you could always use your algorithms and manually move nodes around, but then you're responsible for all the work. Using the approach above makes the Tree-View common control do it with very little information.

 

Microsoft MVP, Visual C#
My Articles
GeneralRe: Sort TreeView items by different logic Pin
Gian16-May-04 22:02
Gian16-May-04 22:02 
GeneralRe: Sort TreeView items by different logic Pin
Heath Stewart17-May-04 3:20
protectorHeath Stewart17-May-04 3:20 
GeneralRe: Sort TreeView items by different logic Pin
Gian17-May-04 4:58
Gian17-May-04 4:58 
GeneralRe: Sort TreeView items by different logic Pin
Heath Stewart17-May-04 5:13
protectorHeath Stewart17-May-04 5:13 
GeneralRe: Sort TreeView items by different logic Pin
Gian17-May-04 5:35
Gian17-May-04 5:35 
GeneralRe: Sort TreeView items by different logic Pin
Heath Stewart17-May-04 6:17
protectorHeath Stewart17-May-04 6:17 
GeneralRe: Sort TreeView items by different logic Pin
Gian17-May-04 23:16
Gian17-May-04 23:16 
GeneralRe: Sort TreeView items by different logic Pin
Heath Stewart18-May-04 3:37
protectorHeath Stewart18-May-04 3:37 
GeneralRe: Sort TreeView items by different logic Pin
Gian19-May-04 5:25
Gian19-May-04 5:25 
Generalhelp with winuser.h Pin
HappyPaws13-May-04 4:28
HappyPaws13-May-04 4:28 
GeneralRe: help with winuser.h Pin
Judah Gabriel Himango13-May-04 6:36
sponsorJudah Gabriel Himango13-May-04 6:36 
GeneralRe: help with winuser.h Pin
Heath Stewart13-May-04 8:37
protectorHeath Stewart13-May-04 8:37 
GeneralRe: help with winuser.h Pin
Heath Stewart13-May-04 8:37
protectorHeath Stewart13-May-04 8:37 
GeneralRe: help with winuser.h Pin
HappyPaws14-May-04 1:37
HappyPaws14-May-04 1:37 
Generalregd DataGrids Pin
karteek13-May-04 4:06
karteek13-May-04 4:06 
Generalregd dialogs Pin
karteek13-May-04 3:41
karteek13-May-04 3:41 
GeneralRe: regd dialogs Pin
Heath Stewart13-May-04 3:47
protectorHeath Stewart13-May-04 3:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.