Question IE Webbrowser

soni_1985

Member
Joined
Jul 11, 2009
Messages
8
Programming Experience
1-3
I m working on project in which i have to add tab & close tab as that of IE.
I m able to do dat,but its not very much clean.Check it out.Plz see if somebody can help me to do it more better.

VB.NET:
Public Class Form5
    Public lSelectedTabPageIndex As Integer
    Public CurrentWEB As WebBrowser

    Public Sub New()
        MyBase.New()
        InitializeComponent()
        TabWebBrowsers.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed
    End Sub

    Private Sub mnuAddTab_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAddTab.Click
        Try
            lSelectedTabPageIndex = lSelectedTabPageIndex + 1
            Call AddNewTabInTabWebBrowsers(True, True)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub AddNewTabInTabWebBrowsers(ByVal bMakeTab As Boolean, ByVal bMakeWebBrowser As Boolean)
        Static lTabPageCount As Integer = 1

        If bMakeTab Then
            Dim tab As TabPage = New TabPage("Connecting...")
            tab.Name = "tab" & lTabPageCount
            tab.Tag = "http://www.google.com"
            tab.ImageIndex = 0
            TabWebBrowsers.TabPages.Add(tab)
            TabWebBrowsers.SelectedIndex = TabWebBrowsers.TabPages.Count - 1
            lSelectedTabPageIndex = TabWebBrowsers.TabPages.Count - 1
            lTabPageCount = lTabPageCount + 1
            If TabWebBrowsers.TabPages.Count > 1 Then
                ContextmnuCloseTab.Enabled = True
            End If
        End If

        If bMakeWebBrowser Then Call AddWebBrowserATRUNTIME(bMakeTab)
    End Sub

    Private Sub AddWebBrowserATRUNTIME(ByVal bMakeTab As Boolean)
        Try
            Dim WEB As New WebBrowser
            Static lWEBCount As Integer = 0
            WEB.Name = "Wbr" & lWEBCount
            WEB.Size = New Size(731, 354)
            WEB.Location = New Point(3, 3)
            If bMakeTab = False Then
                WEB.Navigate("http://www.google.com")
            Else
                WEB.Navigate("about:blank")
            End If

            TabWebBrowsers.TabPages.Item(TabWebBrowsers.TabPages.Count - 1).Controls.Add(WEB)
            lWEBCount = lWEBCount + 1
            TabWebBrowsers.Refresh()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub TabWebBrowsers_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles TabWebBrowsers.DrawItem
        Try
            Dim _imageLocation As Point = New Point(15, 4)
            Dim g As Graphics = e.Graphics
            Dim tp As TabPage = TabWebBrowsers.TabPages(e.Index)
            Dim br As Brush
            Dim sf As New StringFormat

            Dim rold As New RectangleF(e.Bounds.X, e.Bounds.Y + 2, e.Bounds.Width, e.Bounds.Height - 2)

            sf.Alignment = StringAlignment.Center

            Dim strTitle As String = tp.Text

            br = New SolidBrush(Color.LightBlue) ' Change this to your preference
            g.FillRectangle(br, e.Bounds)
            br = New SolidBrush(Color.Black)
            g.DrawString(strTitle, TabWebBrowsers.Font, br, rold, sf)

            Dim img As Image = ImgCommon.Images(0)  'ImgIcons.Images(1) MONISH
            Dim r1 As Rectangle = e.Bounds
            r1 = Me.TabWebBrowsers.GetTabRect(e.Index)
            r1.Offset(2, 2)

            Dim TitleBrush As Brush = New SolidBrush(Color.Black)
            Dim f As Font = Me.Font

            Dim title As String = Me.TabWebBrowsers.TabPages(e.Index).Text
            e.Graphics.DrawImage(img, New Point(r1.X + (Me.TabWebBrowsers.GetTabRect(e.Index).Width - _imageLocation.X) - 10, _imageLocation.Y + 2))
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Call AddNewTabInTabWebBrowsers(False, True)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub ContextmnuCloseTab_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContextmnuCloseTab.Click
        Try

            If lSelectedTabPageIndex + 1 = TabWebBrowsers.TabPages.Count Then
                lSelectedTabPageIndex = lSelectedTabPageIndex - 1
            End If

            With Me.TabWebBrowsers.TabPages
                .Remove(.Item(lSelectedTabPageIndex))
            End With

            With Me.TabWebBrowsers
                .SelectedIndex = lSelectedTabPageIndex
                .Refresh()
            End With

            If TabWebBrowsers.TabPages.Count = 1 Then
                ContextmnuCloseTab.Enabled = False
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub TabWebBrowsers_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TabWebBrowsers.MouseUp
        Try
            Dim tabIndex As Integer
            tabIndex = GetTabIndex(e.Location)
            If tabIndex <> -1 Then
                lSelectedTabPageIndex = CInt(tabIndex.ToString("F0"))
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Function GetTabIndex(ByVal p As Point) As Integer
        Dim result As Integer = -1
        Dim i1 As Integer
        For i1 = 0 To TabWebBrowsers.TabPages.Count - 1
            If TabWebBrowsers.GetTabRect(i1).Contains(p) Then
                GetTabIndex = i1
            End If
        Next
    End Function
End Class

In first tab i m opening google.On other tabs i m opening blank.
I m facing just simple probs in close tab,Suppose i click on first tab that is google,then right click on second tab(tab haeder) to close it...When tab is close,First just a min, google tab is shown then selected tab is shown....I dont want to show even a google tab for a min...


Is there any easy way to craete IE browser..is there any IE Browsr control in VB.Net?
 

Attachments

  • Copy of PRACTISE_VB.zip
    286.1 KB · Views: 6
Don't forget to dispose of your brushes and graphics object when your done with them. Not sure I understand the last part with closing a tab, are you just want to remove a tab with a right button mouse down - even if it is not the current tab?
 
Make a mousedown event for the tabcontrol, then use this:
VB.NET:
If e.Buttons = Windows.Forms.MouseButton.Right Then
  For tabIndex = 0 To tabcontrol.tabpages.count -1
      If tabcontrol.GetTabRect(tabIndex).Contains(e.x,e.y) Then
          tabcontrol.tabpages.RemoveAt(tabIndex)
      End If
  Next
End If
 
hello sir tell me one thing is there also an easy way to craete IE WEbbrowser....There is also an IE Browser controlin Vb.net,have u any idea of it..

[ame=http://www.vbforums.com/showthread.php?p=3568007#post3568007]VS 2005 IE Webbrowser - VBForums[/ame]
 
hello sir tell me one thing is there also an easy way to craete IE WEbbrowser....There is also an IE Browser controlin Vb.net,have u any idea of it..

VS 2005 IE Webbrowser - VBForums

just add the web browser control.

add a textbox and button (txtURL & btnGO)

double click the button

VB.NET:
    Private Sub btnGO_click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGO.click
        Dim Address as string = txtURL.text
        Dim url As System.Uri = New System.Uri(Address)
        Me.WebBrowser1.Navigate(url)

    End Sub
 
Back
Top