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. For a basic description of this, I refer you to: The following excerpts are from the referenced article. What is a Handle? Dublin strit samantha jang pdf to doc. 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.
WPF, How To, Help, Visual Basic, Tutorial, article As I was finishing off the previous blog item on the Windows Forms ListView, it occurred to me that there isn't much documentation around to explain how to edit the ListView items.
Free download driverpack solution 2011 full version. (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. 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. So if you first get the ListView to create its handle, then when you set those properties that you wanted. 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(). 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 creates a new instance of the form. Therefore, if you have set any properties of a form or its controls, and later call ShowDialog to display that form, the form displayed is a new copy of the original form and will not reflect the changes you made programatically.