Click here to Skip to main content
15,922,894 members
Home / Discussions / C#
   

C#

 
AnswerRe: Serialization problem Pin
Matt Gerrans13-Oct-05 11:25
Matt Gerrans13-Oct-05 11:25 
GeneralRe: Serialization problem Pin
Heks13-Oct-05 11:38
Heks13-Oct-05 11:38 
QuestionValidation in Business Layer Pin
tsramkumar13-Oct-05 10:08
tsramkumar13-Oct-05 10:08 
AnswerRe: Validation in Business Layer Pin
facorreia13-Oct-05 11:08
facorreia13-Oct-05 11:08 
QuestionRichTextBox save as HTML Pin
jeweladdict13-Oct-05 8:55
jeweladdict13-Oct-05 8:55 
AnswerRe: RichTextBox save as HTML Pin
Daniel Grunwald13-Oct-05 9:47
Daniel Grunwald13-Oct-05 9:47 
GeneralRe: RichTextBox save as HTML Pin
jeweladdict13-Oct-05 12:09
jeweladdict13-Oct-05 12:09 
GeneralRe: RichTextBox save as HTML Pin
Judah Gabriel Himango13-Oct-05 15:28
sponsorJudah Gabriel Himango13-Oct-05 15:28 
QuestionEnableViewState question Pin
kani9813-Oct-05 8:27
kani9813-Oct-05 8:27 
AnswerRe: EnableViewState question Pin
Guffa13-Oct-05 9:01
Guffa13-Oct-05 9:01 
QuestionAccessing objects in resource files Pin
Rendili13-Oct-05 8:01
Rendili13-Oct-05 8:01 
AnswerRe: Accessing objects in resource files Pin
turbochimp13-Oct-05 20:06
turbochimp13-Oct-05 20:06 
GeneralRe: Accessing objects in resource files Pin
Rendili14-Oct-05 5:49
Rendili14-Oct-05 5:49 
GeneralRe: Accessing objects in resource files Pin
turbochimp14-Oct-05 7:34
turbochimp14-Oct-05 7:34 
GeneralRe: Accessing objects in resource files Pin
Rendili14-Oct-05 7:47
Rendili14-Oct-05 7:47 
GeneralRe: Accessing objects in resource files Pin
turbochimp14-Oct-05 7:57
turbochimp14-Oct-05 7:57 
GeneralRe: Accessing objects in resource files Pin
Rendili14-Oct-05 8:17
Rendili14-Oct-05 8:17 
QuestionTextBox. How to Validate for Number?? Pin
...---...13-Oct-05 6:46
...---...13-Oct-05 6:46 
AnswerRe: TextBox. How to Validate for Number?? Pin
Dan Neely13-Oct-05 6:52
Dan Neely13-Oct-05 6:52 
GeneralRe: TextBox. How to Validate for Number?? Pin
...---...13-Oct-05 8:03
...---...13-Oct-05 8:03 
GeneralRe: TextBox. How to Validate for Number?? Pin
Dan Neely13-Oct-05 9:27
Dan Neely13-Oct-05 9:27 
GeneralRe: TextBox. How to Validate for Number?? Pin
Anonymous13-Oct-05 13:02
Anonymous13-Oct-05 13:02 
AnswerRe: TextBox. How to Validate for Number?? Pin
Anonymous13-Oct-05 16:21
Anonymous13-Oct-05 16:21 
AnswerRe: TextBox. How to Validate for Number?? Pin
nps_ltv13-Oct-05 17:22
nps_ltv13-Oct-05 17:22 
AnswerRe: TextBox. How to Validate for Number?? Pin
Luis Alonso Ramos13-Oct-05 17:29
Luis Alonso Ramos13-Oct-05 17:29 
Handle the Validating event for both text boxes:
private void TextBox_Validating(object sender, CancelEventArgs e)
{
    TextBox textBox = sender as TextBox;
    if(textBox == null)  // Ensure this is indeed a TextBox
        return;
 
    // Allow the user to leave the text box without typing anything
    if(textBox.TextLength == 0)
        return;
 
    bool error = false;
 
    try
    {
        int n = Convert.ToInt32(textBox);
        if(n < 0 || n > 100)  // Check range if necessary
            error = true;
    }
    catch(FormatException)
    {
        // Could not parse the number
        error = true;
    }
    finally
    {
        if(error)
        {
            MessageBox.Show("Please enter a valid number");
 
            textBox.SelectAll();
            textBox.Focus();
 
            e.Cancel = true;  // Don't allow focus to leave the control
        }
    }
}
When the user tries to take focus away from the control, its contents will be validated. If invalid, the user won't be allowed to leave the control unless he corrects it or leaves it empty.

Then in you button handler, just validate that the text boxes have anything. If they do, it's surely a valid number.

I hope this helps!

-- LuisR



Luis Alonso Ramos
Intelectix - Chihuahua, Mexico

Not much here: My CP Blog!


The amount of sleep the average person needs is five more minutes. -- Vikram A Punathambekar, Aug. 11, 2005

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.