Windows Develop Bookmark and Share   
 index > Windows Forms General > listview focusedItem always Null after direct assignment
 

listview focusedItem always Null after direct assignment

Hi,

I have a form with two listviews. The second listview loads items based on the focused item of the first listview. I have the following code to check the items in the first listview:
If listView1.Items.Count > 0 Then

      If myIndex > -1 Then

             lstView1.Items.Item(0).Selected = True

              listView1.Items.Item(0).Focused = True

       End If

        If listView1.FocusedItem Is Nothing Then

              listView1.FocusedItem = listView1.Items.Item(0)

         End If

        listView1.FocusedItem.EnsureVisible()

End If
  
I have an object reference not set to an instance of object error caused by the line listView1.FocusedItem.EnsureVisible(), but the strange thing is that I have assigned the focused item property of the listview and also set the listview item to be focused by using the focus method in the code, and the focuseditem is still null when I checked it with VS debugger. I have spent many hours on this and couldn't figure out why I have this exception. Does anybody know how to resolve this?

Thanks in advance!
Jerry_Hsi  Monday, October 05, 2009 10:50 PM
I had also tried setting the form focus first as my form inherits two other custom forms, but that also did not work.
Jerry_Hsi  Tuesday, October 06, 2009 3:22 AM
No repro.  What View are you using?  Just in case: use EnsureVisible() before setting the focus.

Hans Passant.
nobugz  Tuesday, October 06, 2009 3:38 AM
Hi Nobugz,

Thanks for your reply. My listviews have detail views. Using EnsureVisible before the focus causes the exception as well, the focusedItem property is somehow always set to nothing. When I tried to set it in debugging mode I got a message : operation "=" not defined for listviewitem and listviewitem, and the property continues to be nothing.  
Jerry_Hsi  Tuesday, October 06, 2009 4:01 AM

Hi Jerry_Hsi,

Based on my understanding, the root cause is the code snippet below:

If listView1.FocusedItem Is Nothing Then

          listView1.FocusedItem = listView1.Items.Item(0)        

End If

 

Since the FocusedItem property of ListView is read only, we cannot set it. Instead, we need to write the code below:

listView1.Items.Item(0).Focused = True

 

You can get more about FocusedItem from:

http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.focuseditem(VS.71).aspx

 

Regards,

Aland Li

 


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, October 08, 2009 5:54 AM

You can use google to search for other answers

Custom Search

More Threads

• WebBrowser form/method/name help
• Customize Listbox Items
• SplitContainer - DoubleBuffering does not prevent child panels from flickering
• WebBrowser: Display file from UNC path
• Controls Collection
• Problem with System.ComponentModel.Component
• javier new programmer
• trying to dynamically create a HTML page from a windows service
• Internally Initialize Derived ComboBox Items List?
• Combo Box