Home
Browse all 647 examples
Download all 647 examples
Download sample chapters
Reviews
Errata
Acknowledgments
Links on .NET
Paradoxal Press

Buy directly from Paradoxal Press at $33.99 (Save 43%)



Category: Programming
Level: Beginner to seasoned
900 pages
ISBN-10 097661322-0
ISBN-13 978-097661322-0
$59.99 USA
$79.99 CANADA


Listing 18-2 extracted from chapter Windows forms applications


Listing 18-1<     > Listing 18-3


This listing can be compiled with the command line:
csc.exe /target:exe Example_18_2.cs
Errors: 0 Warnings: 0


Example_18_2.cs
using System;
using System.ComponentModel;
using System.Windows.Forms;

public class PrimeForm : Form {
   public PrimeForm() {
      InitializeComponent();
      InitializeBackgoundWorker();
   }
   private void InitializeBackgoundWorker() {
      backgroundWorker.DoWork += DoWork;
      backgroundWorker.RunWorkerCompleted += Complete;
      backgroundWorker.ProgressChanged += ProgressChanged;
      backgroundWorker.WorkerReportsProgress = true;
      backgroundWorker.WorkerSupportsCancellation = true;
   }
   private void DoWork( object sender,DoWorkEventArgs e ) {
      BackgroundWorker worker = sender as BackgroundWorker;
      e.Result = IsPrime( (int)e.Argument, worker, e );
   }
   private void ProgressChanged( object sender,
                                 ProgressChangedEventArgs e ) {
      progressBar.Value = e.ProgressPercentage;
   }
   private void Complete( object sender,RunWorkerCompletedEventArgs e ) {
      textBoxInput.Enabled = true;
      buttonStart.Enabled = true;
      buttonCancel.Enabled = false;
      if ( e.Error != null //Case where an exception has been raised.
         MessageBox.Show( e.Error.Message );
      else if ( e.Cancelled )// Case of cancellation.
         textBoxResult.Text = "Processing cancelled!";
      else                   // Processing succeeded.
         textBoxResult.Text = e.Result.ToString();
   }
   private void buttonStart_Click( object sender, EventArgs e ) {
      int number = 0;
      if int.TryParse( textBoxInput.Text, out number) ) {
         textBoxResult.Text = String.Empty;
         textBoxInput.Enabled = false;
         buttonStart.Enabled = false;
         buttonCancel.Enabled = true;
         progressBar.Value = 0;
         backgroundWorker.RunWorkerAsync( number );
      else textBoxResult.Text = "input invalid!";
   }
   private void buttonCancel_Click( object sender, EventArgs e ) {
      backgroundWorker.CancelAsync();
      buttonCancel.Enabled = false;
   }
   private string IsPrime( int number,BackgroundWorker worker, 
                           DoWorkEventArgs e) {
      int root = ( (int) System.Math.Sqrt(number) )+1;
      int highestPercentageReached = 0;
      for int = 2; i < root; i++ ) {
         if ( worker.CancellationPending ) {
            e.Cancel = true;
            return String.Empty;
         else {
            if (number % i == 0)
               return "can be divided by " + i.ToString();
            int percentComplete =(int)((float)i / (float)root * 100);
            if ( percentComplete > highestPercentageReached ) {
               highestPercentageReached = percentComplete;
               worker.ReportProgress(percentComplete);
            }
         }
      }
      return "is prime";
   }
   [STAThread]
   static void Main() {
      Application.Run(new PrimeForm());
   }
   private System.ComponentModel.IContainer components = null;

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

   #region Windows Form Designer generated code

   private void InitializeComponent() {
      this.progressBar = new System.Windows.Forms.ProgressBar();
      this.label1 = new System.Windows.Forms.Label();
      this.textBoxInput = new System.Windows.Forms.TextBox();
      this.textBoxResult = new System.Windows.Forms.TextBox();
      this.buttonStart = new System.Windows.Forms.Button();
      this.buttonCancel = new System.Windows.Forms.Button();
      this.backgroundWorker = new System.ComponentModel.BackgroundWorker();
      this.SuspendLayout();
      // 
      // progressBar
      // 
      this.progressBar.Location = new System.Drawing.Point(1233);
      this.progressBar.Name = "progressBar";
      this.progressBar.Size = new System.Drawing.Size(39223);
      this.progressBar.TabIndex = 0;
      // 
      // label1
      // 
      this.label1.AutoSize = true;
      this.label1.Location = new System.Drawing.Point(119);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(2813);
      this.label1.TabIndex = 1;
      this.label1.Text = "Num:";
      // 
      // textBoxInput
      // 
      this.textBoxInput.Location = new System.Drawing.Point(485);
      this.textBoxInput.Name = "textBoxInput";
      this.textBoxInput.Size = new System.Drawing.Size(18120);
      this.textBoxInput.TabIndex = 2;
      // 
      // textBoxResult
      // 
      this.textBoxResult.Location = new System.Drawing.Point(2355);
      this.textBoxResult.Name = "textBoxResult";
      this.textBoxResult.ReadOnly = true;
      this.textBoxResult.Size = new System.Drawing.Size(16920);
      this.textBoxResult.TabIndex = 3;
      // 
      // buttonStart
      // 
      this.buttonStart.Location = new System.Drawing.Point(4124);
      this.buttonStart.Name = "buttonStart";
      this.buttonStart.Size = new System.Drawing.Size(7523);
      this.buttonStart.TabIndex = 4;
      this.buttonStart.Text = "Start";
      this.buttonStart.Click += new 
                            System.EventHandler(this.buttonStart_Click);
      // 
      // buttonCancel
      // 
      this.buttonCancel.Location = new System.Drawing.Point(41233);
      this.buttonCancel.Name = "buttonCancel";
      this.buttonCancel.Size = new System.Drawing.Size(7523);
      this.buttonCancel.TabIndex = 5;
      this.buttonCancel.Text = "Cancel";
      this.buttonCancel.Click += new 
                           System.EventHandler(this.buttonCancel_Click);
      // 
      // PrimeForm
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(49970);
      this.Controls.Add(this.buttonCancel);
      this.Controls.Add(this.buttonStart);
      this.Controls.Add(this.textBoxResult);
      this.Controls.Add(this.textBoxInput);
      this.Controls.Add(this.label1);
      this.Controls.Add(this.progressBar);
      this.Name = "PrimeForm";
      this.Text = "PrimeForm";
      this.ResumeLayout(false);
      this.PerformLayout();
   }
   #endregion
   private System.Windows.Forms.ProgressBar progressBar;
   private System.Windows.Forms.Label label1;
   private System.Windows.Forms.TextBox textBoxInput;
   private System.Windows.Forms.TextBox textBoxResult;
   private System.Windows.Forms.Button buttonStart;
   private System.Windows.Forms.Button buttonCancel;
   private System.ComponentModel.BackgroundWorker backgroundWorker;
}	
Copyright Patrick Smacchia 2006 2007