19 de marzo de 2012

webBrowser en VS

Para integrar un navegador a una aplicación en Visual studio (VB o C#) primero creamos el formulario y luego se agrega el control webBrowser a la aplicación, esto desde las herramientas del formulario



luego en mi caso al diseño del form le agrego un Textbox, un Timer, un Label y 5 botones tal como se muestra




en las propiedades del form1 modificamos estas partes




y para el control Webbrowser se modifica en las propiedades lo siguiente





y ahora el código para el Form1 en Visual basic


Public Class Form1

   Private Sub Bir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bir.Click
        If TextBox1.Text.Trim() <> String.Empty Then
            If Not TextBox1.Text.Trim().ToLower().StartsWith("http://") Then
                TextBox1.Text = "http://" & TextBox1.Text.Trim()
            End If
        End If
        WebBrowser1.Navigate(TextBox1.Text)
    End Sub


    Private Sub Batras_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Batras.Click
        WebBrowser1.GoBack()
    End Sub

    Private Sub Badelante_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Badelante.Click
        WebBrowser1.GoForward()
    End Sub

    Private Sub Bactualizar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bactualizar.Click
        WebBrowser1.Refresh()
    End Sub

    Private Sub Bdetener_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bdetener.Click
        WebBrowser1.Stop()
    End Sub

    Private Sub Binicio_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Binicio.Click
        WebBrowser1.Navigate("www.pabletoreto.blogspot.com")
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        TextBox1.Text = WebBrowser1.Url.AbsoluteUri

      
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Label1.Text = WebBrowser1.StatusText
    End Sub

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then

            e.Handled = True

            WebBrowser1.Navigate(TextBox1.Text)

        End If
    End Sub
End Class





y para C# este sería el código

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void bir_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate(textBox1.Text);
        }

        private void batras_Click(object sender, EventArgs e)
        {
            webBrowser1.GoBack();
        }

        private void badelante_Click(object sender, EventArgs e)
        {
            webBrowser1.GoForward();
        }

        private void bactualizar_Click(object sender, EventArgs e)
        {
            webBrowser1.Refresh();
        }

        private void Detener_Click(object sender, EventArgs e)
        {
            webBrowser1.Stop();
        }

        private void binicio_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate("www.pabletoreto.blogspot.com");
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            textBox1.Text = webBrowser1.Url.AbsoluteUri;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.Text = webBrowser1.StatusText;
        }

       
            private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)(Keys.Enter))
       {
           e.Handled = true;

                webBrowser1.Navigate(textBox1.Text);
            }
        }
    }
}