Créer une zone texte numérique

Author:

Créer,une,zone,texte,numérique
{filelink=15669}

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

public class TextBoxNumerique : Form
{

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

       private NumericTextBox txtNumerique;
      public TextBoxNumerique()
	  {
            InitializeComponent();
      }

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

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

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

}

// Lancer un Beep si l'utilisateur clique sur une touche non numérique
    public class NumericTextBox : TextBox
    {
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
               Console.Beep();
            }
            base.OnKeyPress(e);
        }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *