Windows Develop Bookmark and Share   
 index > Windows Forms General > Stuck up with textbox validations
 

Stuck up with textbox validations

Hi All,

I am using VS03 + c# + Winforms.

In my application i have a textbox in which user can enter +ve value as 100 and-ve value alos eg.-100.

My requirements are 1) I want to dovalidate textboxsuch that user can enter -ve sign only as a first character.

If user enter value such as -1-0 then message should given,please enter valid value.

2) How i find the value entered by user is having -ve sign means user entered a -ve value.

The code in c# is appriciated.

Thanks in advance.

sunDAC  Wednesday, July 23, 2008 5:33 AM

Ok, you can do this inthe TextBox'sTextChanged event handler (rename idTextBox as required):

private void idTextBox_TextChanged(object sender, EventArgs e)

{

// If we don't yet have a valid number, do nothing

if (idTextBox.TextLength== 0|| idTextBox.Text == "-")

{

return;

}

// Convert text to a number

int number = int.Parse(idTextBox.Text);

// Take action based on whether number is negative,positiveor zero

if (number < 0)

{

// Number is negative, e.g. -100

}

else if (number> 0)

{

// Number is positive, e.g. 100

}

else

{

// Number is zero

}

}

Daniel B·  Thursday, July 24, 2008 3:41 PM

Agood way to achieve this is to prevent the user from entering invalid characters with a TextBox.KeyPress event handler (avoids need for error messages):

private void idTextBox_KeyPress(object sender, KeyPressEventArgs e)

{

TextBox textBox = (TextBox)sender;

// If minus key, validate usage

if (e.KeyChar == '-')

{

// If not at start of textbox or duplicate would result, ignore key

if (textBox.SelectionStart != 0 || (textBox.Text.Length > 0 && textBox.Text[0] == '-' &&

(textBox.SelectionStart > 0 || textBox.SelectionLength == 0)))

{

e.Handled = true;

}

}

// Otherwise, if not Backspace or number key, ignore

else if ((e.KeyChar != (char)Keys.Back && !char.IsDigit(e.KeyChar)))

{

e.Handled = true;

}

}

You'll need to add the blank event handler for the TextBox in the Designer and insert the above code.

Daniel B·  Wednesday, July 23, 2008 6:38 AM

Hi ,

Thanks for the reply. I have gone through the code.The validation really works fine.That is solution of my first question.

Now i like to knowhow can i find out the user entered value in the textbox is -ve ie.-100 or it is +ve ie.100

Bcoz depending on the condition my calculations are change.I am not sure can i use firstindex of string ie.textbox value.

I want some function through which i know the entered value is +ve or -ve.

How i can achieve this? Please let know if you dont understand what i want to do.

Thanks for the validation.

Waiting for the reply.

sunDAC  Wednesday, July 23, 2008 9:41 AM

Actually in addition to the KeyPress event handler you'll first want to ensure the user has entered a valid number.

If they just type in a '-' or leave the TextBox empty, this is clearly of no use.

I'd recommend the following approach, assuming your TextBox is on a Form withOK and Cancel buttons:

  1. In Designer, set AcceptButton for Form to your ok button and CancelButton to your cancel button. Then, if user presses Enter, ok button is clicked and if Escape is pressed, Cancel button is clicked.

  2. Setok button's Enabled property to False.

  3. Add a TextChanged event handler for the TextBox, with the following code (rename idTextBox as required):

    private void idTextBox_TextChanged(object sender, EventArgs e)
    {
    okButton.Enabled = (idTextBox.TextLength > 0 && idTextBox.Text != "-");
    }

Now, when the user is able to click the ok button, they will have entered a valid number and you could do something in the Click event handler.

For example, you could test for a negative number as follows:

private void okButton_Click(object sender, EventArgs e)

{

int number = int.Parse(idTextBox.Text);

if (number < 0)

{

// Negative number entered

}

else

{

// Number is zero or positive

}

}

Daniel B·  Wednesday, July 23, 2008 4:05 PM

