Posts: 31
Threads: 6
Joined: Jul 2011
Reputation:
0
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
Posts: 19
Threads: 7
Joined: Jun 2011
Reputation:
0
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--
Posts: 31
Threads: 6
Joined: Jul 2011
Reputation:
0
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.
Posts: 19
Threads: 7
Joined: Jun 2011
Reputation:
0
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--
Posts: 31
Threads: 6
Joined: Jul 2011
Reputation:
0
Hi mate,
Yeah I made the changes etc. However I am still getting a blank browser. <!-- s hock: --><img src="{SMILIES_PATH}/icon_eek.gif" alt=" hock:" title="Shocked" /><!-- s hock: --> <-- Love that smilie. <!-- s --><img src="{SMILIES_PATH}/icon_e_smile.gif" alt=" " title="Smile" /><!-- s -->
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. <!-- s --><img src="{SMILIES_PATH}/icon_e_smile.gif" alt=" " title="Smile" /><!-- s -->
Thanks again for your help. Much appreciated.
Posts: 31
Threads: 6
Joined: Jul 2011
Reputation:
0
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. <!-- s --><img src="{SMILIES_PATH}/icon_e_smile.gif" alt=" " title="Smile" /><!-- s -->
Posts: 245
Threads: 31
Joined: Sep 2010
Reputation:
0
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="mailto atrick@AriSystems.Org">Patrick@AriSystems.Org</a><!-- e -->
Skype: Qwaxer
Youtube: Qwaxer
Posts: 1,006
Threads: 111
Joined: Jul 2010
Reputation:
1
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!!
Posts: 31
Threads: 6
Joined: Jul 2011
Reputation:
0
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. <!-- s hock: --><img src="{SMILIES_PATH}/icon_eek.gif" alt=" hock:" title="Shocked" /><!-- s hock: -->
Posts: 19
Threads: 7
Joined: Jun 2011
Reputation:
0
I Wish I would of know that. Would of made my projects a lot more easier <!-- s --><img src="{SMILIES_PATH}/icon_e_sad.gif" alt=" " title="Sad" /><!-- s -->
--VBCodeGeek--
Posts: 1,006
Threads: 111
Joined: Jul 2010
Reputation:
1
|