ListBox滚动

时间:2011-05-29 10:52:11

标签: c# listbox scroll

当我从ListBox中删除项目时,它会滚动到所选项目。如果没有选择任何项目,那么它将滚动到列表顶部。在移除物品时是否可以保持静止?

我真的不需要选择启用的项目,但即使我将选择模式设置为无,它也会滚动到顶部。

我正在使用listBox1.Items.Remove(...)方法删除项目。

我尝试在删除之前获取AutoScrollOffset,然后在删除之后将其设置为与之前相同的X和Y值,但它不起作用。

我将Thread.SleepMessageBox放在删除项目的方法之后,看起来它在我的消息显示之前滚动,所以它必须是Items.Remove导致滚动。

我的ListBox代码如下:

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(),而且其他任何内容都不重要。

也许有一些我不知道的属性,或者我需要安装更新或其他东西......

1 个答案:

答案 0 :(得分:4)

试试这个:

int tempTopIndex = listBox1.TopIndex;
listBox1.Items.Remove(yourItem);
listBox1.TopIndex = tempTopIndex;
相关问题