-
VB.Net: Tabcontrol and webbrowser control
I've got this little wannabe-webbrowser project that uses the Webbrowser control inside a Tabcontrol.
For opening a new tab+browser I've got this:
Dim n As Integer
Dim w as New WebBrowser
n = tabcontrol1.TabPages.Count + 1
Dim t As New TabPage
t.Text = "Page " & n
w.Dock = DockStyle.Fill
w.Navigate("about:blank")
t.Controls.Add(w)
Me.TabControl1.Controls.Add(t)
Now once I've got it up, how can I tell the Tabcontrol1.Selectedtab's webbrowser control to navigate to cboURL.text?
So, with other words, what should be in place of x in the following lines of code:
cboURL.Items.Add(cboUrl.Text)
x.Navigate(cboUrl.Text)
?
Thnx in advance, graey.
geert
-
w.
cboURL.Items.Add(cboUrl.Text)
w.Navigate(cboUrl.Text)
Phil Weber
http://www.philweber.com
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
-
im sorry if I was unclear.
every webbrowser is called w.
So on tab 1 a browser is called w, on tab 2 a browser is called w, and so on...
and also these arent public declarations, so W is only available within the form's load event...
geert
-
I would probably create a form-level collection (e.g., ArrayList) to contain the WebBrowser controls. Then you could do this:
Dim w As New WebBrowser
BrowserCollection.Add(w)
.
.
.
Dim ActiveBrowser As WebBrowser = CType(BrowserCollection(TabControl1.TabIndex), WebBrowser)
ActiveBrowser.Navigate(cboUrl.Text)
Phil Weber
http://www.philweber.com
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
-
hmm im using vb.net 2005, dunno if that should give difficulties, but i get an error when adding a browser to the arraylist:
[title]NullReferenceException was unhandled.[/title]
Object reference not set to an instance of an object.
as Im new to .net, I don't know what to do with this...
geert
-
Phil Weber
http://www.philweber.com
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
-
Public Class Main
Public i As Integer
Dim Browsercollection As ArrayList
Private Sub newtabToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles newtabToolStripMenuItem.Click
Dim w As New WebBrowser
Dim t As New TabPage
t.Controls.Add(w)
Browsercollection.Add(w)
cboURL.Width = (Me.Width / 2)
w.Dock = DockStyle.Fill
w.Navigate("about:blank")
t.Text = "Tab " & i
tabControl1.Controls.Add(t)
i = i + 1
End Sub
Private Sub cmdGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGo.Click
cboURL.Items.Add(cboURL.Text)
Dim ActiveBrowser As WebBrowser = CType(Browsercollection(tabControl1.TabIndex), WebBrowser)
ActiveBrowser.Navigate(cboURL.Text)
End Sub
End Class
Obviously there were some more items in it, but these are not important (like wether the statusbar is visible and stuff).
geert
-
You're declaring a variable of type ArrayList, but you're never creating an instance of it (New).
Change:
Dim BrowserCollection As ArrayList
To:
Dim BrowserCollection As New ArrayList
Generally, when you get a NullReferenceException it means you've forgotten a New.
Phil Weber
http://www.philweber.com
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
-
thnx 
however this error pops up now:
ArgumentOutOfRangeException was unhandled.
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Code:
Code:
Private Sub cmdGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGo.Click
cboURL.Items.Add(cboURL.Text)
(this line gave the error)Dim ActiveBrowser As WebBrowser = CType(Browsercollection(tabControl1.TabCount), WebBrowser)
ActiveBrowser.Navigate(cboURL.Text)
End Sub
EDIT: i changed tabcontrol1.tabindex into tabcount, still the same error tho...
Last edited by geertvdijk; 03-01-2006 at 08:10 PM.
geert
-
OK, you should be able to figure that out: What is the value of tabControl1.TabIndex when the error occurs? How may items are in the BrowserCollection arraylist? Do the math. ;-)
Phil Weber
http://www.philweber.com
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
-
the strange thing is, no matter how many tabs are open, tabindex is always 6...
and logicly tabcount doesnt work cause it doesnt show the active tabs nr....
geert
-
i see, in vb.net 2005 it is selectedindex...
dunno how it is in earlier .net versions btw... xD
geert
-
Sorry, my mistake. TabIndex is the control's position in the tab order.
Phil Weber
http://www.philweber.com
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
-
ofcourse i could have know that xD
thanks for all of your help, tho...
is there btw any way of calling the webbrowser's find/replace form? xD
geert
-
Hello,
I am trying to do the same make a simple browser with tabs and when I tried out what is above it didn't work for me?
I get this error:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Would anyone know why?
My Code is like this:
Dim w As New WebBrowser
BrowserCollection.Add(w)
Dim ActiveBrowser As WebBrowser = CType(BrowserCollection(Me.TabControl1.SelectedIndex), WebBrowser)
ActiveBrowser.Navigate(Me.ComboBox1.Text)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks