Convert TextBox input to double value


   

using System;
using System.Drawing;
using System.Windows.Forms;
public class EnterPrice : Form {
  private Button enter = new Button();
  private Label answer = new Label();
  private TextBox text = new TextBox( );

  public EnterPrice( ) {
    enter.Text = "Enter Price";
    text.Text = "";
    answer.Text = "";

    Size = new Size(300,200);
    answer.Size = new Size(200,50);

    enter.Location = new Point(30 + enter.Width, 30);
    text.Location = new Point (40 + enter.Width + enter.Width, 30);
    answer.Location = new Point(20, 60);

    AcceptButton = enter;

    Controls.Add(text);
    Controls.Add(answer);
    Controls.Add(enter);

    enter.Click += new EventHandler(Enter_Click);
  }

  protected void Enter_Click(Object sender, EventArgs e) {
    try{
    Console.WriteLine(Double.Parse(text.Text));
    }catch(Exception){
    }
    text.Text = "";
    text.Focus();
  }
  static void Main() {
    Application.Run(new EnterPrice());
  }
}

           
          


A simple text editor


   

using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

public class MenuDialog : Form {
  TextBox text = new TextBox();

  public MenuDialog() {
    Size = new Size(500,200);

    text.Size = new Size(490,190);
    text.Multiline = true;
    text.ScrollBars = ScrollBars.Both;
    text.WordWrap = false;
    text.Location = new Point(5,5);

    MenuItem fileMenu = new MenuItem("File");
    MenuItem open = new MenuItem("Open");
    open.Shortcut = Shortcut.CtrlO;
    MenuItem save = new MenuItem("Save");
    save.Shortcut = Shortcut.CtrlS;
    fileMenu.MenuItems.Add(open);
    fileMenu.MenuItems.Add(save);

    MenuItem formatMenu = new MenuItem("Format");
    MenuItem font = new MenuItem("Font");
    font.Shortcut = Shortcut.CtrlF;
    formatMenu.MenuItems.Add(font);
     
    MainMenu bar = new MainMenu();
    Menu = bar;
    bar.MenuItems.Add(fileMenu);
    bar.MenuItems.Add(formatMenu);

    Controls.Add(text);

    open.Click += new EventHandler(Open_Click);
    save.Click += new EventHandler(Save_Click);
    font.Click += new EventHandler(Font_Click); 
  }
  
  protected void Open_Click(Object sender, EventArgs e) {
    OpenFileDialog o = new OpenFileDialog();
    if(o.ShowDialog() == DialogResult.OK) {
      Stream file = o.OpenFile();
      StreamReader reader = new StreamReader(file);
      char[] data = new char[file.Length];
      reader.ReadBlock(data,0,(int)file.Length);
      text.Text = new String(data);  
      reader.Close();
    }
  }

  protected void Save_Click(Object sender, EventArgs e) {
    SaveFileDialog s = new SaveFileDialog();
    if(s.ShowDialog() == DialogResult.OK) {
      StreamWriter writer = new StreamWriter(s.OpenFile());
      writer.Write(text.Text);
      writer.Close();
    }
  }
  protected void Font_Click(Object sender, EventArgs e) {
    FontDialog f = new FontDialog();
    if(f.ShowDialog() == DialogResult.OK) 
      text.Font = f.Font;
  }

  public static void Main() {
    Application.Run(new MenuDialog());
  }
}

           
          


Text Changed event


   

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

public class Form1 : Form
{
    private System.Windows.Forms.GroupBox GroupBox1;
    private System.Windows.Forms.Label Label4;
    private System.Windows.Forms.Label Label1;
    private System.Windows.Forms.PictureBox pic;
    private System.Windows.Forms.TextBox txt;
    private System.Windows.Forms.Button cmd;
    private System.Windows.Forms.Label Label2;
    private System.Windows.Forms.Label Label3;
    private System.Windows.Forms.ListBox eventLogList;

    public Form1() {
        InitializeComponent();
    }
    private void Log(String data)
    {
        eventLogList.Items.Add(data);
        int itemsPerPage = (int)(eventLogList.Height / eventLogList.ItemHeight);
        eventLogList.TopIndex = eventLogList.Items.Count - itemsPerPage;
    }

