Hey Just a probably very easy question but I'm making a small web browser to put in a program. Basically its a tab control and a button atm, when the buttons pressed it uses the code below to add a new tab and make the tab a web browser.
Code:
Dim browse As New WebBrowser
TabControl1.TabPages.Add(i, "page" & i)
TabControl1.SelectTab(i - 1)
browse.Name = "WB"
browse.Dock = DockStyle.Fill
TabControl1.SelectedTab.Controls.Add(browse)
i = i + 1
What I want is when I navigate to a web page for the tab name to be the title of the web page (instead of tabpage1, tabpage2 etc). I was told to put the below code in the web browser navigated sub but seeing as that sub isn't available because there isn't a browser in there until its running I have no where to put the code. What can i do? Sorry if this is about confusing, found it a bit hard to describe Thanks.
Code:
me.tabcontrol1.selectedtab.text=ctype(me.tabcontrol1.selectedtab.controls(0), webbrowser).documenttitle
me.text=ctype(me.tabcontrol1.selectedtab.controls(0), webbrowser).documenttitle & " - ur program name here"
If it is just one tab that has the browser in it, meaning you are not trying to make a browser with tabs in it, such as Firefox or IE 7/8, then you can simply do:
where TabControl1 is the name of the TabControl, and WebBrowser1 is the name of the web browser on that tab. If you are trying to have multiple tabs with multiple browsers then you would have to use a bit more logic and get the first webbrowser object on the active tab and then get its title.
Yeah I got that part for using just code to set the one web browser in the tab control but that's not what I want. Seeing as every time a tab is added it is automatically set as a web browser I need code to set each tab name to the tabs web browser page name. I have no idea how to go about doing it?
Yeah I got that part for using just code to set the one web browser in the tab control but that's not what I want. Seeing as every time a tab is added it is automatically set as a web browser I need code to set each tab name to the tabs web browser page name. I have no idea how to go about doing it?
If the document is not loaded yet how can you know the document title to set the tabpage text? You can't, you got to download the document first and than set it the way it is shown above. But to not let show tabpage1 as its text until the document is downloading you can set it as "Loading..." as firefox does. after the document is loaded you set it to the document title.
To automate assigning the document title to its tabpage i would Inherit WebBrowser control and set the tabpage text when the download of the document is completed. Here is an example of it:
Code:
Public Class MyBrowser
Inherits WebBrowser
Protected Overrides Sub OnNavigating(ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs)
If Me.Parent IsNot Nothing AndAlso Me.Parent.GetType Is GetType(TabPage) Then
Dim tb As TabPage = DirectCast(Me.Parent, TabPage)
tb.Text = "Loading ..."
End If
MyBase.OnNavigating(e)
End Sub
Protected Overrides Sub OnDocumentTitleChanged(ByVal e As System.EventArgs)
If Me.Parent IsNot Nothing AndAlso Me.Parent.GetType Is GetType(TabPage) Then
Dim tb As TabPage = DirectCast(Me.Parent, TabPage)
tb.Text = Me.DocumentTitle
End If
MyBase.OnDocumentTitleChanged(e)
End Sub
End Class
Note that the web browsers parent control should be tabpage.
And this is the code in formload:
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim tp As New TabPage()
TabControl1.TabPages.Add(tp)
TabControl1.SelectTab(tp)
Dim browse As New MyBrowser
browse.Name = "WB"
browse.Dock = DockStyle.Fill
tp.Controls.Add(browse)
browse.Navigate("http://www.code2point.com")
End Sub
End Class
Last edited by VBDT; Apr 30th, 2010 at 03:16 PM.
Rating is a way of saying thank you. Don't forget to rate always!
Thanks, I did say in the first post it was going to be tabbed so just putting that code in wouldn't work. I know it needs to go in the web navigated function so only after the page has loaded will it display the title, but seeing as I haven't got the web navigated function I cant use it. I'll try at code you gave me. thanks.
Sorry, I'm only a beginner at visual basic iv'e put that code in and achieved nothing? Am I making a easy mistake here?
Add a Class to your project and name it "MyBrowser". Copy the code I provided for MyBrowser class to the added class in your project.
Add the exact code I provided for the form load in to the form load event handler and try it out.
Rating is a way of saying thank you. Don't forget to rate always!
oo I didn't see that load code there! Thanks it works perfectly, any idea's how I'd make the text box that i'm using to type addresses in show the URL of the website im on. Atm I can type in an address then navigate to it but if I leave that page by clicking on links the URL in the text box doesn't update.
Well say your on any tab and you want to navigate to a page, you type what you want in the text box, then hit enter The following code then takes you to the web page:
But lets say you go to www.google.com, then you search for something e.g Hi, the textbox should display : "http://www.google.co.uk/search?sourceid=chrome&ie=UTF-8&q=hi" but mine still displays "www.google.com" it doesn't change when you get directed to different sites.
Dim tC = CType(TabControl1.TabPages(TabControl1.SelectedIndex).Controls.Item(0), WebBrowser)
Dim t
t = tC.DocumentTitle
TabControl1.TabPages(TabControl1.SelectedIndex).Text = t
Thanks for that example it's great. Just got a question. When I type in a URL then hit the button, a new tab page is added then the web page is displayed in that tab and a empty tab is left to the right of it. When I go to navigate to a new page I click back on the first tab type in a URL and again instead of loading it in that page it loads a new tab and loads the web page in there. What I wan't is two buttons, one for adding tabs and another for navigating to the URL in the text box to the current tab? Just like a traditionalb browser. I don't know why this is confusing me.
Addhandler did seem like an easier way of doing it, i found an example of using the code for this purpose which was :
Code:
AddHandler browse. what u want,AddressOff and what u want
and then add a new sub like this
Private sub what u want (Byval sender as object, Byval e as and what u want)
Thanks for that example it's great. Just got a question. When I type in a URL then hit the button, a new tab page is added then the web page is displayed in that tab and a empty tab is left to the right of it. When I go to navigate to a new page I click back on the first tab type in a URL and again instead of loading it in that page it loads a new tab and loads the web page in there. What I wan't is two buttons, one for adding tabs and another for navigating to the URL in the text box to the current tab? Just like a traditionalb browser. I don't know why this is confusing me.
The example I provide works like Firefox does. If you want to add new tab you click the "+" tab (which in the example is "New Tab") and then enter the url and press enter key. That loads the page. If you are in a tab that has loaded page and enter new url in the address bar and hit the enter, it will navigate to that new page without adding new tab. In other words it just navigates to another page using the same tab. This is how Firefox does.
Rating is a way of saying thank you. Don't forget to rate always!
@VBDT Ohh i can't beleive that took me so long to understand. This is not my day! I get what you were saying now. Just wondering is there also a possible way that i could link up the add a new tab function to a button. So that in a menustrip I could have a selection that when you click on adds a new tab.
Thank you konithomimo for your example. Basically I'd like it as konithomimo has done but with the function of the textbox showing the current URL.
Last edited by burgergetsbored; Apr 30th, 2010 at 06:19 PM.
Well, the way I did and the way Firefox and EI browsers work are the same. To have a page in a new tab is to click on the new tabpage and than enter url and press enter. I don't understand what is that you don't like here. All major browsers do the same.
Let me explain you how this example works. Let's say you enter an url and press the enter key. What happens is that if the "New Tab" is selected on the TabControl than it adds new tabpage and loads the page. If the selected tabpage is not "New Tab" than it reloads the new page in the same tab.
This means that if you want to add new tab than all you need to do is to select the "New Tab" in the Tabcontrol. This is all you need to do!
This is the answer to your question.
Rating is a way of saying thank you. Don't forget to rate always!
Well, the way I did and the way Firefox and EI browsers work are the same. To have a page in a new tab is to click on the new tabpage and than enter url and press enter. I don't understand what is that you don't like here. All major browsers do the same.
Let me explain you how this example works. Let's say you enter an url and press the enter key. What happens is that if the "New Tab" is selected on the TabControl than it adds new tabpage and loads the page. If the selected tabpage is not "New Tab" than it reloads the new page in the same tab.
This means that if you want to add new tab than all you need to do is to select the "New Tab" in the Tabcontrol. This is all you need to do!
This is the answer to your question.
I tested your project, and it doesn't function like any browser I have ever used. It opens a new tab every time I enter in a new URL, instead of changing the URL of the current tab.
@VBDT Ohh i can't beleive that took me so long to understand. This is not my day! I get what you were saying now. Just wondering is there also a possible way that i could link up the add a new tab function to a button. So that in a menustrip I could have a selection that when you click on adds a new tab.
Thank you konithomimo for your example. Basically I'd like it as konithomimo has done but with the function of the textbox showing the current URL.
Attached is what I believe you are asking for. In order to add a new tab from a menu just call AddTab()
Thanks, that's perfect! just one last finishing touch. How would I go about removing a tab?
I've tried:
Code:
TabControl1.TabPages.RemoveAt(TabControl1.SelectedIndex)
TabControl1.SelectTab(TabControl1.TabPages.Count - 1)
i = i - 1
but if the "add new tab" is selected it will remove this, causing problems. I would go about adding a close button to each tab but I think that would be getting a bit too advanced
You cannot add controls to the actual tab portion of the tab pages, but you can add another tab to the end that will delete the currently Selected tab.
I tested your project, and it doesn't function like any browser I have ever used. It opens a new tab every time I enter in a new URL, instead of changing the URL of the current tab.
That is not TRUE! Lying is not good for your health.
Last edited by VBDT; Apr 30th, 2010 at 07:40 PM.
Rating is a way of saying thank you. Don't forget to rate always!
It would have been nice to have been able to have one like other browsers do and have the close button inside the tab but if its not possible damnn. Well I don't really want another tab on the end I'l have to make the remove tab in the form of a button. But is there anything I can put in that code to stop the "+" tab being deleted?
Here it is with a remove tab. I also prohibited the Add button from being removed, and made it so that there must always be 1 tab open, just as it is in Firefox. Please let me know if there are any other issues.
It would have been nice to have been able to have one like other browsers do and have the close button inside the tab but if its not possible damnn. Well I don't really want another tab on the end I'l have to make the remove tab in the form of a button. But is there anything I can put in that code to stop the "+" tab being deleted?
If you don't want another tab on the end than don't add it. I gave you the basics how to inherit the tabpage and add some functionality that you were asking before. That is the important part not that you want to have tab at the end or you want to click on a button to add tab. I am not going to spent time to do it for you. In the project i provided, adding a button instead of "New Tab" on the side is a very quick thing to do. The functionality is there for you that helps you to make changes and almost no changes in the code. I made the last change to the project and updated it by adding the functionality for the menu strip.
Rating is a way of saying thank you. Don't forget to rate always!
That is not TRUE! Lying is not good for your health.
I just downloaded your project again, and it works now that you have updated it to use 2 buttons, one for navigating and one for a new tab, instead of just the 1 button you had before which navigated and created a new tab.
This video show's you how to create tabs but with the remove button inside, I know its not a tab control but it's still pretty cool, and has a nicer effect than the default tabs. Might be worth looking into in the near future.
I just downloaded your project again, and it works now that you have updated it to use 2 buttons, one for navigating and one for a new tab, instead of just the 1 tab you had before which navigated and created a new tab.
Yes I updated it, but my project doesn't have two buttons one for navigation one for new tab. I am not sure, are you downloading my project or yours???
My project was working before too without any problem. The reason i updated because i added menustrip to the project to show how to click menu item for New Tab and select the new tab tabpage that burgergetsbored was asking.
Edit *
Actual I realized that i uploaded your project which was next to mine. I updated it again with the correct one.
Last edited by VBDT; Apr 30th, 2010 at 11:14 PM.
Rating is a way of saying thank you. Don't forget to rate always!