Online Book Browser : WebBrowser « GUI Windows Forms « C# / CSharp Tutorial






/*
Mastering Visual C#.Net

# Paperback: 800 pages
# Publisher: Sybex; 1st edition (August 20, 2002)
# Language: English
# ISBN-10: 0782129110
# ISBN-13: 978-0782129113

*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

class Book
{
    public string BookTitle;
    public string BookURL;

    public Book( string title, string url)
    {
        BookTitle = title;
        BookURL = url;
    }

    public override string ToString()
    {
        return BookTitle;
    }
}

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

    private void bookCheckButton_Click(object sender, EventArgs e)
    {
        GetWebPage("http://www.apress.com/book/forthcoming.html");
    }

    private MatchCollection GetBookDetailsFromWebPage(string webPage)
    {
        // Use a regex here to find all the book info
        Regex newBooksRegEx =
            new Regex(
            "<a href=\"(/book/bookDisplay\\.html\\?bID=[0-9]+)\">([^<]+)</a>",
            RegexOptions.Singleline);

        return newBooksRegEx.Matches(webPage);
    }

    private void AddBooksToListBox(MatchCollection books)
    {
        foreach (Match bookMatch in books)
        {
            bookList.Items.Add(
                new Book(bookMatch.Groups[2].Value, bookMatch.Groups[1].Value)
            );

        }
    }

    
    private void GetWebPage(string url)
    {
        WebClient web = new WebClient();

        web.DownloadStringCompleted += 
            new DownloadStringCompletedEventHandler(DownloadComplete);
        web.DownloadProgressChanged += 
            new DownloadProgressChangedEventHandler(ProgressChanged);

        bookList.Items.Clear();
        web.DownloadStringAsync(new System.Uri(url));
    }


    private void ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        downloadProgress.Value = e.ProgressPercentage;
    }

    private void DownloadComplete(object sender, DownloadStringCompletedEventArgs e)
    {

            downloadProgress.Value = 100;
            if (!e.Cancelled)
            {
                string newBooksPage = e.Result;

                AddBooksToListBox(
                    GetBookDetailsFromWebPage(newBooksPage)

                );
            }

    }

    private void bookList_DoubleClick(object sender, EventArgs e)
    {
        if (bookList.SelectedIndex != -1)
        {
            Book selectedBook = (Book)bookList.SelectedItem;
            browser.Navigate("http://www.apress.com/" + selectedBook.BookURL, true);
        }
    }
}
partial class Form1
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.bookList = new System.Windows.Forms.ListBox();
        this.bookCheckButton = new System.Windows.Forms.Button();
        this.browser = new System.Windows.Forms.WebBrowser();
        this.downloadProgress = new System.Windows.Forms.ProgressBar();
        this.SuspendLayout();
        // 
        // bookList
        // 
        this.bookList.FormattingEnabled = true;
        this.bookList.Location = new System.Drawing.Point(12, 40);
        this.bookList.Name = "bookList";
        this.bookList.Size = new System.Drawing.Size(410, 355);
        this.bookList.Sorted = true;
        this.bookList.TabIndex = 0;
        this.bookList.DoubleClick += new System.EventHandler(this.bookList_DoubleClick);
        // 
        // bookCheckButton
        // 
        this.bookCheckButton.Location = new System.Drawing.Point(304, 11);
        this.bookCheckButton.Name = "bookCheckButton";
        this.bookCheckButton.Size = new System.Drawing.Size(118, 23);
        this.bookCheckButton.TabIndex = 1;
        this.bookCheckButton.Text = "Check for books";
        this.bookCheckButton.Click += new System.EventHandler(this.bookCheckButton_Click);
        // 
        // browser
        // 
        this.browser.Location = new System.Drawing.Point(0, 0);
        this.browser.Name = "browser";
        this.browser.Size = new System.Drawing.Size(282, 34);
        this.browser.TabIndex = 0;
        this.browser.Visible = false;
        // 
        // downloadProgress
        // 
        this.downloadProgress.Location = new System.Drawing.Point(12, 401);
        this.downloadProgress.Name = "downloadProgress";
        this.downloadProgress.Size = new System.Drawing.Size(410, 23);
        this.downloadProgress.TabIndex = 2;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(436, 447);
        this.Controls.Add(this.downloadProgress);
        this.Controls.Add(this.browser);
        this.Controls.Add(this.bookCheckButton);
        this.Controls.Add(this.bookList);
        this.Name = "Form1";
        this.Text = "Find forthcoming Apress Books";
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.ListBox bookList;
    private System.Windows.Forms.Button bookCheckButton;
    private System.Windows.Forms.WebBrowser browser;
    private System.Windows.Forms.ProgressBar downloadProgress;
}
public class OnlineBookBrowser
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }
}








23.34.WebBrowser
23.34.1.A simple BrowserA simple Browser
23.34.2.Browser with Url entry boxBrowser with Url entry box
23.34.3.Online Book Browser
23.34.4.Simpel Web Browser with forward, backward buttonSimpel Web Browser with forward, backward button