    private void txt_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        Log("Key Down: " + e.KeyCode.ToString() + e.KeyValue.ToString());
    }

    private void txt_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
        Log("Key Press: " + e.KeyChar.ToString());
    }

    private void txt_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        Log("Key Up: " + e.KeyCode.ToString() + e.KeyValue.ToString() + " Text is: " + txt.Text);
    }

    private void txt_TextChanged(object sender, System.EventArgs e)
    {
        Log("Changed: " + " Text is: " + txt.Text);
    }

    private void pic_MouseEnter(object sender, System.EventArgs e)
    {
        Log("Mouse Enter");
    }

    private void pic_MouseHover(object sender, System.EventArgs e)
    {
        Log("Mouse Hover");
    }

    private void pic_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        Log("Mouse Down: X=" + e.X.ToString() + " Y=" + e.Y.ToString() + " Button=" + e.Button.ToString());
    }

    private void pic_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        Log("Mouse Up: X=" + e.X.ToString() + " Y=" + e.Y.ToString() + " Button=" + e.Button.ToString());
    }

    private void pic_Click(object sender, System.EventArgs e)
    {
        Log("Click");
    }

    private void pic_DoubleClick(object sender, System.EventArgs e)
    {
        Log("Double Click");
    }

    private void pic_MouseLeave(object sender, System.EventArgs e)
    {
        Log("Mouse Leave");
    }

    private void InitializeComponent()
    {
        this.GroupBox1 = new System.Windows.Forms.GroupBox();
        this.Label4 = new System.Windows.Forms.Label();
        this.Label1 = new System.Windows.Forms.Label();
        this.pic = new System.Windows.Forms.PictureBox();
        this.txt = new System.Windows.Forms.TextBox();
        this.cmd = new System.Windows.Forms.Button();
        this.Label2 = new System.Windows.Forms.Label();
        this.Label3 = new System.Windows.Forms.Label();
        this.eventLogList = new System.Windows.Forms.ListBox();
        this.GroupBox1.SuspendLayout();
        ((System.ComponentModel.ISupportInitialize)(this.pic)).BeginInit();
        this.SuspendLayout();
        // 
        // GroupBox1
        // 
        this.GroupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.GroupBox1.Controls.Add(this.Label4);
        this.GroupBox1.Controls.Add(this.Label1);
        this.GroupBox1.Controls.Add(this.pic);
        this.GroupBox1.Controls.Add(this.txt);
        this.GroupBox1.Controls.Add(this.cmd);
        this.GroupBox1.Controls.Add(this.Label2);
        this.GroupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
        this.GroupBox1.Location = new System.Drawing.Point(7, 0);
        this.GroupBox1.Name = "GroupBox1";
        this.GroupBox1.Size = new System.Drawing.Size(384, 148);
        this.GroupBox1.TabIndex = 12;
        this.GroupBox1.TabStop = false;
        // 
        // Label4
        // 
        this.Label4.Location = new System.Drawing.Point(92, 108);
        this.Label4.Name = "Label4";
        this.Label4.Size = new System.Drawing.Size(56, 16);
        this.Label4.TabIndex = 5;
        this.Label4.Text = "And here:";
        // 
        // Label1
        // 
        this.Label1.Location = new System.Drawing.Point(6, 24);
        this.Label1.Name = "Label1";
        this.Label1.Size = new System.Drawing.Size(144, 16);
        this.Label1.TabIndex = 2;
        this.Label1.Text = "Test keyboard events here:";
        // 
        // pic
        // 
        this.pic.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        this.pic.Location = new System.Drawing.Point(156, 48);
        this.pic.Name = "pic";
        this.pic.Size = new System.Drawing.Size(192, 48);
        this.pic.TabIndex = 3;
        this.pic.TabStop = false;
        this.pic.DoubleClick += new System.EventHandler(this.pic_DoubleClick);
        this.pic.Click += new System.EventHandler(this.pic_Click);
        this.pic.MouseHover += new System.EventHandler(this.pic_MouseHover);
        this.pic.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pic_MouseUp);
        this.pic.MouseEnter += new System.EventHandler(this.pic_MouseEnter);
        this.pic.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pic_MouseDown);
        // 
        // txt
        // 
        this.txt.Location = new System.Drawing.Point(156, 20);
        this.txt.Name = "txt";
        this.txt.Size = new System.Drawing.Size(192, 21);
        this.txt.TabIndex = 1;
        this.txt.KeyUp += new System.Windows.Forms.KeyEventHandler(this.txt_KeyUp);
        this.txt.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txt_KeyPress);
        this.txt.TextChanged += new System.EventHandler(this.txt_TextChanged);
        this.txt.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_KeyDown);
        // 
        // cmd
        // 
        this.cmd.FlatStyle = System.Windows.Forms.FlatStyle.System;
        this.cmd.Location = new System.Drawing.Point(156, 100);
        this.cmd.Name = "cmd";
        this.cmd.Size = new System.Drawing.Size(88, 28);
        this.cmd.TabIndex = 4;
        this.cmd.Text = "Button1";
        this.cmd.MouseLeave += new System.EventHandler(this.pic_MouseLeave);
        this.cmd.Click += new System.EventHandler(this.pic_Click);
        this.cmd.MouseEnter += new System.EventHandler(this.pic_MouseEnter);
        this.cmd.MouseHover += new System.EventHandler(this.pic_MouseHover);
        this.cmd.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pic_MouseUp);
        this.cmd.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pic_MouseDown);
        // 
        // Label2
        // 
        this.Label2.Location = new System.Drawing.Point(20, 52);
        this.Label2.Name = "Label2";
        this.Label2.Size = new System.Drawing.Size(128, 16);
        this.Label2.TabIndex = 2;
        this.Label2.Text = "Test mouse events here:";
        // 
        // Label3
        // 
        this.Label3.Location = new System.Drawing.Point(23, 100);
        this.Label3.Name = "Label3";
        this.Label3.Size = new System.Drawing.Size(64, 24);
        this.Label3.TabIndex = 11;
        this.Label3.Text = "Label3";
        // 
        // eventLogList
        // 
        this.eventLogList.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.eventLogList.FormattingEnabled = true;
        this.eventLogList.IntegralHeight = false;
        this.eventLogList.Location = new System.Drawing.Point(7, 156);
        this.eventLogList.Name = "eventLogList";
        this.eventLogList.Size = new System.Drawing.Size(384, 212);
        this.eventLogList.TabIndex = 10;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(399, 374);
        this.Controls.Add(this.GroupBox1);
        this.Controls.Add(this.Label3);
        this.Controls.Add(this.eventLogList);
        this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.Name = "Form1";
        this.Text = "Event Tracker";
        this.GroupBox1.ResumeLayout(false);
        this.GroupBox1.PerformLayout();
        ((System.ComponentModel.ISupportInitialize)(this.pic)).EndInit();
        this.ResumeLayout(false);

    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }

}



           
          


