2013-06-24 3 views
0

CheckedListBox에 드래그 드롭 재정렬 기능을 구현했습니다. 상단에서 끌어서 아래로 스크롤하면 정상 스크롤 막대가 자동 스크롤됩니다. (일반적인 드래그 드롭 자동 스크롤)DragDrop 재정렬을 사용하는 CheckedListBox 자동 스크롤

로드가 많은 WPF 정보를 찾았으나 그 솔루션을 어떻게 적용 할 수 있는지 보지 못했습니다. 내 winform ChekedListBox.

 private void myListBox_DragOver(object sender, DragEventArgs e) 
     { 
      e.Effect = DragDropEffects.Move; 

      Point point = myListBox.PointToClient(new Point(e.X, e.Y)); 

      int index = myListBox.IndexFromPoint(point); 
      int selectedIndex = myListBox.SelectedIndex; 

      if (index < 0) 
      { 
       index = selectedIndex; 
      } 

      if (index != selectedIndex) 
      { 
       myListBox.SwapItems(selectedIndex, index); 
       myListBox.SelectedIndex = index; 
      } 
     } 

답변

1

당신은 자동 스크롤 기능을 구현하기 위해 Timer.Tick 이벤트 핸들러 내에서 CheckedListBox.TopIndex 속성을 업데이트 할 수 있습니다

여기 내 코드입니다. 타이머를 시작하고 중지하려면 CheckedListBox.DragLeaveCheckedListBox.DragEnter 이벤트를 사용하십시오. 코드 스 니펫은 다음과 같습니다.

private void checkedListBox1_DragEnter(object sender, DragEventArgs e) { 
    scrollTimer.Stop(); 
} 

private void checkedListBox1_DragLeave(object sender, EventArgs e) { 
    scrollTimer.Start(); 
} 

private void scrollTimer_Tick(object sender, EventArgs e) { 
    Point cursor = PointToClient(MousePosition); 
    if (cursor.Y < checkedListBox1.Bounds.Top) 
     checkedListBox1.TopIndex -= 1; 
    else if (cursor.Y > checkedListBox1.Bounds.Bottom) 
     checkedListBox1.TopIndex += 1; 
} 
+0

감사합니다. 약간 해키처럼 보이지만, 매우 똑똑한 구현입니다 ... 그리고 그것은 작동합니다! – Christoffer

+0

오직 하나의 문제입니다. 스크롤하는 동안 (목록 상자 외부에서) 마우스 버튼을 놓으면 티커는 시작 모드로 유지됩니다. – Christoffer

0

실제로 이것을 대신 DragOver 이벤트 처리기에 추가했습니다. 어쩌면 매끄러운 것처럼 보이지는 않을지 모르지만 조금 나아질 것입니다.

private void myListBox_DragOver(object sender, DragEventArgs e) 
    { 
     e.Effect = DragDropEffects.Move; 

     Point point = myListBox.PointToClient(new Point(e.X, e.Y)); 

     int index = myListBox.IndexFromPoint(point); 
     int selectedIndex = myListBox.SelectedIndex; 

     if (index < 0) 
     { 
      index = selectedIndex; 
     } 

     if (index != selectedIndex) 
     { 
      myListBox.SwapItems(selectedIndex, index); 
      myListBox.SelectedIndex = index; 
     } 

     if (point.Y <= (Font.Height*2)) 
     { 
      myListBox.TopIndex -= 1; 
     } 
     else if (point.Y >= myListBox.Height - (Font.Height*2)) 
     { 
      myListBox.TopIndex += 1; 
     } 
    }