ARTICLE

WebBrowser Control in WPF

Posted by Rahul Kumar Saxena Articles | WPF using VB.NET July 21, 2010
In this article, I am going to show how we can use WebBrowser control in WPF application.
Download Files:
 
Reader Level:

In this article, I am going to show how we can use WebBrowser control to show html document within our WPF application. WPF introduces WebBrowser control to ensure that we can show html pages embedded inside the control. Here I use 2 buttons also to go back and forward.

This is my XAML code

<Window x:Class="WebBrowserControlIn_WPF.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="WebBrowser Control in WPF" Height="Auto" Width="Auto" Loaded="Window_Loaded">

    <Grid x:Name="LayoutRoot" Width="Auto">

        <Grid.RowDefinitions>

            <RowDefinition Height="10"/>

            <RowDefinition Height="*"/>

        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="65"></ColumnDefinition>

            <ColumnDefinition Width="*"></ColumnDefinition>

            <ColumnDefinition Width="10"></ColumnDefinition>

        </Grid.ColumnDefinitions>

        <Grid HorizontalAlignment="Stretch" Margin="0,15,0,0" Grid.Row="1" Grid.Column="1" VerticalAlignment="Stretch">

            <WebBrowser x:Name="myBrowser" Margin="0,0,0,0" Cursor="Arrow" Height="Auto"/>

        </Grid>

        <Grid HorizontalAlignment="Stretch" Margin="0,15,0,0" Grid.Row="1" Grid.Column="0" VerticalAlignment="Stretch">

            <Grid.RowDefinitions>

                <RowDefinition Height="40"/>

                <RowDefinition Height="40"/>

            </Grid.RowDefinitions>

            <Grid HorizontalAlignment="Stretch" Margin="0,15,0,0" Grid.Row="0"  VerticalAlignment="Stretch">

                <Border Height="20" BorderThickness="2" CornerRadius="5,5,0,0" VerticalAlignment="Top"   />

                <Button Click="GoBack_Click" Width="50" Height="20" x:Name="GoBack">Go Back</Button>

            </Grid>

 

            <Grid HorizontalAlignment="Stretch" Margin="0,15,0,0" Grid.Row="1"  VerticalAlignment="Stretch">

                <Button Click="GoForward_Click" Width="61" Height="20" x:Name="GoForward">Go Forward</Button>

            </Grid>

        </Grid>

    </Grid>

</Window>

This is my XAML.cs code

Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Navigation
Imports System.Windows.Shapes
Class Window1

    Private Sub GoBack_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        If (myBrowser.CanGoBack) Then
            myBrowser.GoBack()
        Else
            MessageBox.Show("Cannot Go back")
        End If

    End Sub

    Private Sub GoForward_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        If (myBrowser.CanGoForward) Then
            myBrowser.GoForward()
        Else
            MessageBox.Show("Cannot Go Forward")
        End If
    End Sub

    Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        myBrowser.Navigate(New Uri("http://www.c-sharpcorner.com"))
    End Sub
End Class

When we run the application then...

WPFWEBBrowser.jpg

Image 1.

Login to add your contents and source code to this article
share this article :
post comment
 

pls can u send windows app 3-tier projects using wpf with c# and without wpf with c# pls Rahul..................

Posted by jay jay Aug 02, 2011
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Diagram
Become a Sponsor