Jossez
Jossez

Reputation: 35

WebBrowser Control auto size the full web page

Is there a way to make a WebBrowser Control auto size the full web page? Like this:

enter image description here

    Public Class Form1
     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Text = "Web Browser"
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        WebBrowser1.Navigate(TextBox1.Text)
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        WebBrowser1.GoBack()
    End Sub
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        WebBrowser1.GoForward()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        WebBrowser1.Refresh()
    End Sub
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        WebBrowser1.Navigate("http://www.google.pt")
    End Sub
End Class

I just have that code... where i should put the code?

Upvotes: 1

Views: 6619

Answers (2)

Chris Raisin
Chris Raisin

Reputation: 442

In the TabControls first Tab set the text to be the URL address (or blank spaces) and pad it with extra spaces to fit the width of the form. That way it looks like the address bar of your browser (If you want it).

Also make sure you set "Modifiers" to Public on all controls which you want to control via code external to the form.

Most important: Do not use "My.settings" within the form if it is not the form first loaded (main loaded form or module). Always store settings within the first loaded module form along with "My.Settings.Save". Any attempt to save settings in anything bu main mofule/form causes corruption in your "My.Settings" (something I learned by accident and taken 3 months to resolve reason corruption was occuring).

Upvotes: 0

Vivek S.
Vivek S.

Reputation: 21905

  • first you have to put a TimerControl in your form
  • and rest is given below

    Public Class Form1

        Dim pswaHeight As Object
        Dim pswaWidth As Object
        Dim pswaHeightInt As Integer
        Dim pswaWidthInt As Integer
    
        Private Enum Exec
            OLECMDID_OPTICAL_ZOOM = 63
        End Enum
    
        Private Enum execOpt
            OLECMDEXECOPT_DODEFAULT = 0
            OLECMDEXECOPT_PROMPTUSER = 1
            OLECMDEXECOPT_DONTPROMPTUSER = 2
            OLECMDEXECOPT_SHOWHELP = 3
        End Enum
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            pswaHeight = Screen.PrimaryScreen.WorkingArea.Height
            pswaWidth = Screen.PrimaryScreen.WorkingArea.Width
            pswaHeightInt = CInt(pswaHeight)
            pswaWidthInt = CInt(pswaWidth)
    
            Me.Text = "Web Browser"
    
        End Sub
    
        Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    
    
            If WebBrowser1.Width > Math.Round(pswaWidthInt / 5.6) And WebBrowser1.Width < Math.Round(pswaWidthInt / 2.97) Or WebBrowser1.Height > Math.Round(pswaHeightInt / 3.0) And WebBrowser1.Height < Math.Round(pswaHeightInt / 2.4) Then
                Try
                    Dim Res As Object = Nothing
                    Dim MyWeb As Object
                    MyWeb = Me.WebBrowser1.ActiveXInstance
                    MyWeb.ExecWB(Exec.OLECMDID_OPTICAL_ZOOM, execOpt.OLECMDEXECOPT_PROMPTUSER, 40, IntPtr.Zero)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
    
            If WebBrowser1.Width > Math.Round(pswaWidthInt / 2.97) And WebBrowser1.Width < Math.Round(pswaWidthInt / 2.58) Or WebBrowser1.Height > Math.Round(pswaHeightInt / 2.4) And WebBrowser1.Height < Math.Round(pswaHeightInt / 2.13) Then
                Try
                    Dim Res As Object = Nothing
                    Dim MyWeb As Object
                    MyWeb = Me.WebBrowser1.ActiveXInstance
                    MyWeb.ExecWB(Exec.OLECMDID_OPTICAL_ZOOM, execOpt.OLECMDEXECOPT_PROMPTUSER, 50, IntPtr.Zero)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
    
            If WebBrowser1.Width > Math.Round(pswaWidthInt / 2.58) And WebBrowser1.Width < Math.Round(pswaWidthInt / 2.23) Or WebBrowser1.Height > Math.Round(pswaHeightInt / 2.13) And WebBrowser1.Height < Math.Round(pswaHeightInt / 1.85) Then
                Try
                    Dim Res As Object = Nothing
                    Dim MyWeb As Object
                    MyWeb = Me.WebBrowser1.ActiveXInstance
                    MyWeb.ExecWB(Exec.OLECMDID_OPTICAL_ZOOM, execOpt.OLECMDEXECOPT_PROMPTUSER, 60, IntPtr.Zero)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
    
            If WebBrowser1.Width > Math.Round(pswaWidthInt / 2.23) And WebBrowser1.Width < Math.Round(pswaWidthInt / 1.89) Or WebBrowser1.Height > Math.Round(pswaHeightInt / 1.85) And WebBrowser1.Height < Math.Round(pswaHeightInt / 1.64) Then
                Try
                    Dim Res As Object = Nothing
                    Dim MyWeb As Object
                    MyWeb = Me.WebBrowser1.ActiveXInstance
                    MyWeb.ExecWB(Exec.OLECMDID_OPTICAL_ZOOM, execOpt.OLECMDEXECOPT_PROMPTUSER, 70, IntPtr.Zero)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
    
            If WebBrowser1.Width > Math.Round(pswaWidthInt / 1.89) And WebBrowser1.Width < Math.Round(pswaWidthInt / 1.6) Or WebBrowser1.Height > Math.Round(pswaHeightInt / 1.64) And WebBrowser1.Height < Math.Round(pswaHeightInt / 1.53) Then
                Try
                    Dim Res As Object = Nothing
                    Dim MyWeb As Object
                    MyWeb = Me.WebBrowser1.ActiveXInstance
                    MyWeb.ExecWB(Exec.OLECMDID_OPTICAL_ZOOM, execOpt.OLECMDEXECOPT_PROMPTUSER, 80, IntPtr.Zero)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
    
            If WebBrowser1.Width > Math.Round(pswaWidthInt / 1.6) And WebBrowser1.Width < Math.Round(pswaWidthInt / 1.2) Or WebBrowser1.Height > Math.Round(pswaHeightInt / 1.53) And WebBrowser1.Height < Math.Round(pswaHeightInt / 1.16) Then
                Try
                    Dim Res As Object = Nothing
                    Dim MyWeb As Object
                    MyWeb = Me.WebBrowser1.ActiveXInstance
                    MyWeb.ExecWB(Exec.OLECMDID_OPTICAL_ZOOM, execOpt.OLECMDEXECOPT_PROMPTUSER, 90, IntPtr.Zero)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
    
            If WebBrowser1.Width > Math.Round(pswaWidthInt / 1.2) AndAlso WebBrowser1.Height > Math.Round(pswaHeightInt / 1.16) Then
                Try
                    Dim Res As Object = Nothing
                    Dim MyWeb As Object
                    MyWeb = Me.WebBrowser1.ActiveXInstance
                    MyWeb.ExecWB(Exec.OLECMDID_OPTICAL_ZOOM, execOpt.OLECMDEXECOPT_PROMPTUSER, 100, IntPtr.Zero)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        End Sub
    
    
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    
        WebBrowser1.Navigate(TextBox1.Text)
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        WebBrowser1.GoBack()
    End Sub
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        WebBrowser1.GoForward()
    End Sub
    
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        WebBrowser1.Refresh()
    End Sub
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        WebBrowser1.Navigate("http://www.google.pt")
    End Sub
        End Class
    

Upvotes: 1

Related Questions