开发者

ListBox scrolling

开发者 https://www.devze.com 2023-03-08 21:07 出处:网络
When I remove item from ListBox, it\'s scrolling to selected item. If there\'s no item selected, then it\'s scrolling to top of the list. Is it possible to make it stay still when removing items?

When I remove item from ListBox, it's scrolling to selected item. If there's no item selected, then it's scrolling to top of the list. Is it possible to make it stay still when removing items?

I don't really need selecting items e开发者_开发知识库nabled, but even if I set selecting mode to none, it's scrolling to the top.

I'm using listBox1.Items.Remove(...) method for removing items.

I tried to get AutoScrollOffset before removing and then after removing set it the same value of X and Y as it was before, but it doesn't work.

I put Thread.Sleep and MessageBox right after method that removes item and it looks like it scrolls before my message is displayed, so it must be Items.Remove causing the scroll.

My ListBox code looks like that:

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);

in listBox1_DrawItem() is just DrawBackground(), Graphics.DrawString(), DrawFocusRectangle(), and nothing else that could matter.

Maybe there is some property I don't know about or maybe I need to install update or something...


Try this:

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

精彩评论

暂无评论...
验证码 换一张
取 消