using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void numberBox_Validating(object sender, CancelEventArgs e)
{
try
{
int numberEntered = int.Parse(numberBox.Text);
if (numberEntered < 1 || numberEntered > 10)
{
e.Cancel = true;
MessageBox.Show("You have to enter a number between 1 and 10");
}
}
catch (FormatException)
{
e.Cancel = true;
MessageBox.Show("You need to enter an integer");
}
}
private void numberBox_Validated(object sender, EventArgs e)
{
MessageBox.Show("Well done, you managed to enter a valid number");
}
private void okButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
partial class Form1
{
private void InitializeComponent()
{
this.numberBox = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.okButton = new System.Windows.Forms.Button();
this.maskedTextBox1 = new System.Windows.Forms.MaskedTextBox();
this.SuspendLayout();
//
// numberBox
//
this.numberBox.Location = new System.Drawing.Point(253, 15);
this.numberBox.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.numberBox.Name = "numberBox";
this.numberBox.Size = new System.Drawing.Size(57, 22);
this.numberBox.TabIndex = 0;
this.numberBox.Validated += new System.EventHandler(this.numberBox_Validated);
this.numberBox.Validating += new System.ComponentModel.CancelEventHandler(this.numberBox_Validating);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(16, 18);
this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(201, 16);
this.label1.TabIndex = 1;
this.label1.Text = "Enter a number between 1 and 10";
//
// okButton
//
this.okButton.Location = new System.Drawing.Point(335, 11);
this.okButton.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.okButton.Name = "okButton";
this.okButton.Size = new System.Drawing.Size(100, 28);
this.okButton.TabIndex = 2;
this.okButton.Text = "OK";
this.okButton.Click += new System.EventHandler(this.okButton_Click);
//
// maskedTextBox1
//
this.maskedTextBox1.Location = new System.Drawing.Point(0, 0);
this.maskedTextBox1.Name = "maskedTextBox1";
this.maskedTextBox1.Size = new System.Drawing.Size(100, 23);
this.maskedTextBox1.TabIndex = 3;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(453, 246);
this.Controls.Add(this.maskedTextBox1);
this.Controls.Add(this.okButton);
this.Controls.Add(this.label1);
this.Controls.Add(this.numberBox);
this.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.TextBox numberBox;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button okButton;
private System.Windows.Forms.MaskedTextBox maskedTextBox1;
}
public class TextBoxValidation
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}