Get value from TextBox

   
 
using System;
using System.Drawing;
using System.Windows.Forms;

public class InterestCalculator : Form {

    Button buttonCalculate  = new Button();

    TextBox textBoxPrincipal = new TextBox();
    TextBox textBoxRate = new TextBox();
    TextBox textBoxInterest = new TextBox();

    Label labelPrincipal = new Label();
    Label labelRate = new Label();
    Label labelInterest = new Label();

    public InterestCalculator() {
        buttonCalculate.Location = new Point(50, 100);
        buttonCalculate.Text = "Calculate";

        buttonCalculate.Click += new System.EventHandler(this.buttonCalculate_Click);

        this.Controls.Add(buttonCalculate);

        textBoxPrincipal.Location = new Point(10, 20);
        textBoxPrincipal.Size = new Size(150, 10);
        textBoxPrincipal.Text = "100000.00";
        this.Controls.Add(textBoxPrincipal);

        textBoxRate.Location = new Point(10, 60);
        textBoxRate.Size = new Size(150, 10);
        textBoxRate.Text = "0.15";
        this.Controls.Add(textBoxRate);

        textBoxInterest.Location = new Point(10, 150);
        textBoxInterest.Size = new Size(150, 10);
        textBoxInterest.Text = "15000.00";
        this.Controls.Add(textBoxInterest);

        labelPrincipal.Location = new Point(10, 5);
        labelPrincipal.Size = new Size(144, 15);
        labelPrincipal.Text = "Principal";
        this.Controls.Add(labelPrincipal);

        labelRate.Location = new Point(10, 45);
        labelRate.Size = new Size(144, 15);
        labelRate.Text = "Rate";
        this.Controls.Add(labelRate);

        labelInterest.Location = new Point(10, 135);
        labelInterest.Size = new Size(144, 15);
        labelInterest.Text = "Interest";
        this.Controls.Add(labelInterest);
    }

