Click here to Skip to main content
15,920,801 members
Home / Discussions / C#
   

C#

 
AnswerRe: [newbie] function Pin
Karmendra Suthar28-Jan-09 3:54
Karmendra Suthar28-Jan-09 3:54 
GeneralRe: [newbie] function Pin
jon-8028-Jan-09 5:59
professionaljon-8028-Jan-09 5:59 
AnswerRe: [newbie] function to return MAX + 1 from primary key Pin
Wendelius28-Jan-09 4:33
mentorWendelius28-Jan-09 4:33 
AnswerRe: [newbie] function to return MAX + 1 from primary key Pin
riced28-Jan-09 5:10
riced28-Jan-09 5:10 
QuestionRatio Textbox Validation Pin
honeyashu28-Jan-09 3:37
honeyashu28-Jan-09 3:37 
AnswerRe: Ratio Textbox Validation [modified] Pin
DaveyM6928-Jan-09 3:56
professionalDaveyM6928-Jan-09 3:56 
GeneralRe: Ratio Textbox Validation Pin
honeyashu28-Jan-09 20:31
honeyashu28-Jan-09 20:31 
GeneralRe: Ratio Textbox Validation Pin
DaveyM6929-Jan-09 0:11
professionalDaveyM6929-Jan-09 0:11 
It depends on how you want to handle it.

The code below uses the Validating[^] event, but that may not be appropriate for your situation. I prefer to stop invalid characters getting into the textbox in the first place. Check out my article - Simple Numeric TextBox[^].
Assumes textBox1, textBox2 and label1 already exist on the form.
private static readonly int MinValue = 0;
private static readonly int MaxValue = 100;
int textBox1Value;
int textBox2Value;
bool textBox1Valid;
bool textBox2Valid;
private void textBox1_Validating(object sender, CancelEventArgs e)
{
    if (!ValidateTextBoxes() && !textBox1Valid)
    {
        e.Cancel = true;
        textBox1.SelectAll();
    }
}
private void textBox2_Validating(object sender, CancelEventArgs e)
{
    if (!ValidateTextBoxes() && !textBox2Valid)
    {
        e.Cancel = true;
        textBox2.SelectAll();
    }
}
private bool ValidateTextBoxes()
{
    textBox1Valid = int.TryParse(textBox1.Text, out textBox1Value);
    textBox2Valid = int.TryParse(textBox2.Text, out textBox2Value);
    if (textBox1Valid)
        textBox1Valid = textBox1Value >= MinValue && textBox1Value <= MaxValue;
    if (textBox2Valid)
        textBox2Valid = textBox2Value >= MinValue && textBox2Value <= MaxValue;
    SetResultLabel(textBox1Valid && textBox2Valid);
    return textBox1Valid && textBox2Valid;
}
private void SetResultLabel(bool validated)
{
    if (validated)
        label1.Text = string.Format("{0}:{1}", textBox1Value, textBox2Value);
    else
        label1.Text = string.Empty;
}


Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

QuestionJPEG Compression in C# Pin
SimpleData28-Jan-09 3:26
SimpleData28-Jan-09 3:26 
AnswerRe: JPEG Compression in C# Pin
User 665828-Jan-09 3:30
User 665828-Jan-09 3:30 
AnswerRe: JPEG Compression in C# Pin
SimpleData28-Jan-09 3:39
SimpleData28-Jan-09 3:39 
AnswerRe: JPEG Compression in C# Pin
Ennis Ray Lynch, Jr.28-Jan-09 5:26
Ennis Ray Lynch, Jr.28-Jan-09 5:26 
GeneralRe: JPEG Compression in C# Pin
SimpleData28-Jan-09 5:29
SimpleData28-Jan-09 5:29 
GeneralRe: JPEG Compression in C# Pin
Ennis Ray Lynch, Jr.28-Jan-09 5:42
Ennis Ray Lynch, Jr.28-Jan-09 5:42 
GeneralRe: JPEG Compression in C# Pin
SimpleData28-Jan-09 6:02
SimpleData28-Jan-09 6:02 
GeneralRe: JPEG Compression in C# Pin
Mark Churchill28-Jan-09 12:11
Mark Churchill28-Jan-09 12:11 
QuestionC# Clipboard - Works, but is one step behind Pin
Olav Alexander Mjelde28-Jan-09 3:22
Olav Alexander Mjelde28-Jan-09 3:22 
AnswerRe: C# Clipboard - Works, but is one step behind Pin
User 665828-Jan-09 3:25
User 665828-Jan-09 3:25 
QuestionAny way to convert/compress JPG to TIFF CCITT Fax4? Pin
Matjaz-xyz28-Jan-09 3:19
Matjaz-xyz28-Jan-09 3:19 
AnswerRe: Any way to convert/compress JPG to TIFF CCITT Fax4? Pin
Mark Churchill28-Jan-09 12:40
Mark Churchill28-Jan-09 12:40 
GeneralRe: Any way to convert/compress JPG to TIFF CCITT Fax4? Pin
Matjaz-xyz28-Jan-09 22:30
Matjaz-xyz28-Jan-09 22:30 
GeneralRe: Any way to convert/compress JPG to TIFF CCITT Fax4? Pin
Mark Churchill29-Jan-09 3:40
Mark Churchill29-Jan-09 3:40 
QuestionDevice Drivers Pin
queries36528-Jan-09 3:15
queries36528-Jan-09 3:15 
QuestionReading xml that contains reference to xml’s Pin
Ronenb28-Jan-09 3:08
Ronenb28-Jan-09 3:08 
QuestionStruct vs array performance Pin
Lutosław28-Jan-09 2:25
Lutosław28-Jan-09 2:25 

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.