Hi,

Thanks for the reply.I have gone through all your suggestion.It is really a good method.But in my application i want

to do all the calculations as soon as text changes.If i have -100 or 100 acoording to that my calculations are changed.

So let me know weheather i can find when user enter value in textbox at that moment wheather it is -ve or +ve.

I am surching for function which really finds the starting value is "-ve" .

How can i find it?

Waiting for the reply.

sunDAC  Thursday, July 24, 2008 4:36 AM

I am not familiar with C# but I think my visual basic 6.0 programming logic may be helpful to you�Please change my code as per C# syntax grammar.

1) one textbox place on form and wrote code in KeyPress and KeyUp event

Private Sub txtNumber_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 43 'For +
If InStr(1, txtNumber, "-", vbTextCompare) = 1 Then
txtNumber = "+" & Mid(txtNumber, 2)
txtNumber.SelStart = Len(txtNumber)
txtNumber.SelLength = 0
KeyAscii = 0
ElseIf InStr(1, txtNumber, "+", vbTextCompare) = 0 Then
txtNumber = "+" & txtNumber
txtNumber.SelStart = Len(txtNumber)
txtNumber.SelLength = 0
KeyAscii = 0
Else
KeyAscii = 0
End If
Case 45 'for -
If InStr(1, txtNumber, "+", vbTextCompare) = 1 Then
txtNumber = "-" & Mid(txtNumber, 2)
txtNumber.SelStart = Len(txtNumber)
txtNumber.SelLength = 0
KeyAscii = 0
ElseIf InStr(1, txtNumber, "-", vbTextCompare) = 0 Then
txtNumber = "-" & txtNumber
txtNumber.SelStart = Len(txtNumber)
txtNumber.SelLength = 0
KeyAscii = 0
Else
KeyAscii = 0
End If
Case vbKeyBack, vbKey0 To vbKey9

Case Else
KeyAscii = 0
End Select
End Sub
Private Sub txtNumber_KeyUp(KeyCode As Integer, Shift As Integer)
If Trim(txtNumber) = "" Or KeyCode = 109 Or KeyCode = 107 Then Exit Sub
If txtNumber.Text > 100 Or txtNumber.Text < -100 Then
MsgBox "Please enter proper value.", vbInformation + vbOKOnly
txtNumber.Text = Left(txtNumber.Text, Len(txtNumber.Text) - 1)
txtNumber.SelStart = Len(txtNumber)
txtNumber.SelLength = 0
End If
End Sub
Hearty81  Thursday, July 24, 2008 6:00 AM

Ok, you can do this inthe TextBox'sTextChanged event handler (rename idTextBox as required):

private void idTextBox_TextChanged(object sender, EventArgs e)

{

// If we don't yet have a valid number, do nothing

if (idTextBox.TextLength== 0|| idTextBox.Text == "-")

{

return;

}

// Convert text to a number

int number = int.Parse(idTextBox.Text);

// Take action based on whether number is negative,positiveor zero

if (number < 0)

{

// Number is negative, e.g. -100

}

else if (number> 0)

{

// Number is positive, e.g. 100

}

else

{

// Number is zero

}

}

Daniel B·  Thursday, July 24, 2008 3:41 PM

Hi,

Thanks for your patient reply.The code really works as a magic for me.Thanks!

Cheers!

sunDAC  Friday, July 25, 2008 4:20 AM

Hi


A warm welcome always


May this is helpful? Then please mark as answerer.

Hearty81  Friday, July 25, 2008 5:29 AM

You can use google to search for other answers

Custom Search

More Threads

• Calculate Width of a String in Pixels
• UnauthorizedAccessException in Windows Vista
• ListView SelectedIndexChanged
• MCTS Moc2547 Application Settings question
• Checkbox Validate Event
• How can I add lines in a richTextBox in C# ????
• How to make a usercontrol to act as an container?
• Drag and Drop - Object in my location ??
• Visual studio 2005 Team System
• Proper way to invalidate using a Region