Search code examples
vb.netbrowserstatus

VB Log WebBrowser Status


i'm working on a new project and I'm currently stuck. I'm adding a logger using a ListBox.

In my project I'm using a WebBrowser and I want to add to the log every time the page has loaded. And i'm trying to do it like this:

    If WebBrowser1.StatusText.ToString = "Klar" Then
        Label14.Text = "Success"
    End If
    If Label14.Text = "Success" Then
        Logger.Items.Add("> Successfully loaded")
    End If

"Klar" translates to "Done" (Swedish)

But the status updates all the time and spam "Done/Klar" so my item to the logger adds 5+ times befor the status changes (I change the page).

So I'm looking for some way to only write it once, maybe check if it already has been added on the item befor. I'm using this in a timer so it will be added many times in the log, but I only want it to be added once if the last item in the log is the same.

Anyone know how this is done?

Thanks for your help! PS. I will take a look at this thread in the morning, But I would really appreciate any help I can get :)


Solution

  • I worked up an example for you and very easy to follow. Just change what you need to. You can use the "Contains Function" to get what you need to check if the item exists in your listbox control.

        Public Class Form1
    
    'Global Variable to hold your string(text)'
    Private strText As String = ""
    
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim myArray() As String = {"Car", "Truck", "Motorcycle"}
        ListBox1.Items.AddRange(myArray)
    End Sub
    
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    
        'First whatever you are sending to the listbox you need to set it to a variable'
        'For example I want to see if Motorcycle exists in the listbox so I set my variable'
        strText = "Motorcycle"
    
        'Now lets check if this exists in our listbox!'
        If ListBox1.Items.Contains(strText) Then
            'We can even select the item that is found'
            ListBox1.SelectedItem = strText
    
            MsgBox("Listbox contains: " & strText) 'Or do what you want here'
    
        Else
            MsgBox("NO FOUND ITEM")
        End If
    
    End Sub
    End Class
    

    You can ignore the load as I used this just for testing... Also just put this code where you have it inserting your data (before it does the insert into your listbox and if its not in the listbox, continue to add what you want to. Let me know how it works out for you!

    Thanks!