Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Displaying Data in WebBrowser
#1
Hi folks,

I am extremely new to Visual Basic. In fact I have only created one simple program and now want to revamp it. Basically, in a ListBox you select a word and it will display the meaning of that word in a TextBox.
Now, what I want to do, is instead of displaying the word meaning in a TextBox, I want to display it in a WebBrowser.
I have searched google for the past two days and can not find the code anywhere to do it so I am hoping someone here can provide a working fix?

Your help is greatly appreciated.

Cheers,
Danno
#2
Hi VicDesigns.
I think I can help you.
I am assuming your webbrowsers name = "webbrowser1" and your listboxs name = "listbox1".
I'm also guessing your code looks something like this right know?
Code:
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        If ListBox1.SelectedIndex = 0 Then
            TextBox1.Text = "Definition"
        End If
        If ListBox1.SelectedIndex = 1 Then
            TextBox1.Text = "Definition"
        End If
        If ListBox1.SelectedIndex = 3 Then
            TextBox1.Text = "Definition"
        End If
        If ListBox1.SelectedIndex = 4 Then
            TextBox1.Text = "Definition"
        End If
    End Sub

If so, all you need to is change a single line of code. Where the code Textbox1.text = "definition", you just need to replace it with webbrowser1.DocumentText = "Definitioin"

Does this Help you out??
--VBCodeGeek--
#3
Hi there,

Thank you very much for your help.
Unfortunately I am still having issues. I am just getting a blank browser. Here is some of the code to give you an idea. I won't post it all.

Here is the first part:
Code:
Public Class Form1

    Private Sub WebBrowser1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub KryptonListBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim Item As String = WebBrowser1.DocumentText.ToString()

        Dim index As Integer = KryptonListBox1.FindString(Item)

        If index = -1 Then

            KryptonListBox1.SelectedIndex = KryptonListBox1.SelectedIndex

        Else

            KryptonListBox1.SetSelected(index, True)

        End If
    End Sub

Part of The ListBox part:
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        KryptonListBox1.Items.Add("Afters")

        KryptonListBox1.Items.Add("Ages")

        KryptonListBox1.Items.Add("Agro")

And Part of the WebBrowser part:
Code:
Private Sub KryptonButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonButton1.Click

        If KryptonListBox1.Text = "Afters" Then

            WebBrowser1.DocumentText = "Dessert - 'Are you having your afters?'"

        ElseIf KryptonListBox1.Text = "Ages" Then

            WebBrowser1.DocumentText = "A long time - 'It took ages!'"

        ElseIf KryptonListBox1.Text = "Agro" Then

            WebBrowser1.DocumentText = "Fight"

        ElseIf KryptonListBox1.Text = "Alco" Then

            WebBrowser1.DocumentText = "An alchoholic"

Now that code, along with the rest of it, if I change the WebBrowser back to TextBox or, in this case, KryptonTextBox1, it will work flawlessly. But I would really like to get it to diaply in the webbrowser because it looks more awesome.

I hope yourself or someone can help. Thank you so much in advance.
#4
Ok. I took another look at it and think I found a solution.

In order for this to work I will need to tell you a little about index's. If you already know about these, you can skip this section. If you add an item to a listbox, It gets an index number. An index number is like an ID number. No two items in the same listbox get the same index number. The first item added to the listbox gets and index of 0. The second item added to the listbox get an index of 1. The index increases each time you add a new item to the listbox. We will use the index numbers to see what item is selected in the listbox.

