ErrorProvider: number must be in a range : ErrorProvider « GUI Windows Forms « C# / CSharp Tutorial






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;
                errorProvider1.SetError(numberBox, "You must enter a number between 1 and 10");
            }
        }
        catch (FormatException)
        {
            e.Cancel = true;
            errorProvider1.SetError(numberBox, "You need to enter a whole number");
        }
    }

    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();
    }

    private void numberBox_TextChanged(object sender, EventArgs e)
    {
        MessageBox.Show("text changed");
    }

}
partial class Form1
{
    private void InitializeComponent()
    {
        System.ComponentModel.Container components = new System.ComponentModel.Container();
        this.numberBox = new System.Windows.Forms.TextBox();
        this.label1 = new System.Windows.Forms.Label();
        this.okButton = new System.Windows.Forms.Button();
        this.errorProvider1 = new System.Windows.Forms.ErrorProvider(components);
        ((System.ComponentModel.ISupportInitialize)(this.errorProvider1)).BeginInit();
        this.SuspendLayout();
        // 
        // numberBox
        // 
        this.numberBox.Location = new System.Drawing.Point(190, 12);
        this.numberBox.Name = "numberBox";
        this.numberBox.Size = new System.Drawing.Size(44, 20);
        this.numberBox.TabIndex = 0;
        this.numberBox.Validated += new System.EventHandler(this.numberBox_Validated);
        this.numberBox.Validating += new System.ComponentModel.CancelEventHandler(this.numberBox_Validating);
        this.numberBox.TextChanged += new System.EventHandler(this.numberBox_TextChanged);
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(12, 15);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(164, 13);
        this.label1.TabIndex = 1;
        this.label1.Text = "Enter a number between 1 and 10";
        // 
        // okButton
        // 
        this.okButton.Location = new System.Drawing.Point(251, 9);
        this.okButton.Name = "okButton";
        this.okButton.Size = new System.Drawing.Size(75, 23);
        this.okButton.TabIndex = 2;
        this.okButton.Text = "OK";
        this.okButton.Click += new System.EventHandler(this.okButton_Click);
        // 
        // errorProvider1
        // 
        this.errorProvider1.ContainerControl = this;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(340, 47);
        this.Controls.Add(this.okButton);
        this.Controls.Add(this.label1);
        this.Controls.Add(this.numberBox);
        this.Name = "Form1";
        this.Text = "Form1";
        ((System.ComponentModel.ISupportInitialize)(this.errorProvider1)).EndInit();
        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.ErrorProvider errorProvider1;
}
public class ErrorProviderTextBox
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }
}








23.32.ErrorProvider
23.32.1.ErrorProvider: number must be in a range
23.32.2.ErrorProvider: Text lengthErrorProvider: Text length
23.32.3.Get ErrorMessage from ErrorProviderGet ErrorMessage from ErrorProvider