0

我有一个listboxcontrol插入日志文本并始终显示最后几行的地方

listBoxControl1.SelectedIndex = listBoxControl1.Items.Count - 1;

但是当用户向上/向下滚动列表时,我希望保持用户的滚动位置,
这是取消上面代码的条件

我试过MouseUp/MouseDown但这些事件不会在点击ScrollBar

void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        listBoxControl1.Items.Add(e.UserState);
        if (!mouseDown) // this is not working !!
            listBoxControl1.SelectedIndex = listBoxControl1.Items.Count - 1;
    }

谢谢

4

1 回答 1

1

我相信您可以使用以下方法:

SubscribeScrollEvent(listBoxControl1);    // Before start items adding
bw.RunWorkerAsync();
//...
void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
    UnsubscribeScrollEvent(listBoxControl1);  // After items adding complete 
}
void bw_ProgressChanged(object sender, ProgressChangedEventArgs e) {
    listBoxControl1.Items.Add(e.ProgressPercentage);
    if(!userScrollPerformed)
        listBoxControl1.SelectedIndex = listBoxControl1.Items.Count - 1;
}
//...
void SubscribeScrollEvent(ListBoxControl listBox) {
    var hScroll = listBox.Controls[0] as DevExpress.XtraEditors.HScrollBar;
    var vScroll = listBox.Controls[1] as DevExpress.XtraEditors.VScrollBar;
    vScroll.Scroll += ListBox_Scroll;
    hScroll.Scroll += ListBox_Scroll;
}
void UnubscribeScrollEvent(ListBoxControl listBox) {
    var hScroll = listBox.Controls[0] as DevExpress.XtraEditors.HScrollBar;
    var vScroll = listBox.Controls[1] as DevExpress.XtraEditors.VScrollBar;
    vScroll.Scroll -= ListBox_Scroll;
    hScroll.Scroll -= ListBox_Scroll;
}
bool userScrollPerformed;
void ListBox_Scroll(object sender, ScrollEventArgs e) {
    if(e.Type == ScrollEventType.ThumbTrack)
        userScrollPerformed = true; // set a flag
}
于 2013-01-24T14:18:39.573 回答