I have the following code:
private void myTextBox_Validating(object sender, CancelEventArgs e)
{
if (some condition)
{
e.Cancel = true;
errorProvider.SetError(myTextBox, "Error occurred");
return;
}
this.errorProvider.SetError(this.myTextBox, null);
}
And the following Button_click code to trigger all Validating events:
private void saveData_Click(object sender, EventArgs e)
{
if (!this.ValidateChildren())
{
return;
}
}
It did validate correctly. However, I can't seem to click anywhere besides myTextBox when I click that texbox without changing its invalid value. Can I disable that "locking to your error until you change it" feature?
You can control the behaviour of your controls, whether they are allowed to lose focus while having invalid data or not.
On your form you can set the following property:
this.AutoValidate = AutoValidate.EnableAllowFocusChange;
For details and other ways of getting a similar effect, please have a look at the docs
Related
After spending 90 minutes searching for a solution to this simple problem I have to post a question in shame.
I'm working on a WPF project where the user inputs text. I want to check the inputs while the user is typing, display a tool tip and ideally block characters that are not allowed. Basically it's this thread:
How do I validate characters a user types into a WinForms textbox? or this
Is there a best practice way to validate user input?
private void NameTextbox_KeyDown(object sender, KeyEventArgs e)
{
e.???
}
I created this code behind by double clicking in the KeyDown-Property Field in the designer (just mentioning this if I messed up there).
Screenshot of the Property Window
I can not access the e.SupressKeyPress Property. Why?
As of the Properties offered by VS I think that e is of the wrong Type or in the wrong context here.
Intellisense Screenshot
Edit1
private void NameTextbox_KeyDown(object sender, KeyEventArgs e)
{
var strKey = new KeyConverter().ConvertToString(e.Key);
if (!strKey.All(Char.IsLetter))
{
MessageBox.Show("Wrong input");
e.Handled = true;
}
}
Thanks to #rokkerboci I was able to build something that kind of works.
Yet I think it is overly complex. So improvements are still welcome :)
New Error When Creating a Message Box the application hangs without an exception thrown.
You are using WPF, which does not include the WindowsForms specific SupressKeyPress property.
You can do this in WPF by using the KeyDown event, and setting the KeyEventArgs.Handled property to true (it tells the handler, that it doesn't have to do anything with this event.)
private void NameTextbox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Delete)
{
MessageBox.Show("delete pressed");
e.Handled = true;
}
}
EDIT:
I have found a perfect answer to your question:
C#:
char[] invalid = new char[] { 'a', 'b' };
private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
foreach (var item in invalid)
{
if (e.Text.Contains(item))
{
e.Handled = true;
return;
}
}
}
private void TextBox_Pasting(object sender, DataObjectPastingEventArgs e)
{
var text = e.DataObject.GetData(typeof(string)).ToString();
foreach (var item in invalid)
{
if (text.Contains(item))
{
e.CancelCommand();
return;
}
}
}
XAML:
<TextBox PreviewTextInput="TextBox_PreviewTextInput" DataObject.Pasting="TextBox_Pasting" />
Im working with errorprovider in a c# winforms application.
Now I want to have a "double" validation. Once on the textfields directly, so the user sees that he has made some errors, and once on the button itself. So when there are still errors, the "save" button will keep greyed out or "disabled".
Because I don't want to block my user when he is making an error, and I want him to be able to make the changes whenever he wants im using the event "leave" or on focus lost. This because otherwise I noticed you cannot go to another field, until you changed your error.
So, now the code:
private void txtFirstname_Leave(object sender, EventArgs e)
{
if (!InputChecks.IsFilledIn(txtFirstname.Text))
{
errorProvider1.SetError(txtFirstname, "Firstname needs to be filled in!");
isValidated = false;
}
else
{
errorProvider1.SetError(txtFirstname, "");
isValidated = true;
}
}
So far, so good. The error provider works correctly and my user can edit whenever he wants.
public void setSaveButton()
{
if (isValidated == true)
{
btnSave.Enabled = true;
}
else
{
btnSave.Enabled = false;
}
}
bool isValidated;
private void btnSave_Click(object sender, EventArgs e)
{
if (isValidated == true)
{
employeePresenter.addEmployee(txtFirstname.Text, txtLastname.Text, txtUsername.Text, txtPassword.Text);
}
}
This was still okey in my head. BUT, as I give the ability to the user to change the issues whenever they want, this doesn't work. I tried to put the method "setSaveButton()" under "isvalidated" but this is not working either. Because of the focus lost.
Anyone has a better idea for this? I have been looking on google and the only things i found was a single validation with the errorprovider, or the event validating. But these events don't allow users to edit their errors whenever they want. It blocks them into one particular text field.
You don't need to make the save button disabled. It's enough to check ValidateChildren method of your form and if it returned false, it means there is some validation error. To use this approach you should remember to set e.Cancel = true in Validating event of the control when you set an error for control.
Also to let the user to move between controls even if there is an error, set AutoValidate property of your Form to EnableAllowFocusChange in designer or using code:
this.AutoValidate = System.Windows.Forms.AutoValidate.EnableAllowFocusChange;
Code for Validation:
private void txtFirstname_Validating(object sender, CancelEventArgs e)
{
if (string.IsNullOrEmpty(this.txtFirstname.Text))
{
this.errorProvider1.SetError(this.txtFirstname, "Some Error");
e.Cancel = true;
}
else
{
this.errorProvider1.SetError(this.txtFirstname, null);
}
}
private void btnSave_Click(object sender, EventArgs e)
{
if (this.ValidateChildren())
{
//Here the form is in a valid state
//Do what you need when the form is valid
}
else
{
//Show error summary
}
}
I'm trying to set the "txtMiles" textbox to focus after:
The form opens
When the "clear" button is clicked
I have tried using txtMiles.Focus(); but it doesn't seem to work for me.
CODE BEING USED ON THIS FORM
private void btnConvert_Click(object sender, EventArgs e)
{
//assigns variable in memory.
double txtMile = 0;
double Results;
try
{
// here is where the math happens.
txtMile = double.Parse(txtMiles.Text);
Results = txtMile * CONVERSION;
lblResults.Text = Results.ToString("n2");
txtMiles.Focus();
}
// if the user enters an incorrect value this test will alert them of such.
catch
{
//MessageBox.Show (ex.ToString());
MessageBox.Show("You entered an incorrect value");
txtMiles.Focus();
}
}
private void btnClear_Click(object sender, EventArgs e)
{
//This empties all the fields on the form.
txtMiles.Text = "";
txtMiles.Focus();
lblResults.Text = "";
}
private void btnExit_Click(object sender, EventArgs e)
{
// closes program
this.Close();
}
}
}
Thanks in advance for the help.
You should make sure your TabIndex is set and then instead of Focus(), try use Select(). See this MSDN link.
txtMiles.Select();
Also make sure there isn't a TabStop = true attribute set in the view file.
It's old, but someone could need this.
Control.Focus() is bugged. If it's not working, try workaround:
this.SelectNextControl(_controlname, true, true, true, true);
Change function parameters so they will work with your control, and remember about TabStop = true property of your control.
You already have your txtMiles focused after the clear-button click. As for the Startup, set txtMiles.Focus() in your load-method.
private void MilesToKilometers_Load(object sender, EventArgs e)
{
txtMiles.Focus();
}
using this solution worked perfectly...
txtMiles.Select();
I have two text boxes in windows form.
Also one disabled button.
How I can do validation text box:
if field is empty then disable button
if value inside field is less then 5 then disable button
other case - enable button
I tried this on event TextChange, but when I tried to enter value 43 I get notification, because event TextChange works after each typing symbols.
Code:
private void textBox2_TextChanged(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(textBox2.Text))
{
button6.Enabled = true;
}
}
If you don't want to validate each time a key is pressed but would rather validate when the user leaves the field, instead of hooking into the TextChanged event, hook into the Leave event.
private void textBox2_Leave(object sender, EventArgs e)
{
button6.Enabled = !(string.IsNullOrEmpty(textBox2.Text)) && textBox2.Text.Length >= 5;
if (!button6.Enabled)
{
textBox2.Focus();
}
}
Update your event handle like this :
private void textBox2_TextChanged(object sender, EventArgs e)
{
button6.Enabled =
!String.IsNullOrEmpty(textBox2.Text) && textBox2.Text.Length > 5
}
As for disabling the button on start up, you just set button6 to be disabled by default.
Or, invoke your validation in your constructor :
textBox2_TextChanged(null, null);
Neither TextChanged nor Leave events are appropriate for this. The proper event is called (surprise:-) Validating. You need to set e.Cancel = true if validation is wrong. More info: https://msdn.microsoft.com/en-us/library/system.windows.forms.control.validating(v=vs.110).aspx
To make sure that the user name input is valid, I added such callback method to do the verification:
Regex UserNameRE = new Regex(#"^[a-zA-Z]\w*$");
//being called when input box is not focused any more
private void UserNameInput_Leave(object sender, EventArgs e)
{
//pop up a warning when user name input is invalid
if (!UserNameRE.IsMatch(UserNameInput.Text))
{
MessageBox.Show("Invalid User Name!");
this.UserNameInput.Text = "";
this.UserNameInput.Focus();
}
}
The method will be called when user finished their inputting(the method is bounded with the event-"leaving the input box"). It works when user left a invalid User_Name and begin to enter a password.
But it also works when user click another tab, e.g. the Register tab. I don't want this happen. Because the user obviously don't wanna login anymore if he clicks "Register" tab, and my C# app shouldnot pop up a warning box and force them inputting a valid user name again.
How can the C# tell the difference of such 2 situations? It should be easy if I know which object is being clicked.
You will have source of event in object sender in UserNameInput_Leave event.
private void UserNameInput_Leave(object sender, EventArgs e)
{
//sender is source of event here
}
Here's an option:
private void UserNameInput_Leave(object sender, EventArgs e)
{
if (sender.GetType() != typeof(TextBox))
{
return;
}
TextBox tBox = (TextBox)sender;
//pop up a warning when user name input is invalid
if (!UserNameRE.IsMatch(UserNameInput.Text) && tBox.Name == UserNameInput.Name)
{
MessageBox.Show("Invalid User Name!");
this.UserNameInput.Text = "";
this.UserNameInput.Focus();
}
}
I am not sure if there's a right solution for this particular scenario here.
When you add a handler to validate your control on mouse leave, definitely it will be executed first regardless you clicked on another control within the tab or another tab itself.
This normal flow can't be ignored easily. It must be possible by hanlding the message loop yourself but the event based flow, first leave focus, and selected index change (selecting) event will be fired. I would suggest you not to disturb the flow as the validation is client side and pretty fast. Instead of messagebox, I would recommend you to use ErrorProvider and attach to the control whenever required. Also messagebox is quite disturbing and as per your code, you're forcefully making it focus to the textbox again.
How about the following code?
public partial class Form1 : Form
{
ErrorProvider errorProvider = new ErrorProvider();
public Form1()
{
InitializeComponent();
textBox1.Validating += new CancelEventHandler(textBox1_Validating);
}
private void textBox1_Leave(object sender, EventArgs e)
{
textBox1.CausesValidation = true;
}
void textBox1_Validating(object sender, CancelEventArgs e)
{
Regex UserNameRE = new Regex(#"^[a-zA-Z]\w*$");
if (!UserNameRE.IsMatch(textBox1.Text))
{
errorProvider.SetError(this.textBox1, "Invalid username");
}
}
}