每次计时器刷新后,C# 列表框选定索引重置为 0
我有一个列表框,显示每个计时器滴答声从数据库中提取的数据,设置为 2 秒。数据需要始终尽可能保持最新。我正在使用事件处理程序 SelectedIndexChanged 来显示列表框选定索引中的详细数据。
我当前遇到的问题是每次刷新后所选索引都会重置为 0。我需要一种方法来继续刷新,但保留文本字段中的数据。
I've got a listbox that displays data pulled from a database on every timer tick, set at 2 seconds. The data needs to be as current as possible at all times. I'm using an event handler SelectedIndexChanged to display detailed data from a selected index of the listbox.
The problem I have currently is that the selected index resets to 0 after every refresh. I need a way to continue refreshing, but to retain the data in the text fields.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在刷新之前将选定的
ListItem
保存到变量中,然后将值设置回该变量。Save the selected
ListItem
into a variable before you do the refresh and set the value back to it afterwards.这是正常行为,要覆盖该行为,您需要编写代码以在添加新项目后重新选择项目。
要减少闪烁,您可以尝试调用
ListBox 上的 SuspendLayout
,然后在添加项目后调用ResumeLayout()
That is a normal behavior and to override that you will need to write code to reselect the Item after adding new item.
To reduce the Flicker you can try calling
SuspendLayout
on the ListBox and then after Adding Items CallResumeLayout()