2010-07-29 7 views
0

많은 노드가있는 Treeview가 있습니다. 노드를 전환하면 트리 뷰의 스크롤 막대가 아래쪽으로 설정됩니다.트 리뷰는 스크롤 위치를 유지합니다.

전환 된 노드를 계속 표시하려면 node.EnsureVisible()을 사용하십시오. 하지만 최종 사용자를 혼란스럽게하기 때문에이 방법이 맘에 들지 않습니다.

은 그래서 더보고 지금은 여기에 제공되는 코드를 사용하고, 트 리뷰의 내용이 스크롤되지 않습니다

Maintain scroll position of treeview

이 코드의 문제를. 스크롤 바가 올바른 위치에 있지만 내용은 아무 것도하지 않습니다. 스크롤 막대를 클릭 할 때까지 (스크롤하지 않습니다) 내용이 표시됩니다.

그래서, 내가 원하는 것은 treenode가 전환 될 때 스크롤 위치를 유지하려고하는 것입니다.

노드를 전환하는 코드입니다. 이 경우에는 노드가 아래쪽에 있습니다. 이 같은 기능의 모양을

// Check a node is selected 
if (tvCategories.SelectedNode == null) 
    return; 

// The first node may not be moved 
if (IsNewRootCategorySelected()) 
    return; 

Point ScrollPos = GetTreeViewScrollPos(tvCategories); 

// Declare and instantiate the parent node 
TreeNodeCollection parent = null; 
if (tvCategories.SelectedNode.Parent == null) 
    parent = tvCategories.Nodes; 
else 
    parent = tvCategories.SelectedNode.Parent.Nodes; 

TreeNode selectedNode = tvCategories.SelectedNode; 
int index = selectedNode.Index; 

// Check there's a next node at the same level 
if (tvCategories.SelectedNode.NextNode == null) 
{ 
    // Check if the parent node has a next node 
    if (tvCategories.SelectedNode.Parent != null && tvCategories.SelectedNode.Parent.NextNode != null) 
    { 
     // get the destination parent 
     TreeNode destParent = selectedNode.Parent.NextNode; 

     // remove selected node from tree view 
     parent[index].Remove(); 

     // If selected node is a category, add the node to the first index 
     if (selectedNode.Tag is Category) 
     { 
      destParent.Nodes.Insert(0, selectedNode); 
     } 

     // If selected node is a question, add node below last category 
     if (selectedNode.Tag is Question) 
     { 
      int newIndex = 0; 

      // Loop through collection to find last category 
      for (int i = destParent.Nodes.Count - 1; i >= 0; i--) 
      { 
       if (destParent.Nodes[i].Tag is Category) 
       { 
        newIndex = i + 1; 
        break; 
       } 
      } 

      destParent.Nodes.Insert(newIndex, selectedNode); 
     } 

     selectedNode.Expand(); 
    } 
} 
else 
{ 
    // Switch nodes in same level 

    tvCategories.BeginUpdate(); 
    _loading = true; 

    if (selectedNode.Tag is Category) 
    { 
     // Only switch category downwards when next node is a catgory 
     if (selectedNode.NextNode.Tag is Category) 
     { 
      // Perform switch 
      TreeNode switchNode = parent[index + 1]; 

      parent[index + 1].Remove(); 
      parent[index].Remove(); 

      parent.Insert(index, switchNode); 
      parent.Insert(index + 1, selectedNode); 
     } 
     else if (selectedNode.NextNode.Tag is Question) 
     { 
      // Make the switch to another node below 
      if (selectedNode.Parent.NextNode != null) 
      { 
       // Parent is always a category 

       TreeNode categoryParent = selectedNode.Parent.NextNode; 

       // Remove selected node from current parent 
       parent.Remove(selectedNode); 

       // Insert selected node 
       categoryParent.Nodes.Insert(0, selectedNode); 

      } 
     } 
    } 
    if (selectedNode.Tag is Question) 
    { 
     if (selectedNode.NextNode.Tag is Question) 
     { 
      // Perform switch 
      TreeNode switchNode = parent[index + 1]; 

      parent[index + 1].Remove(); 
      parent[index].Remove(); 

      parent.Insert(index, switchNode); 
      parent.Insert(index + 1, selectedNode); 
     } 
    } 
} 

tvCategories.EndUpdate(); 
// Set focus 
tvCategories.Focus(); 

tvCategories.SelectedNode = selectedNode; 
SetTreeViewScrollPos(tvCategories, ScrollPos); 
+0

"노드를 전환합니다."그게 무슨 뜻입니까? –

+0

노드를 전환하는 코드가 있습니다. 시작 지점에서 내 편집보기 – Martijn

답변

0
using System.Runtime.InteropServices; 

     [DllImport("user32.dll")] 
     static public extern int SendMessage(
       IntPtr hWnd, // HWND handle to destination window 
       int Msg,  // UINT message 
       int wParam, // WPARAM first message parameter 
       int lParam // LPARAM second message parameter 
       ); 

     public const int SB_LINEUP = 0; 
     public const int SB_LINELEFT = 0; 
     public const int SB_LINEDOWN = 1; 
     public const int SB_LINERIGHT = 1; 
     public const int SB_PAGEUP = 2; 
     public const int SB_PAGELEFT = 2; 
     public const int SB_PAGEDOWN = 3; 
     public const int SB_PAGERIGHT = 3; 
     public const int SB_THUMBPOSITION = 4; 
     public const int SB_THUMBTRACK = 5; 
     public const int SB_TOP = 6; 
     public const int SB_LEFT = 6; 
     public const int SB_BOTTOM = 7; 
     public const int SB_RIGHT = 7; 
     public const int SB_ENDSCROLL = 8; 

     public const int WM_HSCROLL = 276; 
     public const int WM_VSCROLL = 277; 

     public void eZScroll(TreeView treeView, ArrowDirection direction, int numScrolls) 
     { 
      int Msg = 0; 
      int wParam = 0; 
      int lParam = 0; 

      switch (direction) 
      { 
       case ArrowDirection.Up: 
        Msg = WM_VSCROLL; 
        wParam = SB_LINEUP; 
        break; 
       case ArrowDirection.Down: 
        Msg = WM_VSCROLL; 
        wParam = SB_LINEDOWN; 
        break; 
       case ArrowDirection.Left: 
        Msg = WM_HSCROLL; 
        wParam = SB_LINELEFT; 
        break; 
       case ArrowDirection.Right: 
        Msg = WM_HSCROLL; 
        wParam = SB_LINERIGHT; 
        break; 
      } 

      for (int i = 0; i < numScrolls; i++) 
      { 
       SendMessage(treeView.Handle, Msg, wParam, lParam); 
      } 
     } 

붙여 넣기 어딘가에 후 :

public Form1() 
{ 
    InitializeComponent(); 
} 

전화 같은 :이 코드를 잘 해결한다면

eZScroll(treeView1, ArrowDirection.Up, 1); 

는 잘 모르겠어요 그러나 나는 그것이 당신의 문제를 푸는 길에 당신을 잘 이끌어 줄 것이라고 확신합니다.

관련 문제