Numeric TextBox Control : Numeric TextBox « Components « C# / C Sharp






Numeric TextBox Control

Numeric TextBox Control

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 NumericTextBox numericTextBox1;
      public Form1() {
            InitializeComponent();
      }

        private void InitializeComponent()
        {
            this.numericTextBox1 = new NumericTextBox();
            this.SuspendLayout();

            this.numericTextBox1.Location = new System.Drawing.Point(13, 13);
            this.numericTextBox1.Name = "numericTextBox1";
            this.numericTextBox1.TabIndex = 0;

            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(248, 130);
            this.Controls.Add(this.numericTextBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();
        }

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

}

    public class NumericTextBox : TextBox
    {
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
            base.OnKeyPress(e);
        }
    }
           
       








Related examples in the same category