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.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;
}
于 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 に答える