C# Displaying error messages by completing a simple registration form - c#

I'm new about C#, I learnt C programmation for one year now.
I created a Window Form which asks the user to complete a registration form.
My registration form
I'd like to display an error message below the buttons when a field is not filled or a field isn't well used.
I used this basic code :
private void button1_Click(object sender, EventArgs e)
{
if (!isOkay(userTextBox.Text))
{
label5.Text = "Please, enter an username.";
label5.Visible = true;
}
else if (!isOkay(mailTextBox.Text))
{
label5.Text = "Please, enter a mail address.";
label5.Visible = true;
}
else if (!confirmMailTextBox.Text.Equals(mailTextBox.Text) || !isOkay(confirmMailTextBox.Text))
{
label5.Text = "Please, match both mails addresses.";
label5.Visible = true;
}
else if (!isOkay(passwordTextBox.Text))
{
label5.Text = "Please, enter a password.";
label5.Visible = true;
}
else
{
label5.Text = "Valid form, yay !";
label5.Visible = true;
}
}
private Boolean isOkay(string textBoxContent)
{
return (textBoxContent.Length > 0 || textBoxContent.Equals(null));
}
Are there any elegant or optimized ways to do it properly ? I found some Error providers, but apparently error providers open a pop-up, and I just want a "red error message below buttons".
Can you give me some help ? :)

