Devexpress ListBoxControl滚动事件

时间:2013-01-24 10:14:03

标签: c# winforms listbox devexpress

我有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;
    }

由于

1 个答案:

答案 0 :(得分: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
}