Now that is out of the way, lets get to work. Delete the sub with the webbrowser text change. No need to have extra code. Also delete the KryptonListBox1_TextChanged sub. It is not needed and is not a real event. (Don't worry about events yet).

Now lets add the code:
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Add as many items to the listbox you want
        KryptonListBox1.Items.Add("PIE")'First item. Index: 0
        KryptonListBox1.Items.Add("ages")' second item. Index: 1
        KryptonListBox1.Items.Add("VB 2010")'Third Item. Index: 2
    End Sub
This is where the magic happens and the webbrowser's text changes.
Code:
Private Sub KryptonButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonButton1.Click

        If KryptonListBox1.SelectedIndex = 0 Then 'the first item added to the listbox

            WebBrowser1.DocumentText = "PIE - 'A tasty desert'"

        ElseIf KryptonListBox1.SelectedIndex = 1 Then 'the second item added to the listbox

            WebBrowser1.DocumentText = "A long time - 'It took ages!'"

        ElseIf KryptonListBox1.SelectedIndex = 2 Then 'the third item added to the listbox

            WebBrowser1.DocumentText = "The application your writing this in"
        End If
    End Sub

Hope this one fixes those errors.
--VBCodeGeek--
#5
Hi mate,

Yeah I made the changes etc. However I am still getting a blank browser. <!-- sConfusedhock: --><img src="{SMILIES_PATH}/icon_eek.gif" alt="Confusedhock:" title="Shocked" /><!-- sConfusedhock: --> <-- Love that smilie. <!-- sSmile --><img src="{SMILIES_PATH}/icon_e_smile.gif" alt="Smile" title="Smile" /><!-- sSmile -->
It seems the ListBox is not communicating with the Search TextBox nor the button. So I am guessing this is why I am not getting anything displayed on the browser. I will play around with it some more and see if I can figure out WTF is going on and will let you know. <!-- sSmile --><img src="{SMILIES_PATH}/icon_e_smile.gif" alt="Smile" title="Smile" /><!-- sSmile -->

Thanks again for your help. Much appreciated.
#6
Nah. Ripped it apart and redid it using the two pieces of code you provided and still getting a blank browser.
Oh well. Guess it aint meant to be. <!-- sSmile --><img src="{SMILIES_PATH}/icon_e_smile.gif" alt="Smile" title="Smile" /><!-- sSmile -->
#7
Hello

Uh since i was too lazy today to read all what you wrote i went ahead and made a project file for you to download so please let me know if this was not what you were looking for

But here is the link: <!-- m --><a class="postlink" href="http://dl.dropbox.com/u/12582973/listboxwithwebbrowser.zip">http://dl.dropbox.com/u/12582973/listbo ... rowser.zip</a><!-- m -->



EDIT:

If this did not help you are welcome to add me on msn and i can talk you through it:
<!-- e --><a href="mailto:Jeg-Kan-Li-Kage@Hotmail.Com">Jeg-Kan-Li-Kage@Hotmail.Com</a><!-- e -->
Website: <!-- m --><a class="postlink" href="http://www.PBAProductions.com">http://www.PBAProductions.com</a><!-- m -->
E-Mail: <!-- e --><a href="mailtoTongueatrick@AriSystems.Org">Patrick@AriSystems.Org</a><!-- e -->
Skype: Qwaxer
Youtube: Qwaxer
[Image: 2hnx8av.jpg]
#8
Hello,
Sorry for not replying until this thread seems to be completely full.

First of all, there is no need to mess around with indexes and such, that may just make your code more confusing. It is much easier just to go straight for the actual text of the listbox item.

So here is some code that should work, it has been tested to work on my system.

Code:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListBox1.Items.Add("Pie")
        ListBox1.Items.Add("Keyboard")
        ListBox1.Items.Add("Shoes")
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        'First, we clear the exisitng WebBrowser text
        WebBrowser1.DocumentText = ""
        'Then, we set the new text
        writeWebBrowser()
        If WebBrowser1.DocumentText Is Nothing Then
            writeWebBrowser()
        Else
            Trace.TraceError("Error.")
        End If
    End Sub
    Sub writeWebBrowser()
        If ListBox1.SelectedItem.ToString = "Pie" Then WebBrowser1.DocumentText = "Pie - A tasty treat"
        If ListBox1.SelectedItem.ToString = "Keyboard" Then WebBrowser1.DocumentText = "Keyboard - Quite literally, a board of keys"
        If ListBox1.SelectedItem.ToString = "Shoes" Then WebBrowser1.DocumentText = "Shoes - A huge problem facing our communities... Not really."
    End Sub
End Class

Sorry if this is hard to understand, but it is very simple. With this code, the listbox has to be clicked a couple of times....

Please tell me if this works or not. And remember to adjust the element names in the code to your elements!!
My Blog | My Setup | My Videos | Have a wonderful day.
#9
brandonio21 Wrote:Hello,
Sorry for not replying until this thread seems to be completely full.

First of all, there is no need to mess around with indexes and such, that may just make your code more confusing. It is much easier just to go straight for the actual text of the listbox item.

So here is some code that should work, it has been tested to work on my system.

Code:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListBox1.Items.Add("Pie")
        ListBox1.Items.Add("Keyboard")
        ListBox1.Items.Add("Shoes")
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        'First, we clear the exisitng WebBrowser text
        WebBrowser1.DocumentText = ""
        'Then, we set the new text
        writeWebBrowser()
        If WebBrowser1.DocumentText Is Nothing Then
            writeWebBrowser()
        Else
            Trace.TraceError("Error.")
        End If
    End Sub
    Sub writeWebBrowser()
        If ListBox1.SelectedItem.ToString = "Pie" Then WebBrowser1.DocumentText = "Pie - A tasty treat"
        If ListBox1.SelectedItem.ToString = "Keyboard" Then WebBrowser1.DocumentText = "Keyboard - Quite literally, a board of keys"
        If ListBox1.SelectedItem.ToString = "Shoes" Then WebBrowser1.DocumentText = "Shoes - A huge problem facing our communities... Not really."
    End Sub
End Class

Sorry if this is hard to understand, but it is very simple. With this code, the listbox has to be clicked a couple of times....

Please tell me if this works or not. And remember to adjust the element names in the code to your elements!!

You are a legend. Thank you so much. Works perfectly. <!-- sConfusedhock: --><img src="{SMILIES_PATH}/icon_eek.gif" alt="Confusedhock:" title="Shocked" /><!-- sConfusedhock: -->
#10
I Wish I would of know that. Would of made my projects a lot more easier <!-- sSad --><img src="{SMILIES_PATH}/icon_e_sad.gif" alt="Sad" title="Sad" /><!-- sSad -->
--VBCodeGeek--
#11
VicDesigns Wrote:You are a legend. Thank you so much. Works perfectly. <!-- sConfusedhock: --><img src="{SMILIES_PATH}/icon_eek.gif" alt="Confusedhock:" title="Shocked" /><!-- sConfusedhock: -->

No problem man! Glad I could help! And remember, if you have anymore problems, feel free to ask them on BP Forums!!
My Blog | My Setup | My Videos | Have a wonderful day.


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I fill my form with MySQL data based on Combobox? kismetgerald 8 26,740 10-14-2012, 09:05 PM
Last Post: brandonio21
  Loading DB data into Combobox then populate TextBox Controls kismetgerald 0 5,717 09-02-2012, 08:44 PM
Last Post: kismetgerald
  Extracting Data From A Website Bradley 3 14,726 05-10-2012, 01:03 AM
Last Post: Bradley
  Bookmarking in Webbrowser Gamester699 0 5,921 11-03-2011, 12:28 PM
Last Post: Gamester699
  Update Data Tabel From Edit mfbf 1 8,556 07-30-2011, 12:54 PM
Last Post: brandonio21
  Custom Webbrowser Component vbcodegeek 2 11,533 07-12-2011, 03:56 AM
Last Post: vbcodegeek
  IceGecko WebBrowser Kaine599 3 15,070 02-17-2011, 02:34 PM
Last Post: redpois0n

Forum Jump:


Users browsing this thread: 1 Guest(s)