WebBrowser en Csharp con Visual Studio.

0
3238

Compilamos con el framework .NET un simple ( además de inútil ) webrowser en C#.

Una de las primeras cosas que se aprende a hacer en un lenguaje de programación es el típìco «Hola Mundo». Como es algo que no está demás hacer por el simple hecho de ejecutar un proyecto. En este caso os dejo la de microsoft para hacer el hola mundo en csharp.

Antes de nada vamos a explicar en un par de frases que es C#, también conocido por csharp. Es un lenguaje de programación orientado a objetos desarrollado por Microsoft como parte de su plataforma .NET. Su sintaxis viene derivada de C/C++ y utiliza el modelo de objetos de .NET

No hay mucha diferencia de hacer un «Hola Mundo» a lo que vamos a hacer, contando que ya tenemos instalado el Visual Studio. Creamos un nuevo proyecto en Visual C# con Windows Forms: Le pondremos de nombre de proyecto test.

Una vez creado el proyecto podemos copiar y pegar el siguiente código y sustituirlo por el nuestro:

Form1.Designer.cs

namespace test
{
    partial class Form1
    {

        private System.ComponentModel.IContainer components = null;


        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region


        private void InitializeComponent()
        {
            this.butBack = new System.Windows.Forms.Button();
            this.butForward = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.butGo = new System.Windows.Forms.Button();
            this.webBrowser1 = new System.Windows.Forms.WebBrowser();
            this.SuspendLayout();
            // 
            // butBack
            // 
            this.butBack.BackColor = System.Drawing.Color.Transparent;
            this.butBack.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
            this.butBack.Cursor = System.Windows.Forms.Cursors.Default;
            this.butBack.FlatAppearance.BorderColor = System.Drawing.Color.White;
            this.butBack.FlatAppearance.BorderSize = 0;
            this.butBack.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.butBack.ForeColor = System.Drawing.Color.Black;
            this.butBack.Location = new System.Drawing.Point(12, 10);
            this.butBack.Name = "butBack";
            this.butBack.Size = new System.Drawing.Size(62, 36);
            this.butBack.TabIndex = 0;
            this.butBack.Text = "Back";
            this.butBack.UseVisualStyleBackColor = false;
            this.butBack.Click += new System.EventHandler(this.butBack_Click);
            // 
            // butForward
            // 
            this.butForward.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
            this.butForward.FlatAppearance.BorderSize = 0;
            this.butForward.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.butForward.ForeColor = System.Drawing.Color.Black;
            this.butForward.Location = new System.Drawing.Point(80, 11);
            this.butForward.Name = "butForward";
            this.butForward.Size = new System.Drawing.Size(55, 36);
            this.butForward.TabIndex = 1;
            this.butForward.Text = "Forward";
            this.butForward.UseVisualStyleBackColor = true;
            this.butForward.Click += new System.EventHandler(this.butForward_Click);
            // 
            // textBox1
            // 
            this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.textBox1.Location = new System.Drawing.Point(150, 19);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(286, 20);
            this.textBox1.TabIndex = 2;
            this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
            this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
            // 
            // butGo
            // 
            this.butGo.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.butGo.FlatAppearance.BorderSize = 0;
            this.butGo.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.butGo.Location = new System.Drawing.Point(424, 19);
            this.butGo.Name = "butGo";
            this.butGo.Size = new System.Drawing.Size(102, 20);
            this.butGo.TabIndex = 3;
            this.butGo.Text = "GO";
            this.butGo.UseVisualStyleBackColor = true;
            this.butGo.Click += new System.EventHandler(this.butGo_Click);
            // 
            // webBrowser1
            // 
            this.webBrowser1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.webBrowser1.Location = new System.Drawing.Point(12, 52);
            this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
            this.webBrowser1.Name = "webBrowser1";
            this.webBrowser1.Size = new System.Drawing.Size(525, 351);
            this.webBrowser1.TabIndex = 4;
            this.webBrowser1.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webBrowser1_DocumentCompleted);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(549, 415);
            this.Controls.Add(this.webBrowser1);
            this.Controls.Add(this.butGo);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.butForward);
            this.Controls.Add(this.butBack);
            this.Name = "Form1";
            this.Text = "Browser on C#";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion
        private System.Windows.Forms.Button butForward;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Button butGo;
        private System.Windows.Forms.WebBrowser webBrowser1;
        private System.Windows.Forms.Button butBack;
    }
}

Form1.cs

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

namespace test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            webBrowser1.ScriptErrorsSuppressed = true;

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            webBrowser1.Navigate("https://sysadm.es");
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

        }

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

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

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

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)ConsoleKey.Enter)
            {
                webBrowser1.Navigate(textBox1.Text);
            }
        }
    }
}

Si respetamos el nombre de proyecto mencionado anteriormente, al darle a compilar nos debería de funcionar perfectamente. Si le hemos puesto otro nombre de proyecto. Tendremos que cambiar los namespaces de cada hoja por el nombre de nuestro proyecto.

Si lo único que queréis es probar el WebBrowser, podéis descargar directamente el setup pulsando aquí. El instalador se ha creado con Inno Setup. En esta entrada anteriormente mostramos como realizar un script de instalación en Inno Setup, no te lo pierdas, pulsa aquí!.

Hasta la próxima!.

DEJA UNA RESPUESTA

Por favor ingrese su comentario!
Por favor ingrese su nombre aquí