0

我刚刚为 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;
            }
         }
4

2 回答 2

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;
}
于 2013-06-25T17:49:29.017 回答
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;
        }
    }
于 2013-06-25T21:34:00.743 回答