[Solved] Dynamic textbox validation [closed]


Assuming cmbRateChart.SelectedValue contains qty value with respect to RangeChart.

private void textBox_Validating(object sender, CancelEventArgs e)
{
    bool cancel = false;
    int number = -1;

    if (int.TryParse(this.textBox.Text, out number))
    {
        var validRange = Convert.ToInt32(cmbRateChart.SelectedValue) * 6;
        if (number <= validRange)
            cancel = false; //passed validation.
        else
            cancel = true; //failed validation, number is not in valid range

    }
    else
        cancel = true;//failed validation: text box is not a number
    e.Cancel = cancel;
}

Usage: Call this function to check the validation.

this.ValidateChildren(ValidationConstraints.Enabled);

Reference: Validation in Windows Forms

2

solved Dynamic textbox validation [closed]