viernes, 20 de marzo de 2015

Textbox Only Money Values

private void EditMoney_Leave(object sender, EventArgs e)
{
    decimal d;
    if (decimal.TryParse(((TextBox)sender).Text, out d))
        ((TextBox)sender).Text = d.ToString();
}

private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
    //Only basckspace, numbers and decimal dot
    NumberFormatInfo numberFormatInfo = System.Globalization.CultureInfo.CurrentCulture.NumberFormat;
    string decimalSeparator = numberFormatInfo.NumberDecimalSeparator;
    string groupSeparator = numberFormatInfo.NumberGroupSeparator;
    string negativeSign = numberFormatInfo.NegativeSign;

    e.Handled =
            !(e.KeyChar.Equals((char)8)) &&
            !Char.IsDigit(e.KeyChar) &&
            !(e.KeyChar == (char)46 && (((TextBox)sender).Text.IndexOf(decimalSeparator) < 0));

    try
    {
        if (((TextBox)sender).Text.Length == 0 && e.KeyChar.Equals((char)48)) //0
        {
            e.Handled = true;
        } 
        else
        if (((TextBox)sender).Text.Length == 0 && e.KeyChar.ToString().Equals(decimalSeparator)) //.
        {
            ((TextBox)sender).Text = ((char)48).ToString() + decimalSeparator;
            ((TextBox)sender).Select(((TextBox)sender).Text.Length, 0);
            e.Handled = true;
        } 
        else
        if (!(e.KeyChar.Equals((char)8)) &&
            (((TextBox)sender).Text.Substring(((TextBox)sender).Text.IndexOf(decimalSeparator)).Length >= 3))
        {
            e.Handled = true;
        }
    }

    catch { }
}


INTERFACE:

private void InitializeComponent()
{
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.label1 = new System.Windows.Forms.Label();
        this.SuspendLayout();
        //
        // textBox1
        //
        this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        this.textBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.textBox1.Location = new System.Drawing.Point(47, 45);
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(251, 38);
        this.textBox1.TabIndex = 0;
        this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
        this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
        //
        // label1
        //
        this.label1.Location = new System.Drawing.Point(49, 47);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(26, 34);
        this.label1.TabIndex = 1;
        this.label1.Text = "$";
        this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        //
        // Form1
        //
        this.AutoScaleDimensions = new System.Drawing.SizeF(11F, 24F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(347, 145);
        this.Controls.Add(this.label1);
        this.Controls.Add(this.textBox1);
        this.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.Margin = new System.Windows.Forms.Padding(6);
        this.Name = "Form1";
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.Text = "Form1";
        this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
        this.Load += new System.EventHandler(this.Form1_Load);
        this.Shown += new System.EventHandler(this.Form1_Shown);
        this.ResizeEnd += new System.EventHandler(this.Form1_ResizeEnd);
        this.ResumeLayout(false);
        this.PerformLayout();
}