首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ListBox滚动

ListBox滚动
EN

Stack Overflow用户
提问于 2011-05-29 18:52:11
回答 1查看 3.4K关注 0票数 2

当我从ListBox中删除项目时,它会滚动到选定的项目。如果没有选择任何项目,则滚动到列表顶部。有没有可能让它在移除物品时保持不动?

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

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

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

我将Thread.SleepMessageBox放在移除项的方法后面,它看起来像是在我的消息显示之前滚动,所以一定是Items.Remove导致了滚动。

我的ListBox代码如下所示:

代码语言:javascript
复制
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(),没有其他重要的东西。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-05-29 19:27:30

试试这个:

代码语言:javascript
复制
int tempTopIndex = listBox1.TopIndex;
listBox1.Items.Remove(yourItem);
listBox1.TopIndex = tempTopIndex;
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6167311

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档