How to validate textboxes in C#.net winforms - winforms

I have a form where I need to validate the textboxes like Firstname, Middlename, Lastname, emailId, date, mobilenumber. The validation should happen when a user starts typing in the textbox. errorprovider should show message under textbox if a user enters numbers in place of text and text in place of numbers. I got to know about implicit validation and explicit validation but I feel better to use implicit validation only because its on time error provider when user looses focus on text box or if he shifts to another textbox. I've posed this kind of question with a explicit validation code but no one responded me. So Im making it simple to get help. Do not think I havent done enough research before posting this question.

If you have a very specific validation to do, Marc's answer is correct. However, if you only ensure the "enter number instead of letters" or "enter letters instead of numbers" thing, a MaskedTextBox would do the job better than you (user wouldn't be able to answer incorrect data, and you can still warn them by handling the MaskInputRejected event)
http://msdn.microsoft.com/en-us/library/kkx4h3az(v=vs.100).aspx

You should take a look to the TextChanged Event in of your textbox.
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.textchanged.aspx
This event is raised if the Text property is changed by either a
programmatic modification or user interaction.
I would do something like this
private void TextBoxExample_TextChanged(object sender, EventArgs e)
{
TextBox box = sender as TextBox;
if (box.Text.Contains("Example"))
{
LabelError.Text = "Error";
}
else
{
LabelError.Text = string.Empty;
}
}
Hope it helps :)

You can also use keyPressEvent to Avoid the entering the numerical values in the textboxes
it will not allow the numerical chars in the text box
private void textboxName_KeyPress(object sender, KeyPressEventArgs e)
{
//not allowing the non character values
if (!char.IsLetter(e.KeyChar) && !char.IsControl(e.KeyChar) && !(e.KeyChar == (char)Keys.Back) && !(e.KeyChar == (char)Keys.Left) && !(e.KeyChar == (char)Keys.Right) && !(e.KeyChar == (char)Keys.Space) && !char.IsPunctuation(e.KeyChar))
{
e.Handled = true;
}
}

Related

How can I get more than one character to show in a messagebox from a textbox user input?

I have made a textbox and I want the user to type in a string of numbers and hit enter. I have setup the following:
private void textBox1_TextChanged(object sender, EventArgs e)
{
String UserBarcode;
Focus();
UserBarcode = Console.ReadLine();
MessageBox.Show(UserBarcode);
}
When I enter any key into the textbox, I get a message box with nothing in it. I want to have the program wait til it hears the enter key then display the contents of the textbox.
The Textbox.TextChanged event fires as soon as the text in the textbox is changed at all. If you want a message box with the full string, you probably want to consider using the Textbox.LostFocus event or a button's Click event.
So you could have something like (I'm taking a stab at this here, as I've used VB rather than C#)
private void textBox1_LostFocus(object sender, EventArgs e)
{
MessageBox.Show(sender.Text)
}
If you're using a button, the above function should work, but you'll want to substitute textBox1.Text for sender.Text.
Take a look at Focus and Validation Events
There are several events that you can handle, depending on your goals and how your application is designed. If you want to perform validation and/or are using data binding, you may want to go with handling the validating/validated events. By default data bindings update a bound property after OnValidating. If you use LostFocus and read the value from a bound object, instead of your control, you will get inconsistent results.
I was able to figure it out finally. For some reason when I manually entered the code I kept getting multiple random errors. I started a new Visual C # Windows Forms Application, Made a textbox, chose the keydown property and double clicked on it to have the program inject the code for the keydown function and then I filled in the if statement pointing to the enter key. The final code looks like this:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show(textBox1.Text);
}
}

Using event in WPF

