我正在尝试在vb.net中实现选项卡式浏览器。除了一件事,一切都很好。当用户在选项卡之间切换时,textbox1
的文本不会更改为视图中当前选项卡的URL。
这是我的代码:
Public Class Form1
Dim int As Integer = 0
Private Sub Loading(ByVal sender As Object, ByVal e As Windows.Forms.WebBrowserProgressChangedEventArgs)
ToolStripProgressBar1.Maximum = e.MaximumProgress
ToolStripProgressBar1.Value = e.MaximumProgress
End Sub
Private Sub Done(ByVal sender As Object, ByVal e As Windows.Forms.WebBrowserDocumentCompletedEventArgs)
TabControl1.SelectedTab.Text = CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).DocumentTitle
TextBox1.Text = CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Url.ToString
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Browser As New WebBrowser
TabControl1.TabPages.Add("New Page")
Browser.Name = "Web Browser"
Browser.Dock = DockStyle.Fill
TabControl1.SelectedTab.Controls.Add(Browser)
AddHandler Browser.ProgressChanged, AddressOf Loading
AddHandler Browser.DocumentCompleted, AddressOf Done
int = int + 1
CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate(My.Settings.homePage)
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
Dim Browser As New WebBrowser
TabControl1.TabPages.Add("New Page")
TabControl1.SelectTab(int)
Browser.Name = "Web Browser"
Browser.Dock = DockStyle.Fill
TabControl1.SelectedTab.Controls.Add(Browser)
AddHandler Browser.ProgressChanged, AddressOf Loading
AddHandler Browser.DocumentCompleted, AddressOf Done
int = int + 1
CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate(My.Settings.homePage)
End Sub
我尝试实现此代码,但它给了我 OutOfRangeException ,调试器建议tabcontrol1.selectedtab.controls.item(0)
的索引不是0
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles TabControl1.SelectedIndexChanged
TextBox1.Text = CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Url.ToString
End Sub
答案 0 :(得分:0)
尝试将您的代码更改为以下内容:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Button7_Click(Nothing, EventArgs.Empty)
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
Dim tab As New TabPage("New Page")
Dim browser As New WebBrowser() With {.Name = "Web Browser", .Dock = DockStyle.Fill}
AddHandler browser.ProgressChanged, AddressOf Loading
AddHandler browser.DocumentCompleted, AddressOf Done
tab.Controls.Add(browser)
Me.TabControl1.TabPages.Add(tab)
Me.TabControl1.SelectTab(tab)
browser.Navigate(My.Settings.homePage)
End Sub
答案 1 :(得分:0)
尝试添加更多支票,如下所示:
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles TabControl1.SelectedIndexChanged
Dim TP As TabPage = TabControl1.SelectedTab
If Not IsNothing(TP) Then
If TP.Controls.Count > 0 AndAlso TypeOf (TP.Controls.Item(0)) Is WebBrowser Then
Dim WB As WebBrowser = CType(TP.Controls.Item(0), WebBrowser)
If Not IsNothing(WB.Url) Then
TextBox1.Text = WB.Url.ToString
End If
End If
End If
End Sub