    private void buttonCalculate_Click(object sender, System.EventArgs e) {
        double prin = Convert.ToDouble(textBoxPrincipal.Text);
        double rate = Convert.ToDouble(textBoxRate.Text);
        double amt = prin * rate;
        textBoxInterest.Text = amt.ToString("f2");
    }

    public static void Main(string[] args) {
        Application.Run(new InterestCalculator());
    }
}

    


Keyboard event and TextBox

   
 
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;

public class Form1 : System.Windows.Forms.Form {
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.TextBox textBox2;
    private System.Windows.Forms.GroupBox groupBox1;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.GroupBox groupBox2;
    private System.Windows.Forms.Label label2;
    public Form1() {
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.textBox2 = new System.Windows.Forms.TextBox();
        this.groupBox1 = new System.Windows.Forms.GroupBox();
        this.label1 = new System.Windows.Forms.Label();
        this.groupBox2 = new System.Windows.Forms.GroupBox();
        this.label2 = new System.Windows.Forms.Label();
        this.groupBox1.SuspendLayout();
        this.groupBox2.SuspendLayout();
        this.SuspendLayout();
        // 
        // textBox1
        // 
        this.textBox1.Location = new System.Drawing.Point(16, 24);
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(168, 20);
        this.textBox1.TabIndex = 5;
        this.textBox1.Text = "";
        this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
        // 
        // textBox2
        // 
        this.textBox2.Location = new System.Drawing.Point(16, 24);
        this.textBox2.Name = "textBox2";
        this.textBox2.Size = new System.Drawing.Size(168, 20);
        this.textBox2.TabIndex = 6;
        this.textBox2.Text = "";
        this.textBox2.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox2_KeyDown);
        // 
        // groupBox1
        // 
        this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                this.label1,
                                                                                this.textBox1});
        this.groupBox1.Location = new System.Drawing.Point(8, 8);
        this.groupBox1.Name = "groupBox1";
        this.groupBox1.TabIndex = 7;
        this.groupBox1.TabStop = false;
        this.groupBox1.Text = "Key Monitor";
        // 
        // label1
        // 
        this.label1.Location = new System.Drawing.Point(16, 64);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(168, 20);
        this.label1.TabIndex = 6;
        // 
        // groupBox2
        // 
        this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                this.textBox2,
                                                                                this.label2});
        this.groupBox2.Location = new System.Drawing.Point(8, 120);
        this.groupBox2.Name = "groupBox2";
        this.groupBox2.TabIndex = 8;
        this.groupBox2.TabStop = false;
        this.groupBox2.Text = "Keys Enumeration";
        // 
        // label2
        // 
        this.label2.Location = new System.Drawing.Point(16, 64);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(168, 20);
        this.label2.TabIndex = 9;
        // 
        // Form1
        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(216, 229);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                      this.groupBox2,
                                                                      this.groupBox1});
        this.groupBox1.ResumeLayout(false);
        this.groupBox2.ResumeLayout(false);
        this.ResumeLayout(false);

    }
    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }

    private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
        label1.Text = Convert.ToString(e.KeyValue);
    }

    private void textBox2_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
        if (e.Shift) Console.WriteLine("Shift, ");
        if (e.Alt) Console.WriteLine("Alt, ");
        if (e.Control) Console.WriteLine("Ctrl, ");

        if (e.KeyCode == Keys.W || e.KeyCode == Keys.R ) {
            Console.WriteLine("W R ");
        } else if (e.KeyCode == Keys.Escape && e.Modifiers == (Keys.Shift | Keys.Alt)) {
            Console.WriteLine("Escape");
        } else if (e.KeyCode == Keys.C && e.Modifiers == (Keys.Alt | Keys.Control)) {
            Console.WriteLine("s");
            textBox2.SelectedText = "";
            textBox2.SelectionLength = 0;
        } else {
            Console.WriteLine(Convert.ToString(e.KeyData));
        }
    }
}

    


