当我从ListBox中删除项目时,它会滚动到选定的项目。如果没有选择任何项目,则滚动到列表顶部。有没有可能让它在移除物品时保持不动?
我真的不需要启用选择项,但即使我将选择模式设置为none,它也会滚动到顶部。
我正在使用listBox1.Items.Remove(...)方法来删除项目。
我尝试在删除之前获取AutoScrollOffset,然后在删除之后将其设置为与以前相同的X和Y值,但它不起作用。
我将Thread.Sleep和MessageBox放在移除项的方法后面,它看起来像是在我的消息显示之前滚动,所以一定是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(),没有其他重要的东西。
也许有一些我不知道的属性,或者可能我需要安装更新或其他东西。
发布于 2011-05-29 19:27:30
试试这个:
int tempTopIndex = listBox1.TopIndex;
listBox1.Items.Remove(yourItem);
listBox1.TopIndex = tempTopIndex;https://stackoverflow.com/questions/6167311
复制相似问题