Results 1 to 7 of 7

Thread: Maintain selecting Listview after navigating Webbrowser

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    127

    Maintain selecting Listview after navigating Webbrowser

    Hi guys,

    I have been migrating my VB2010 solutions to VS2013 and found a little..... inconvenience in the new platform. I was using a Listview to navigate through .pdf documents on a WebBrowser and it worked okay. However, in the current environment, (which I'm not sure whether pertinent to the behavior of the program), here's the problem>>

    The WebBrowser, after navigating a document, keeps on being selected. I have tried to always return to selecting the Listview as the main browsing tool as I would like to browse through the documents by just pressing the "down" button of the keyboard. It looks like this:

    vb.net Code:
    1. Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
    2.        
    3.         WebBrowser1.Navigate(Path & "\" & ListView1.FocusedItem.Text())
    4.         ListView1.Select()
    5.  
    6. End Sub

    I also tried using ListView1.Select() upon DocumentCompleted or Navigated, but it doesn't work.

    Any suggestions? Thanks.

    Vizier87

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Maintain selecting Listview after navigating Webbrowser

    Try putting the ListView1.Select() in the WebBrowser_DocumentCompleted event.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    127

    Re: Maintain selecting Listview after navigating Webbrowser

    Well as stated in the first post it didn't work. Any other suggestions?

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Maintain selecting Listview after navigating Webbrowser

    Code:
    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
            ListView1.Select()
        End If
    End Sub

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    127

    Re: Maintain selecting Listview after navigating Webbrowser

    Hi Paul, it still didn't work. I'm pretty confused here. However, if I did it like this:

    vb.net Code:
    1. Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    2.         MsgBox("")
    3.         If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then ListView1.Select()
    4.     End Sub

    Then the listview will be selected. I suppose the MessageBox "exits" the selection of the Webbrowser but I don't know why
    vb.net Code:
    1. If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then ListView1.Select()
    only didn't work.

  6. #6
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: Maintain selecting Listview after navigating Webbrowser

    Hi,

    Are you sure you have provided all the relevant information here about the errors that you may be getting? I have just tried what you are doing and the initial thing that struck me is that you are not checking to make sure an item is selected in the ListView before you use the FocusedItem Property.

    What happens when you move to different items in the ListView is that the current selected item is deselected which raises the SelectedIndexChanged Event and then the new item is selected which again raises the SelectedIndexedChanged event. Due to this you need to make sure that an item is selected in the ListView before you use it and one way to do this is to check the SelectedIndicies property.

    The next thing to realise is that the ListView control does not lose the Focus when you navigate to the selected file in a WebBrowser in this way and therefore there is no need to call the ListViews’s Select Method. Have a play with this which works fine for me:-

    vb.net Code:
    1. 'Create a ListView with a Header and some PDF Items
    2. With ListView1
    3.   .View = View.Details
    4.   .Columns.Add(New ColumnHeader With {.Text = "URL", .Width = -2})
    5.   .Items.Add(New ListViewItem("PDFDoc1.pdf"))
    6.   .Items.Add(New ListViewItem("PDFDoc2.pdf"))
    7.   .Items.Add(New ListViewItem("PDFDoc3.pdf"))
    8. End With
    9.  
    10. 'Navigate to a file if an item is selected in the ListView - Notice that the Control does NOT Lose Focus
    11. Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
    12.   If ListView1.SelectedIndices.Count > 0 Then
    13.     Dim Path As String = "c:\temp"
    14.     WebBrowser1.Navigate(Path & "\" & ListView1.SelectedItems(0).Text())
    15.   End If
    16. End Sub

    I hope that helps.

    Cheers,

    Ian

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Posts
    127

    Re: Maintain selecting Listview after navigating Webbrowser

    Hi Ian, thanks a lot for the input. I did realize SelectedIndexChanged event will be raised twice for this case. I changed my code to this as you suggested:

    vb.net Code:
    1. Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
    2.        
    3.         If ListView1.SelectedIndices.Count > 0 Then
    4.             WebBrowser1.Navigate(Path & "\" & ListView1.FocusedItem.Text())
    5.         End If
    6.  
    7.     End Sub

    And it still happened. As I rolled my scroller on my mouse after clicking on an item on the Listview (with the intent of scrolling down the Listview), but after the document finishes loading the Webbrowser is the one which gets to be scrolled. It's driving me crazy.

    However, I do have something. I wrote this to check the DocumentCompleted event like this:

    vb.net Code:
    1. Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    2.         If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then MsgBox("")
    3. End Sub

    ....the blank message box doesn't appear after loading the .pdf after I selected an item on the Listview!

    Appreciate your help bros. Cheers.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width