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

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » GUI Windows Forms » WebBrowser 
23.34.3.Online Book Browser
/*
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 Bookstring 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(1240);
        this.bookList.Name = "bookList";
        this.bookList.Size = new System.Drawing.Size(410355);
        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(30411);
        this.bookCheckButton.Name = "bookCheckButton";
        this.bookCheckButton.Size = new System.Drawing.Size(11823);
        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(00);
        this.browser.Name = "browser";
        this.browser.Size = new System.Drawing.Size(28234);
        this.browser.TabIndex = 0;
        this.browser.Visible = false;
        // 
        // downloadProgress
        // 
        this.downloadProgress.Location = new System.Drawing.Point(12401);
        this.downloadProgress.Name = "downloadProgress";
        this.downloadProgress.Size = new System.Drawing.Size(41023);
        this.downloadProgress.TabIndex = 2;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(436447);
        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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.