TextBox location


   
 

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

public class frmLogin : System.Windows.Forms.Form {
    System.Windows.Forms.TextBox txtUser;
    System.Windows.Forms.Button btnOK;
    System.Windows.Forms.Button btnCancel;

    public frmLogin() {
        txtUser = new System.Windows.Forms.TextBox();
        txtUser.Location = new Point(30, 15);
        txtUser.Size = new Size(250, 20);
        txtUser.Text = "";
        txtUser.Name = "txtUser";
        this.Controls.Add(txtUser);

        btnOK = new System.Windows.Forms.Button();
        btnOK.Location = new Point(40,(txtUser.Location.Y + txtUser.Size.Height + btnOK.Size.Height));
        btnOK.Text = "OK";
        btnOK.Name = "btnOK";
        this.Controls.Add(btnOK);

        btnCancel = new System.Windows.Forms.Button();
        btnCancel.Location = new Point((this.Size.Width -
                                        btnCancel.Size.Width) - 40,
           (txtUser.Location.Y + txtUser.Size.Height + btnOK.Size.Height));
        btnCancel.Text = "Cancel";
        btnCancel.Name = "btnCancel";
        this.Controls.Add(btnCancel);

        this.Size = new Size(this.Size.Width, btnCancel.Location.Y +
                             btnCancel.Size.Height + 60);

        btnCancel.Click += new System.EventHandler(btnCancelHandler);
        btnOK.Click += new System.EventHandler(btnEventHandler);
    }

    private void btnEventHandler(object sender, System.EventArgs e) {
        MessageBox.Show(((Button)sender).Name);
    }

    private void btnCancelHandler(object sender, System.EventArgs e) {
        MessageBox.Show("The second handler");
    }

    [STAThread]
    static void Main() {
        Application.Run(new frmLogin());
    }
}

    


new TextBox(), Localtion, Name, TabIndex, Text

   
 

using System;
using System.Windows.Forms;

class MainForm : Form
{
    private Label label1;
    private TextBox textBox1;
    private Button button1;

    public MainForm()
    {
         this.label1 = new Label();
         this.textBox1 = new TextBox();
         this.button1 = new Button();
         this.SuspendLayout();

         this.label1.Location = new System.Drawing.Point(16, 36);
         this.label1.Name = "label1";
         this.label1.Size = new System.Drawing.Size(128, 16);
         this.label1.TabIndex = 0;
         this.label1.Text = "Please enter your name:"; 

         this.textBox1.Location = new System.Drawing.Point(152, 32);
         this.textBox1.Name = "textBox1";
         this.textBox1.TabIndex = 1;
         this.textBox1.Text = "";

         this.button1.Location = new System.Drawing.Point(109, 80);
         this.button1.Name = "button1";
         this.button1.TabIndex = 2;
         this.button1.Text = "Enter";
         this.button1.Click += new System.EventHandler(this.button1_Click);

         this.ClientSize = new System.Drawing.Size(292, 126);
         this.Controls.Add(this.button1);
         this.Controls.Add(this.textBox1);
         this.Controls.Add(this.label1);
         this.ResumeLayout(false);
     }
     private void button1_Click(object sender, System.EventArgs e)
     {
        System.Console.WriteLine("User entered: " + textBox1.Text);
        MessageBox.Show("Welcome, " + textBox1.Text, "Visual C#");
     }
     [STAThread]
     public static void Main()
     {
        Application.EnableVisualStyles();
        Application.Run(new MainForm());
     }
}