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-1 extracted from chapter Windows forms applications


Listing 17-22<     > Listing 18-2


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


Example_18_1_to_rename_Converter.cs
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;

namespace WindowsMileKm {
   public class MyForm : Form {
      private Button Mile2Km;
      private Button Km2Mile;
      private TextBox TextBoxMile;
      private Label Label1;
      private Label Label2;
      private TextBox TextBoxKm;
      public MyForm() { InitializeComponent(); }

      private void InitializeComponent() {
         this.Mile2Km = new Button();
         this.Km2Mile = new Button();
         this.Label1 = new Label();
         this.Label2 = new Label();
         this.TextBoxMile = new TextBox();
         this.TextBoxKm = new TextBox();
         this.SuspendLayout();

         // Mile2Km
         this.Mile2Km.Location = new Point(17814);
         this.Mile2Km.Name = "Mile2Km";
         this.Mile2Km.Size = new System.Drawing.Size(12532);
         this.Mile2Km.TabIndex = 0;
         this.Mile2Km.Text = "Miles -> Kilometers";
         this.Mile2Km.Click += this.Mile2Km_Click;

         // Km2Mile
         this.Km2Mile.Location = new Point(17856);
         this.Km2Mile.Name = "Km2Mile";
         this.Km2Mile.Size = new Size(12532);
         this.Km2Mile.TabIndex = 1;
         this.Km2Mile.Text = "Kilometers -> Miles";
         this.Km2Mile.Click += this.Km2Mile_Click;

         // Label1
         this.Label1.Location = new Point(10424);
         this.Label1.Name = "Label1";
         this.Label1.Size = new Size(6816);
         this.Label1.TabIndex = 3;
         this.Label1.Text = "Miles";

         // Label2
         this.Label2.Location = new Point(10456);
         this.Label2.Name = "Label2";
         this.Label2.Size = new Size(6816);
         this.Label2.TabIndex = 5;
         this.Label2.Text = "Kilometres";

         // TextBoxMile
         this.TextBoxMile.Location = new Point(824);
         this.TextBoxMile.Name = "TextBoxMile";
         this.TextBoxMile.Size = new Size(8820);
         this.TextBoxMile.TabIndex = 4;
         this.TextBoxMile.Text = "0";

         // TextBoxKm
         this.TextBoxKm.Location = new Point(856);
         this.TextBoxKm.Name = "TextBoxKm";
         this.TextBoxKm.Size = new Size(8820);
         this.TextBoxKm.TabIndex = 6;
         this.TextBoxKm.Text = "0";

         // Form1
         this.ClientSize = new Size(315102);
         this.Controls.AddRange(new Control[] {
                  this.TextBoxKm,
                  this.Label2,
                  this.TextBoxMile,
                  this.Label1,
                  this.Km2Mile,
                  this.Mile2Km});
         this.Name = "Form1";
         this.Text = "Converter Miles/Kilometres";
         this.ResumeLayout(false);
      }
      [System.STAThread]
      static void Main() { Application.Run(new MyForm()); }
      const double RATE = 1.609;
      private void Mile2Km_Click(object sender,System.EventArgs e){
         double ditanceMile;
         if double.TryParse( TextBoxMile.Text, out ditanceMile ) )
            TextBoxKm.Text = ( ditanceMile * RATE ).ToString();
      }
      private void Km2Mile_Click(object sender,System.EventArgs e){
         double ditanceKm;
         ifdouble.TryParse( TextBoxKm.Text, out ditanceKm ) )
            TextBoxMile.Text = ( ditanceKm / RATE ).ToString();
      }
   }
} 	
Copyright Patrick Smacchia 2006 2007