最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
asp.net C# ListBox 自动滚动到底部方法总结
时间:2022-06-25 03:52:59 编辑:袖梨 来源:一聚教程网
方法一:
代码如下 | 复制代码 |
this.listBox1.Items.Add("new line"); this.listBox1.SelectedIndex = this.listBox1.Items.Count - 1; this.listBox1.SelectedIndex = -1; |
在添加记录后,先选择最后一条记录,滚动条会自动到底部,再取消选择。
缺点是需两次设置选中条目,中间可能会出现反色的动画,影响美观。
方法二:
代码如下 | 复制代码 |
this.listBox1.Items.Add("new line"); this.listBox1.TopIndex = this.listBox1.Items.Count - (int)(this.listBox1.Height / this.listBox1.ItemHeight); |
通过计算ListBox显示的行数,设置TopIndex属性(ListBox中第一个可见项的索引)而达到目的。
方法二 plus:智能滚动
代码如下 | 复制代码 |
bool scroll = false; if(this.listBox1.TopIndex == this.listBox1.Items.Count - (int)(this.listBox1.Height / this.listBox1.ItemHeight)) scroll = true; this.listBox1.Items.Add("new line"); if (scroll) this.listBox1.TopIndex = this.listBox1.Items.Count - (int)(this.listBox1.Height / this.listBox1.ItemHeight); |
在添加新记录前,先计算滚动条是否在底部,从而决定添加后是否自动滚动。
相关文章
- 明日方舟新六星射手维什戴尔怎么获取 明日方舟新六星射手维什戴尔获取方法 04-30
- 原神遗迹龙兽速刷路线分享 原神遗迹龙兽boss在哪 04-30
- 原神4.6林尼配队推荐 原神4.6林尼怎么搭配阵容 04-30
- 王者荣耀五五朋友节蔷薇珍宝阁开启时间2024 王者荣耀蔷薇珍宝阁活动介绍 04-30
- 射雕手游郊狼横道任务怎么做 射雕手游郊狼横道任务攻略 04-30
- 《恋与深空》羁绊值提升攻略 04-30