vb.net 如何以编程方式滚动列表视图项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25116065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to scroll listview items programmatically
提问by user3711357
I have a listview control on my WinForms application.
我的 WinForms 应用程序上有一个列表视图控件。
here, on click of separate button, i do change couple of listview items backcolor and reload the whole grid as there are certain changes into database so, reloading from database on each click of button.
在这里,单击单独的按钮,我确实更改了几个列表视图项目的背景色并重新加载整个网格,因为数据库中有某些更改,因此每次单击按钮时都会从数据库重新加载。
Now, problem is, once the grid is reloaded then lastly added items are scrolled so, need to scroll all items and find so, it makes hard to end user.
现在,问题是,一旦网格重新加载,最后添加的项目就会滚动,因此需要滚动所有项目并找到,这让最终用户很难。
Is there any way to ,scroll the lastly added items or updated items into listview automatically (I mean, programmatically so, it could be view to user directly without being manually scrolled).
有什么方法可以将最后添加的项目或更新的项目自动滚动到列表视图中(我的意思是,以编程方式,它可以直接向用户查看而无需手动滚动)。
采纳答案by user3711357
listView1.EnsureVisible(X);where Xis the item index.
listView1.EnsureVisible(X);其中X是项目索引。
This snippet can be used to scroll the ListView automatically to a particular index in the listView.
此代码段可用于将 ListView 自动滚动到listView.
Consider the code: with this you can automatically scroll to the index 8on button click
考虑代码:有了这个,您可以8在单击按钮时自动滚动到索引
private void button2_Click(object sender, EventArgs e)
{
listView1.EnsureVisible(8);
}
回答by ehh
Despite @user3711357 correct answer, I spent too much time trying to understand why it is not working for me. I found that trying to call EnsureVisible in the constructor of the form will not work.
尽管@user3711357 正确答案,但我花了太多时间试图理解为什么它对我不起作用。我发现尝试在表单的构造函数中调用EnsureVisible 是行不通的。
public class MyForm
{
public MyForm()
{
InitializeComponent();
listView1.EnsureVisible(8); // will not work !!!
}
private void MyForm_Load(object sender, EventArgs e)
{
listView1.EnsureVisible(8); // Works fine
}
}
回答by lookat23
Can send messages directly.
可以直接发送消息。
public partial class Form1 : Form
{
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
public Form1()
{
InitializeComponent();
c_scroll.ScrollSlide += C_scroll_ScrollSlide;
}
private void C_vScrollBar_Scroll(object sender, ScrollEventArgs e)
{
const int LVM_SCROLL = (0x1000 + 20);
SendMessage(c_listView_show.Handle, LVM_SCROLL, 0, e.NewValue - e.OldValue);
}
}
回答by Grim
Before you refresh the list, store the currently focussed or selected item (depending on how your interaction code works) into a variable, then you can restore the selected item afterwards. For example;
在刷新列表之前,将当前聚焦或选定的项目(取决于您的交互代码的工作方式)存储到一个变量中,然后您可以在之后恢复选定的项目。例如;
Dim selectedObjectName = listview.SelectedItems(0).Name
...
' refresh your list
...
Dim vItem as ListViewItem
If listview.SelectedItem.ContainsKey(selectedObjectName) Then
vItem = listview.Items(selectedObjectName)
Else
vItem = listview.Items(0)
End If
vItem.Selected = True
vItem.Focus