I have two textboxes in WPF. named txt1 and txt2.
In the lostFocus of txt1 I write
If txt1.Text is nothing then
txt1.Focus
End If
In the lostFocus event of txt2 I write
If txt2.Text is nothing then
txt2.Focus
End If
Now, If txt1 and txt2 are both empty and user presses TAB key in txt1 the problem occurs. Program goes in infinite loop. I mean cursor comes to txt1 and goes to txt2 infinite times.I know This is normal behavior according to my code.
So I want to have validating event to avoid the problems like above. But I cannot find one in WPF.
So which event should I use?
I am not a VB coder so can't write exact code for you but here is what you should do. Add event handler for event PreviewLostKeyboardFocus. inside the event handler set e.Handled to true if the text is empty.
Sample C# code. I have writter a generic handler.
private void TextBox_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
if (string.IsNullOrEmpty((sender as TextBox).Text))
{
e.Handled = true;
}
}
A better solution might be to allow the user to navigate away from the empty/null text box but either revert the text to the initial value (if there was one) or provide a validation error. Providing validation errors is relatively easy with IDataErrorInfo.
As a software user myself I get annoyed when an application prevents me from navigating away from a field.
Reset Value Approach
See this stackoverflow appraoch for how to maintain and get the previous value. In the LostFocus event you can set your member variable back to _oldValue if the current value is invalid.
determine a textbox's previous value in its lost focused event? WPF
Validation Approach
Those two dates are stored in a model or a view model class. In that class implement IDataErrorInfo (http://msdn.microsoft.com/en-us/library/system.componentmodel.idataerrorinfo(v=vs.95).aspx). Then in your xaml you can show the validation errors.
//This is your model/viewmodel validation logic
public string this[string columnName]
{
get
{
string result = null;
if (columnName == "FirstName")
{
if (string.IsNullOrEmpty(FirstName))
result = "Please enter a First Name";
}
if (columnName == "LastName")
{
if (string.IsNullOrEmpty(LastName))
result = "Please enter a Last Name";
}
if (columnName == "Age")
{
if (Age < = 0 || Age >= 99)
result = "Please enter a valid age";
}
return result;
}
}
//Here is a sample of a xaml text block
<textbox x:Name="tbFirstName" Grid.Row="0" Grid.Column="1" Validation.Error="Validation_Error" Text="{Binding UpdateSourceTrigger=LostFocus, Path=FirstName, ValidatesOnDataErrors=true, NotifyOnValidationError=true}" />
You can also look at these other StackOverflow posts-
What is IDataErrorInfo and how does it work with WPF?
IDataErrorInfo notification

WPF datagrid validation inconsistency

I've got a DataGrid that I'm binding to an ObservableCollection of "Customer" classes, implementing IDataErrorInfo. One of the properties on the Customer class is an int, and in my IDataErrorInfo implementation I check that it's within a valid range, e.g.:-
public class Customer : IDataErrorInfo
{
public int PercentDiscount { get; set; }
... other properties & methods removed for clarity
public string this[columnName]
{
get
{
if (PercentDiscount < 0 || PercentDiscount > 10)
return "Percent Discount is invalid";
}
}
}
In my XAML code-behind I handle a couple of events. In the PreparingCellForEdit event I store a reference to the row being edited:-
private void DataGrid_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
{
_rowBeingEdited = e.Row;
}
Then in the RowEditEnding event, I take some action if the row is in an invalid state (in my case I revert the Customer properties back to their previous "good" values):-
private void DataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
if (_rowBeingEdited != null)
{
var errors = Validation.GetErrors(_rowBeingEdited);
if (errors.Count > 0)
{
.. do something
}
}
}
This works fine if the user enters a numeric value that fails my validation rule, but if the user enters a non-numeric value then the RowEditEnding event never fires and the cell remains in an edit state. I assume it's because WPF fails to bind the non-numeric value to the int property. Is there any way I can detect/handle when this happens?
Last resort is to change the PercentDiscount property to a string, but I'm trying to avoid going down this route.
Edit - I've just found that I can successfully handle both types of error using the CellEditEnding event instead of RowEditEnding. A new problem has appeared though - if I enter an invalid value into the cell then press Enter, the underlying property doesn't get updated, so when CellEditEnding fires Validation.GetErrors is empty. The end result is that the row leaves edit mode but still shows the invalid value in the cell with red border. Any idea what's going on now?
This may not be much of an answer especially since you already mentioned it, but I've fought with DataGrid validation for a while and ended up resorting to making my backing values be strings. You'll notice in the output window of the debugger that a binding or conversion exception happens when you type an alpha character into a DataGridColumn bound to an int.
You can get different behavior by changing the UpdateSourceTrigger, or by putting a converter in between the binding and the property, but I never got exactly what I needed until I backed the values with strings.
I suppose you could also try creating your own DataGridNumericColumn derived from DataGridTextColumn and maybe you'd have more control over the binding/validation behavior.
I struggled to find a good solution for this, but I saw some other people messing with the CellEditEnding event and I ended up, coming up with this code to revert values if they fail conversion:
private void CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
if (e.EditingElement is TextBox)
{
var cellTextBox = (TextBox)e.EditingElement;
var cellTextBoxBinding = cellTextBox.GetBindingExpression(TextBox.TextProperty);
if (cellTextBoxBinding != null && !cellTextBoxBinding.ValidateWithoutUpdate())
{
cellTextBoxBinding.UpdateTarget();
}
}
}
Calling ValidateWithoutUpdate on the editing elements binding returns false if the conversion of the value fails, then calling the UpdateTarget forces the value to be reverted to the current 'model' value.

