Issue
I have currently set it up such that when a button is pressed and the textbox value is incorrect, it shows a messagebox. It is ok when I am testing only one textbox but when I want to test more than 1 textbox, it becomes messy where multiple messageboxes starts appearing.
Is there a way I can set it up such that the messagebox flashes the moment the user clicks away from the textbox.
For example I am expecting the textbox text to be ‘World’ and the user fills in the textbox with the text “hello”. After that, the use goes on to click into the next textbox. When he does that the messagebox appears.
I have conflicting thoughts on this cos user could click ‘x’ on the messagebox and go to click the 3rd textbox which will prompt the messagebox again causing annoyance. Would be great if I could get some advice on maybe a better way to do this.
In total I am having 3 textboxes all needing to be filled and I want to check if there is any invalid entry for each of them. I tried touchleave event but it is not working. Thanks for help.
private void Button_Click(object sender, RoutedEventArgs e)
{
name = textbox_Name.Text;
if (name != "World")
{
MessageBox.Show("Invalid Entry for name.");
}
age = textbox_age.Text;
if (age != "World")
{
MessageBox.Show("Invalid Entry for age.");
}
gender = textbox_gender.Text;
if (gender != "World")
{
MessageBox.Show("Invalid Entry for gender.");
}
}
Solution
Change the logic to something that only produces one message box when the button is clicked. You could display the issue with the input in that box, maybe if name and age are wrong you could list both problems. It just requires some extra logic somewhere. I don’t know what logic you need, but below is a simple example that shows a non-specific validation error.
private void Button_Click(object sender, RoutedEventArgs e)
{
name = textbox_Name.Text;
age = textbox_age.Text;
gender = textbox_gender.Text;
if (gender != "World" || name != "World" || age!="World" )
{
MessageBox.Show("Invalid Entry.");
}
}
As far as the TextChanged event goes, I’d recommend highlighting it on TextChanged, as suggested in a comment above. It’s less annoying for your users. Then you could show a single MessageBox if the user clicks the button to let them know they need to fix the red textboxes.
Answered By – eddie_cat
Answer Checked By – Senaida (BugsFixing Volunteer)