none
vb.net 自製瀏覽器開分頁 獲取原始碼問題 RRS feed

  • 問題

  • 網路上找到不少文章是可以利用tab control 開啟webbrowser的

    想請教大大有辦法說把原本點下去會跑出ie出來的target 的網站的分頁

    變成tab control上的分頁嗎

    爬了好多文,都做不出來,有大大有相關資訊可以參考嗎

    感謝了

    以下是我目前參考別人的

    有大大可以幫忙看看怎麼改成可以把target改成在新的tab顯示嗎

    Public Class Form1
    
        'Author : Mohamed Shimran
        'Blog : http://www.ultimateprogrammingtutorials.blogspot.com
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            WebBrowser1.ScriptErrorsSuppressed = True '不要顯示網頁指令錯誤
            Try
                Dim tab As New TabPage
                Dim brws As New WebBrowser
                brws.Dock = DockStyle.Fill
                tab.Text = " New Tab"
                tab.Controls.Add(brws)
                Me.TabControl1.TabPages.Add(tab)
                Me.TabControl1.SelectedTab = tab
                brws.Navigate("")
            Catch ex As Exception
    
            End Try
    
        End Sub
    
    
    
        Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
            Try
                Dim tab As New TabPage
                Dim brws As New WebBrowser
                brws.Dock = DockStyle.Fill
                tab.Text = "New Tab"
                tab.Controls.Add(brws)
                Me.TabControl1.TabPages.Add(tab)
                Me.TabControl1.SelectedTab = tab
                brws.Navigate("")
    
    
            Catch ex As Exception
    
            End Try
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).GoBack()
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).GoForward()
    
        End Sub
    
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Refresh()
        End Sub
    
        Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
            Try
                CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate(TextBox1.Text)
    
            Catch ex As Exception
    
            End Try
        End Sub
        Private Sub TabPage2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    
        End Sub
    
        Private Sub TabPage1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    
        End Sub
    
        Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
            TabControl1.Controls.Remove(TabControl1.SelectedTab)
        End Sub
    
        Private Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
            CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Stop()
        End Sub
    End Class



    2017年2月20日 上午 06:48

解答

  • 您好,

    請問您要的是如果link的target是 _blank 的話,就用新的tab來開嗎?

    1.請新增一個 C# 的類別庫專案,

    將「http://blogs.artinsoft.net/Mrojas/archive/2008/09/18/Extended-WebBrowser-Control-SeriesNewWindow2-Events-in-the-C-WebBrowserControl.aspx」裡面的 code 放進去 class1.cs 之中,要加入 System.Windows.Forms.dll 的參考

    2.在你的vb專案中,加入上面的那個C#類別庫專案的參考,然後就可以使用了哦!

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    	Try
    		Dim tab As New TabPage
    		tab.Text = "Loading..."
    		Dim brws As New ExtendedWebBrowser
    		brws.Dock = DockStyle.Fill
    		brws.ScriptErrorsSuppressed = True
    		brws.Tag = tab
    		brws.Parent = tab
    
    		'InitWebBrowserEvents(brws)
    		Me.TabControl1.TabPages.Add(tab)
    		Me.TabControl1.SelectedTab = tab
    		brws.Navigate("https://dotblogs.com.tw/rainmaker/2017/02/16/232531")
    	Catch ex As Exception
    
    	End Try
    End Sub
    
    
    
    
    Sub InitWebBrowserEvents(wb As ExtendedWebBrowser)
    	AddHandler wb.NewWindow2, AddressOf WebBrowser_NewWindow2
    End Sub
    
    Private Sub WebBrowser_NewWindow2(sender As Object, e As NewWindow2EventArgs)
    	Try
    		Dim tab As New TabPage
    		tab.Text = "Loading..."
    		Dim brws As New ExtendedWebBrowser
    		brws.Dock = DockStyle.Fill
    		brws.ScriptErrorsSuppressed = True
    		brws.Tag = tab
    		brws.Parent = tab
    		e.PPDisp = brws.Application
    		InitWebBrowserEvents(brws)
    		Me.TabControl1.TabPages.Add(tab)
    		Me.TabControl1.SelectedTab = tab
    
    	Catch ex As Exception
    
    	End Try
    End Sub

    整個方案我放在 github上面,你可以下載來看一下,

    https://github.com/rainmakerho/WebBrowserNewWindowDemo

    2017年2月21日 上午 06:46

所有回覆

  • 您好,

    請參考一下,

    http://stackoverflow.com/questions/1286746/open-link-in-new-tab-webbrowser-control

    2017年2月20日 上午 09:02
  • 感謝大大分享,這篇我有看過了,但是功力太弱

    沒辦法改好~有大大可以幫忙嗎

    2017年2月20日 下午 02:08
  • 看看這篇有沒有幫助:

    VB.NET TabControl Web Browser?

    2017年2月21日 上午 02:41
  • 感謝,容小弟研究看看
    2017年2月21日 上午 04:42
  • 好像沒有用呢 大大

    2017年2月21日 上午 04:45
  • 您好,

    請問您要的是如果link的target是 _blank 的話,就用新的tab來開嗎?

    1.請新增一個 C# 的類別庫專案,

    將「http://blogs.artinsoft.net/Mrojas/archive/2008/09/18/Extended-WebBrowser-Control-SeriesNewWindow2-Events-in-the-C-WebBrowserControl.aspx」裡面的 code 放進去 class1.cs 之中,要加入 System.Windows.Forms.dll 的參考

    2.在你的vb專案中,加入上面的那個C#類別庫專案的參考,然後就可以使用了哦!

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    	Try
    		Dim tab As New TabPage
    		tab.Text = "Loading..."
    		Dim brws As New ExtendedWebBrowser
    		brws.Dock = DockStyle.Fill
    		brws.ScriptErrorsSuppressed = True
    		brws.Tag = tab
    		brws.Parent = tab
    
    		'InitWebBrowserEvents(brws)
    		Me.TabControl1.TabPages.Add(tab)
    		Me.TabControl1.SelectedTab = tab
    		brws.Navigate("https://dotblogs.com.tw/rainmaker/2017/02/16/232531")
    	Catch ex As Exception
    
    	End Try
    End Sub
    
    
    
    
    Sub InitWebBrowserEvents(wb As ExtendedWebBrowser)
    	AddHandler wb.NewWindow2, AddressOf WebBrowser_NewWindow2
    End Sub
    
    Private Sub WebBrowser_NewWindow2(sender As Object, e As NewWindow2EventArgs)
    	Try
    		Dim tab As New TabPage
    		tab.Text = "Loading..."
    		Dim brws As New ExtendedWebBrowser
    		brws.Dock = DockStyle.Fill
    		brws.ScriptErrorsSuppressed = True
    		brws.Tag = tab
    		brws.Parent = tab
    		e.PPDisp = brws.Application
    		InitWebBrowserEvents(brws)
    		Me.TabControl1.TabPages.Add(tab)
    		Me.TabControl1.SelectedTab = tab
    
    	Catch ex As Exception
    
    	End Try
    End Sub

    整個方案我放在 github上面,你可以下載來看一下,

    https://github.com/rainmakerho/WebBrowserNewWindowDemo

    2017年2月21日 上午 06:46
  • 感謝 亂馬客 大大的幫助 小弟實驗看看

    非常感謝阿

    2017年2月21日 上午 07:57