我刚刚为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;
}
}
答案 0 :(得分:1)
您可以更新CheckedListBox.TopIndex事件处理程序中的Timer.Tick属性以实现自动滚动功能。要启动和停止计时器,请使用CheckedListBox.DragLeave和CheckedListBox.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;
}
答案 1 :(得分: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;
}
}