vb.net 如何以编程方式设置列表视图 FocusedItem
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30937138/
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 set a listview FocusedItem programatically
提问by RagnaRock
How can I set the FocusedItem property programatically?
如何以编程方式设置 FocusedItem 属性?
I've tried this so far with no success:
到目前为止,我已经尝试过但没有成功:
If lvw.FocusedItem Is Nothing Then
If lvw.Items.Count > 0 Then
lvw.Focus()
lvw.HideSelection = False
lvw.Items(0).Selected = True
lvw.Items(0).Focused = True
lvw.FocusedItem = lvw.Items(0)
lvw.Select()
End If
End If
btw the form where the listview is has not called the ShowDialog method yet. Can this be the reason for this not to work?
顺便说一句,列表视图所在的表单还没有调用 ShowDialog 方法。这可能是它不起作用的原因吗?
回答by TnTinMn
You have to understand that each control on the Form and the Form itself is a window and for the window to have focus it must first be created and assigned a handle.
您必须了解 Form 上的每个控件和 Form 本身都是一个窗口,要使窗口具有焦点,必须首先创建它并为其分配一个句柄。
For a basic description of this, I refer you to: All About Handles in Windows FormsThe following excerpts are from the referenced article.
对于这方面的基本描述,我建议您参阅: Windows 窗体中的所有关于句柄以下摘录来自参考文章。
What is a Handle?
A handle (HWND) is the return value from CreateWindowEx which the Windows Operating System uses to identify a window. A "window" in win32 is a much broader concept than you may think - each individual button, combobox, listbox etc comprises a window. (For more information see About Window Classes ) NOTE: there are other things known as "Handles" in the Framework - e.g. GDI Handles from a Bitmap or Handles to Device Contexts (HDCs) - this article discusses HWNDs only.
...
When does a Control create its handle? (When does a control call CreateWindowEx?)
A control tries as much as possible to defer creating its handle. This is because setting properties forces chatty interop between the CLR and user32.
Typically the handles for all the controls are created before the Form.Load event is called. Handles can also be created if the "Handle" property is called and the handle has not yet been created, or CreateControl() is called.
什么是手柄?
句柄 (HWND) 是 Windows 操作系统用来标识窗口的 CreateWindowEx 的返回值。win32 中的“窗口”是一个比您想象的更广泛的概念 - 每个单独的按钮、组合框、列表框等都包含一个窗口。(有关更多信息,请参阅关于窗口类) 注意:框架中还有其他称为“句柄”的内容 - 例如位图的 GDI 句柄或设备上下文 (HDC) 的句柄 - 本文仅讨论 HWND。
...
控件何时创建其句柄?(控件何时调用 CreateWindowEx?)
控件尽可能地尝试推迟创建其句柄。这是因为设置属性会强制 CLR 和 user32 之间进行繁琐的互操作。
通常在调用 Form.Load 事件之前创建所有控件的句柄。如果“Handle”属性被调用并且句柄尚未创建,或者 CreateControl() 被调用,也可以创建句柄。
So a window's handle is not immediately created when you instantiate a control. However, you can force a control to create its handle by referencing its Handle property.
因此,当您实例化控件时,不会立即创建窗口句柄。但是,您可以通过引用其Handle 属性来强制控件创建其句柄。
So if you first get the ListView to create its handle, then when you set those properties that you wanted.
所以如果你首先让 ListView 创建它的句柄,那么当你设置你想要的那些属性时。
Dim f2 As New Form2
' you do not need this condition, it is here only for demonstration purposes
' so that you can step through the code in the debugger and observe the
' code execution.
If Not f2.ListView1.IsHandleCreated Then
' retrieval of the Handle will cause a handle to be created
' if it has not yet been created
' if you delete the If-Then block, you will need to retain the
' following statement
Dim h As IntPtr = f2.ListView1.Handle
End If
f2.ListView1.FocusedItem = f2.ListView1.Items(2)
f2.ListView1.Items(2).Selected = True
f2.ListView1.Items(2).Focused = True
f2.ActiveControl = f2.ListView1
f2.ShowDialog()
回答by Josh
As others have commented, your code should work as written. If all you need is to programmatically access the focused item in your code, you shouldn't be experiencing any difficulties. (If you are, please describe them.)
正如其他人所评论的那样,您的代码应该像编写的那样工作。如果您只需要以编程方式访问代码中的焦点项,那么您应该不会遇到任何困难。(如果你是,请描述它们。)
If you are looking for a visual effect (the row being highlighted), my guess is that your code is in another control's event and the focus is being set back to that control automatically the instant after your code runs. More than likely your code needs to be where it is and trying to move it elsewhere to prevent this issue would be a waste of time.
如果您正在寻找视觉效果(突出显示的行),我的猜测是您的代码在另一个控件的事件中,并且在您的代码运行后立即将焦点自动设置回该控件。您的代码很可能需要在它所在的位置,而试图将其移动到其他地方以防止出现此问题将是浪费时间。
However, there are other ways to set a row apart visually. When a list view isn't likely to stay focused, my preferred method is to distinguish the selected item with a different fore/back color. (You can use the focused item if you prefer, but I find the selected item more useful. Your call.)
但是,还有其他方法可以在视觉上将行分开。当列表视图不太可能保持聚焦时,我的首选方法是使用不同的前/后颜色区分所选项目。(如果您愿意,可以使用重点项目,但我发现所选项目更有用。您的来电。)
Here is an example which visually highlights the selected row, regardless of focus:
这是一个在视觉上突出显示所选行的示例,无论焦点如何:
Private Sub lvw_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lvw.SelectedIndexChanged
If lvw.Items Is Nothing Then Exit Sub
For Each lvi As ListViewItem In lvw.Items
If lvi.Selected = True Then
lvi.ForeColor = Color.DarkGray
lvi.BackColor = Color.LightCyan
Else
lvi.ForeColor = Color.Black
lvi.BackColor = Color.White
End If
Next
End Sub
EDIT:
编辑:
In response to the added information that this form is being displayed using ShowDialog, yes, that is likely the source of your problem.
为了响应使用 显示此表单的附加信息ShowDialog,是的,这可能是您问题的根源。
ShowDialogcreates a new instanceof the form. Therefore, if you have set any properties of a form or its controls, and later call ShowDialogto display that form, the form displayed is a new copy of the original form and will not reflect the changes you made programatically.
ShowDialog创建表单的新实例。因此,如果您设置了窗体或其控件的任何属性,然后调用ShowDialog以显示该窗体,则显示的窗体是原始窗体的新副本,不会反映您以编程方式所做的更改。
Imagine you sit down at a computer where a blank Word document is already open. You type something in it and then open a new document. The text you typed in the first document is not copied to the second. I think this is the root of your troubles here.
想象一下,您坐在一台已经打开空白 Word 文档的计算机前。您在其中键入内容,然后打开一个新文档。您在第一个文档中键入的文本不会复制到第二个文档中。我想这就是你在这里遇到麻烦的根源。

