C# 带DragDrop重新排序的Autoscroll CheckedListBox
我刚刚为CheckedListBox实现了一个拖放重新排序功能。现在,如果在底部外部拖动,我希望它向下滚动,反之亦然,在顶部拖动(正常的dragdrop autoscroll) 我已经找到了大量WPF信息,但我不知道如何将这些解决方案应用到我的winform ChekedListBox 这是我的密码:C# 带DragDrop重新排序的Autoscroll CheckedListBox,c#,winforms,visual-studio-2012,C#,Winforms,Visual Studio 2012,我刚刚为CheckedListBox实现了一个拖放重新排序功能。现在,如果在底部外部拖动,我希望它向下滚动,反之亦然,在顶部拖动(正常的dragdrop autoscroll) 我已经找到了大量WPF信息,但我不知道如何将这些解决方案应用到我的winform ChekedListBox 这是我的密码: private void myListBox_DragOver(object sender, DragEventArgs e) { e.Ef
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;
}
}
private void myListBox\u DragOver(对象发送方,DragEventArgs e)
{
e、 效果=DragDropEffects.Move;
Point Point=myListBox.PointToClient(新点(e.X,e.Y));
int index=myListBox.IndexFromPoint(点);
int selectedIndex=myListBox.selectedIndex;
如果(指数<0)
{
索引=选定的索引;
}
如果(索引!=selectedIndex)
{
myListBox.SwapItems(selectedIndex,index);
myListBox.SelectedIndex=索引;
}
}
您可以更新事件处理程序中的属性以实现自动滚动功能。要启动和停止计时器,请使用和事件。以下是一段代码片段:
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;
}
private void checkedListBox1_DragEnter(对象发送方,DragEventArgs e){
scrollTimer.Stop();
}
私有void checkedListBox1_DragLeave(对象发送方,事件参数e){
scrollTimer.Start();
}
私有void scrollTimer_Tick(对象发送方,事件参数e){
点光标=指向客户端(鼠标位置);
if(cursor.YcheckedListBox1.Bounds.Bottom)
checkedListBox1.TopIndex+=1;
}
实际上,我最终将其添加到了我的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;
}
}
private void myListBox\u DragOver(对象发送方,DragEventArgs e)
{
e、 效果=DragDropEffects.Move;
Point Point=myListBox.PointToClient(新点(e.X,e.Y));
int index=myListBox.IndexFromPoint(点);
int selectedIndex=myListBox.selectedIndex;
如果(指数<0)
{
索引=选定的索引;
}
如果(索引!=selectedIndex)
{
myListBox.SwapItems(selectedIndex,index);
myListBox.SelectedIndex=索引;
}
如果(点Y=myListBox.Height-(字体高度*2))
{
myListBox.TopIndex+=1;
}
}
谢谢。看起来有点老套,但实现起来非常聪明。。。而且它有效!但只有一个问题。。如果我在滚动时(在列表框外)松开鼠标按钮,股票代码将保持在启动模式。