2011-05-29 70 views
2

當我從ListBox中刪除項目時,它將滾動到所選項目。如果沒有選擇的項目,那麼它將滾動到列表頂部。移除物品時是否可以保持靜止狀態?ListBox滾動

我並不真的需要選擇啓用的項目,但即使我將選擇模式設置爲無,它將滾動到頂部。

我正在使用listBox1.Items.Remove(...)刪除項目的方法。

我試圖在刪除之前獲取AutoScrollOffset,然後在刪除之後將其設置爲與之前的X和Y相同的值,但它不起作用。

我把Thread.SleepMessageBox之後的方法刪除項目,它看起來像它滾動之前我的消息顯示,所以它必須是Items.Remove導致滾動。

我的列表框的代碼看起來像這樣:

private ListBox listBox1 = new ListBox(); 
this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
          | System.Windows.Forms.AnchorStyles.Left) 
          | System.Windows.Forms.AnchorStyles.Right))); 
this.listBox1.BorderStyle = System.Windows.Forms.BorderStyle.None; 
this.listBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed; 
this.listBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(238))); 
this.listBox1.FormattingEnabled = true; 
this.listBox1.IntegralHeight = false; 
this.listBox1.ItemHeight = 16; 
this.listBox1.Location = new System.Drawing.Point(14, 6); 
this.listBox1.Name = "listBox1"; 
this.listBox1.ScrollAlwaysVisible = true; 
this.listBox1.SelectionMode = System.Windows.Forms.SelectionMode.None; 
this.listBox1.Size = new System.Drawing.Size(180, 388); 
this.listBox1.TabIndex = 0; 
this.listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox1_DrawItem); 

listBox1_DrawItem()只是DrawBackground()Graphics.DrawString()DrawFocusRectangle(),沒有別的可能無所謂。

也許有一些財產,我不知道也許我需要安裝更新或東西...

回答

4

試試這個:

int tempTopIndex = listBox1.TopIndex; 
listBox1.Items.Remove(yourItem); 
listBox1.TopIndex = tempTopIndex; 
+0

這是偉大的和最簡單的:)我的想法關於使用IndexFromPoint,然後計算SelectedIndex並在刪除項目後設置它,但現在沒關係...... – 2011-05-29 11:51:01