TextBox Password Char

the textbox in Windows Forms used to have a PasswordChar property. In WPF there is an extra control for that: PasswordBox. This wouldn't be a problem but my application runs on an touchscreen only device. Unfortunately the password box does not support the on screen keyboard. I was wondering if there is a way of adding the password char feature to the standard textbox.
This answer may provide you with what you need.
I made my way around this particular problem by creating two Properties for the Password content and binding both of them to the same Model value. One of them (the visible UI Element) binds to Password. The Get on this property of course then returns an array of characters for display. The functions that must use the password text can use the PlainPassword Property.
Adding "UpdateSourceTrigger=PropertyChanged" to the Binding for the textbox causes the characters to appear in the text box as they are typed.
public string Password
{
set
{
Model.Password = value;
OnPropertyChanged("Password");
}
get
{
return new String('●', Model.Password.Length);
}
}
public string PlainPassword
{
set
{
Model.Password = value;
OnPropertyChanged("Password");
}
get { return Model.Password; }
}
I believe the only way you can achieve this is to create your own control based on textbox. Then just bind the actual text property to a property that returns your password character rather than the actual password. Then you can pull the password as a dependency property (though I've heard this is rather insecure, which is why it is not a dependency property in the password box), or just a regular property and access it by passing the whole textbox object.
A simple way to obfuscate the password in a TextBox is to use the Webdings font.
txtInput.FontFamily = new FontFamily("Webdings");
This is not completely safe, but sufficient in most cases. Note that Webdings works better than Wingdings, because Wingdings does not cover the lower case letters and returns everything in upper case.
helló!
im new here but maybe i can help u. i find this -> can be work whit WPF and passwordbox
private void delete_Click(object sender, RoutedEventArgs e)
{
if (pass_passbox.IsFocused == true)
{
pass_passbox.Password= "";
}
}
ofc u do this pass_passbox.Text if its textbox but when change WPF passwordbox u need to write can pass_passbox.Password and u can do changes from screen keyboard .
not fully tested but u can reset this way
and u can do select like this:
string Query = "Select * from [employeeinfo] where username='" + this.txt_user_name.Text + "' and password='" + this.pass_passbox.Password + "' ";
u can see this.pass_passbox.Password is the same at textbox this.pass_passbox.Text
private void txtBoxPassword_TextChanged(object sender, EventArgs e)
{
//password view protection//
txtBoxPassword.UseSystemPasswordChar = true;
}
That's a way to enable the DEFAULT character used for hiding the password from the system,if you wish to set your own password char just substitute the actual line inside the event function with the following:
txtBoxPassword.PasswordChar='*'; //or any other character

WPF Textbox use default text

I have a TextBox, which I want my users to enter a time value in the format XX:XX:XX. I already have validation in place to make sure they enter it in this format. However, now I'd like to have the colons there automatically. I'd like them to be in the textbox, and as the user types numbers, they just skip over the colons. Is it possible to have some kind of format decorator for the TextBox?
EDIT: I am using WPF 4.
you can use a masked textbox from the wpf toolkit
http://wpftoolkit.codeplex.com/wikipage?title=MaskedTextBox&referringTitle=Home
If you want to stick to vanilla WPF you could create a Custom Control and add 3 textboxes.
Put colons in between them and handle the keydown events to pass focus from one textbox to the other and at the same time accepting numbers only.
Again: using the toolkit might be less work.
Using three TextBoxes as Erno suggested is probably a better solution but you could also use the TextChanged event to add colons to the text (which might confuse the user), here'd be the code that would insert them after the second and fifth character:
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox tb = sender as TextBox;
if (e.Changes.Count == 1)
{
if (e.Changes.ElementAt(0).AddedLength == 1 && (tb.Text.Length == 2 || tb.Text.Length == 5))
{
tb.Text += ":";
tb.SelectionStart = tb.Text.Length;
}
}
}

Resources