Given a class like this
public class RequiredFieldsError
{
private List<string> errors;
public RequiredFieldsError()
{
errors = new List<string>();
}
public int Count
{
get{return errors.Count;}
}
public void AddField(string errorField)
{
errors.Add(errorField);
}
public override string ToString()
{
if(errors.Count == 0)
return string.Empty;
else
{
string fields = string.Join(Environment.NewLine, errors);
fields = "The following fields contains errors:" + Environment.NewLine + fields;
return fields;
}
}
}
then you could change your code to
private void button1_Click(object sender, EventArgs e)
{
RequiredFieldsError rfe = new RequiredFieldsError();
if (!isOkay(userTextBox.Text))
rfe.AddField("User name missing, Please, enter an username.";
if (!isOkay(mailTextBox.Text))
rfe.AddField("Email address missing, Please, enter a mail address.";
if (!confirmMailTextBox.Text.Equals(mailTextBox.Text) || !isOkay(confirmMailTextBox.Text))
rfe.AddField("Email address doesn't match the confirmation email");
if (!isOkay(passwordTextBox.Text))
rfe.AddField("Password missing, Please, enter a password.";
if(rfe.Count > 0)
{
// MessageBox.Show(rfe.ToString());
label5.Text = rfe.ToString()
label5.Visible = true;
}
}
This approach avoids the unnerving situation (for your user) when he/she receives an error message, he/she fixes it just to receive another error message at the next attempt to confirm the form.
Of course your label should be tall enough to show all the possible messages or just use a messagebox.
I suggest also to change your IsOkay function to
private Boolean isOkay(string textBoxContent)
{
return !string.IsNullOrWitheSpace(textBoxContent));
}
this will handle also a string composed just of one or more spaces. (not null and not length==0)

You can use ErrorProvider. It show's an error icon after your textbox.
For one of your textboxes for example:
if (!isOkay(userTextBox.Text))
{
errorProvider1.SetError(userTextBox "yourmessage");
}
else{
errorProvider1.Clear();
}
Link: http://www.dotnetperls.com/errorprovider

Related

How to validate textbox value while user enters and display error message?

So, I have to code for a method that validates whether the string that saves name contains alphabets only, no numbers. The validation of textbox values should apply when the user enters by textchanged event before submitting the form and display an error message of red color on the label. My code works but the problem is when I enter a numeric number in text box, the label displays error which stays even when I delete the text box value and enter the alphabetic string.
I have declared a method which assign error string to label, and is called if regular expression does not match with the text box input, during text changed event.
public void Validator()
{
Calculate_Salary.Enabled = false;
label4.Text = "Please enter only alphabetical letters";
}
private void _Name_TextChanged(object sender, EventArgs e)
{
Regex pattern = new Regex("/^[A-Za-z]+$/");
string name = _Name.Text;
if (pattern.IsMatch(name))
{
Calculate_Salary.Enabled = true;
label4.Text = "";
}
else
{
Validator();
}
}
Just clear the textbox before you validate:
public void Validator()
{
Calculate_Salary.Enabled = false;
label4.Text = "Please enter only alphabetical letters";
}
private void _Name_TextChanged(object sender, EventArgs e)
{
label4.Text = "";
Regex pattern = new Regex("/^[A-Za-z]+$/");
string name = _Name.Text;
if (pattern.IsMatch(name))
{
Calculate_Salary.Enabled = true;
}
else
{
Validator();
}
}
Your Regex comparison is wrong try this code:
public void Validator()
{
Calculate_Salary.Enabled = false;
label4.Text = "Please enter only alphabetical letters";
}
private void _Name_TextChanged(object sender, EventArgs e)
{
label4.Text = "";
string name = _Name.Text;
if (Regex.IsMatch(name, #"^[a-zA-Z]+$"))
Calculate_Salary.Enabled = true;
else
Validator();
}
I changed the validation code. It seems to work now.
private void _Name_TextChanged(object sender, EventArgs e)
{
label4.Text = string.Empty;
string name = _Name.Text;
if (Regex.IsMatch(_Name.Text, "^[a-zA-Z]+$") || _Name.Text=="")
{
Calculate_Salary.Enabled = true;
}
else
{
Calculate_Salary.Enabled = false;
label4.Text = Validator();
}
}

C# winform - equivalent of InputScope

Is there a property in Winform (.Net 4.0) that is equivalent to InputScope in UWP?
No I do not believe so. You need to use the Validating event
Such as if you want to assure the textbox contains an email you would do something like this:
private void textBox1_Validating(object sender,
System.ComponentModel.CancelEventArgs e)
{
string errorMsg;
if(!ValidEmailAddress(textBox1.Text, out errorMsg))
{
// Cancel the event and select the text to be corrected by the user.
e.Cancel = true;
textBox1.Select(0, textBox1.Text.Length);
// Set the ErrorProvider error with the text to display.
this.errorProvider1.SetError(textBox1, errorMsg);
}
}
private void textBox1_Validated(object sender, System.EventArgs e)
{
// If all conditions have been met, clear the ErrorProvider of errors.
errorProvider1.SetError(textBox1, "");
}
public bool ValidEmailAddress(string emailAddress, out string errorMessage)
{
// Confirm that the email address string is not empty.
if(emailAddress.Length == 0)
{
errorMessage = "email address is required.";
return false;
}
// Confirm that there is an "#" and a "." in the email address, and in the correct order.
if(emailAddress.IndexOf("#") > -1)
{
if(emailAddress.IndexOf(".", emailAddress.IndexOf("#") ) > emailAddress.IndexOf("#") )
{
errorMessage = "";
return true;
}
}
errorMessage = "email address must be valid email address format.\n" +
"For example 'someone#example.com' ";
return false;
}

Give feedback with MessageBox if nothing matches text file C#

I'm making a login system currently and I would like to present feedback when the username or the password are incorrect.
Here is the content of the text file from which I'm reading the details:
Ryan:password
Username:password
When I enter Ryan and password, it works fine and brings me to the next form.
However, when I enter Username and password, it comes up with the 'Username Incorrect' message box first, and then after I close out of that message box, it brings me to the next form.
I would like it to bring me directly to the next form without showing the Username Incorrect MessageBox first, even if I do enter the details on the second line. There would be more lines in the text file in the future.
Any help would be greatly appreciated, thanks!
Here is the code:
private void button1_Click(object sender, EventArgs e)
{
string[] userdetails = File.ReadAllLines(AppDomain.CurrentDomain.BaseDirectory + "UserDetails.txt");
foreach (string user in userdetails)
{
string[] splitDetails = user.Split(':');
Login.username = splitDetails[0];
Login.password = splitDetails[1];
label1.Text = Login.username;
label2.Text = Login.password;
if ((txtUsername.Text == Login.username) && (txtPassword.Text == Login.password))
{
MessageBox.Show("Welcome " + Login.username);
this.Hide();
frmMainMenu menu = new frmMainMenu();
menu.Show();
break;
}
else
{
if ((txtUsername.Text == Login.username) && (txtPassword.Text != Login.password))
{
MessageBox.Show("Password incorrect");
break;
}
if(txtUsername.Text != Login.username)
{
MessageBox.Show("Username incorrect");
}
}
}
}
The logic is incorrect.
Ask yourself, when should you stop going through your list of credentials?
Assuming a username is unique, I see only one situation that could break your loop, and that is "the username has been found".
As soon as you find the input username in your list, you know the loop has to break. You then only have to check whether the password is correct or not.
If the password is correct, you can open your new window and return your function, it has done its job.
And after the loop, you put your MessageBox, with a message depending on if the username has been found or not.
private void button1_Click(object sender, EventArgs e)
{
string[] userdetails = File.ReadAllLines(AppDomain.CurrentDomain.BaseDirectory + "UserDetails.txt");
bool usernameFound = false;
foreach (string user in userdetails)
{
string[] splitDetails = user.Split(':');
Login.username = splitDetails[0];
Login.password = splitDetails[1];
label1.Text = Login.username;
label2.Text = Login.password;
if (txtUsername.Text == Login.username)
{
if (txtPassword.Text == Login.Password)
{
MessageBox.Show("Welcome " + Login.username);
this.Hide();
frmMainMenu menu = new frmMainMenu();
menu.Show();
return; // we're done here, so return instead of break
}
usernameFound = true;
break; // we're not gonna find this username again, so might as well quit the loop
}
}
//we only get there if the credentials were incorrect
//so we check if the username was found, if yes, the
//password was incorrect, if not, the username was
string message = String.Empty;
if (usernameFound)
message = "Password";
else
message = "Username";
message += " incorrect";
MessageBox.Show(message);
//or shorten the above 7 lines with a ternary operator
//MessageBox.Show((usernameFound ? "Password" : "Username") + " incorrect");
}
In your senario if you had 100 users (user1..user100)
your code reads
for each line in file
check if matches
if yes make new form
else complain it isnt a match
So for user100, 99 its not a match messages will appear one for each non match before it.
You would need to code it like this
isfound=false
for each line in file
check if match
if yes set isfound and break
if isfound
show form blah
else
whine not found

Only one message box is popped up after validating the 2 validations

abstract class User
{
public string Username;
public string Password;
public virtual bool Validate(string username, string password)
{
Username = "";
Password = "";
if (password == Password && username == Username)
{
MessageBox.Show("Incorrect password or username");
return true;
}
else
{
return false;
}
}
form code:
private void btnSubmit_Click(object sender, EventArgs e)
{
UserAdmin admin = new UserAdmin();
UserEmployee empp = new UserEmployee();
bool validateAdmin = admin.Validate(txtUsername.Text, txtPassword.Text);
bool validateEmpp = empp.Validate(txtUsername.Text, txtPassword.Text);
if (validateAdmin==true || validateEmpp == true )
{
this.Hide();
// Create a new instance of the options class
Options opt = new Options();
opt.Closed += (s, args) => this.Close();
// Show the settings form
opt.Show();
}
When the first validation is false it keeps checking the second validation as well.By doing so 2 message boxes are being popped up.
EDIT:
Can you show only one message box instead of 2? it only shows the message box if the username or password is incorrect
You can combine validations in conditional expression:
if(admin.Validate(txtUsername.Text, txtPassword.Text) && empp.Validate(txtUsername.Text, txtPassword.Text))
{
... // is ok
}
else
{
... // when either Validate from left to right is not ok
}
This way if admin.Validate return false, then empp is not validated.
I would say that the standard way to stop execution of the code - is throwing an exception. So what You could do - is throw a 'ValidationExceptio' and implement appropriate ex handlers
To stop showing two message boxes, Remove the message box from method and include it into the click event. So the method will be like this:
public virtual bool Validate(string username, string password)
{
if (password == Password && username == Username)
{
return true;
}
else
{
return false;
}
}
And the click event will be like :
private void btnSubmit_Click(object sender, EventArgs e)
{
UserAdmin admin = new UserAdmin();
UserEmployee empp = new UserEmployee();
bool validateAdmin = admin.Validate(txtUsername.Text, txtPassword.Text);
bool validateEmpp = empp.Validate(txtUsername.Text, txtPassword.Text);
if (validateAdmin)
{
MessageBox.Show("Successfylly login as Admin");
//operation here
}
else if (validateEmpp)
{
MessageBox.Show("Successfylly login as " + txtUsername.Text);
//operation here
}
else { MessageBox.Show("Incorrect password or username"); }
}

Sign up using Entity Framework

So i try to create a sign up method using entity framework and c#, this is the method :
//Button create new account
private void BtnSignUp_Click(object sender, EventArgs e)
{
IEnumerable<DriverAcount> list = from a in context.DriverAcounts select a;
foreach (var Ac in list)
{
if (TxBoxNewUserName.Text != Ac.Login)
{
if (TxtBoxPASS1.Text == TxBoxPass.Text)
{
Ac.Login = TxBoxNewUserName.Text;
Ac.Password = TxtBoxPASS1.Text;
context.DriverAcounts.Add(Ac);
MessageBox.Show("the account is create succefuly");
TxBoxNewUserName.Text = "";
TxtBoxPASS1.Text = "";
TxBoxPass.Text = "";
break;
}
else
{
MessageBox.Show("the two passwords didn't matched");
}
TxBoxNewUserName.Text = "";
TxtBoxPASS1.Text = "";
TxBoxPass.Text = "";
continue;
}
else
{
MessageBox.Show("this username is already exist, please choose another one");
TxBoxNewUserName.Text = "";
TxtBoxPASS1.Text = "";
TxBoxPass.Text = "";
break;
}
}
context.SaveChanges();
}
the problem is when i want to add a new user normally it should looking if it exist in database or not, but it didn't do it right, for example if we have two names in DB name1 and name2 and into the TextBox we have name2 it will add the name2 in DB even if it is already exist.
So plz if someone have an idea i will be very appreciate.
You need to look at your code again and understand exactly what it's doing - have you attached the debugger and stepped through it? The driver name will always be created on the first iteration of the foreach loop if the first name retrieved doesn't match the one in the text box. All the others will be ignored.
You can try this instead. There's no need querying for all the records (like you're doing at the moment) when you only want to check if one already exists.
private void BtnSignUp_Click(object sender, EventArgs e)
{
// This performs a case sensitive match on the login name, you'll need to change it if you want to ignore case
DriverAcount existingAccount = context.DriverAcounts.FirstOrDefault(d => d.Login == TxBoxNewUserName.Text);
if (existingAccount != null)
{
MessageBox.Show("This username already exists, please choose another one.");
}
else
{
if (TxtBoxPASS1.Text == TxBoxPass.Text)
{
Ac.Login = TxBoxNewUserName.Text;
Ac.Password = TxtBoxPASS1.Text;
context.DriverAcounts.Add(Ac);
// Only need to call this if you've made changes, so I've moved it here
context.SaveChanges();
MessageBox.Show("The account was created successfully");
}
else
{
MessageBox.Show("The two passwords didn't match each other.");
}
}
TxBoxNewUserName.Text = "";
TxtBoxPASS1.Text = "";
TxBoxPass.Text = "";
}

Categories