2013-01-24 101 views
0

我有一個listboxcontrol在那裏我將記錄文本 和顯示器總是最後行DevExpress的ListBoxControl滾動事件

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

但當用戶scolls向上/向下的名單,我希望保持用戶的滾動位置 ,
條件取消上述

代碼我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; 
    } 

感謝

+0

你的意思是你想跳過'listBoxControl1.SelectedIndex = listBoxControl1.Items.Count - 1;'這時候用戶執行滾動(上/下)?? – yogi

+0

你在哪裏寫過「listBoxControl1.SelectedIndex = listBoxControl1.Items.Count - 1;」?.................在哪個事件中? – andy

+0

是的,謝謝 – Zyku

回答

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 
} 
+0

完美的作品,謝謝,我等待你的回答:) – Zyku