As I just started with C# I have some trouble to get started. I am trying to make a simple login system, and this is what I've got so far:
private void button1_Click(object sender, EventArgs e)
{
string username = "Admin";
string password = "root";
if (!string.IsNullOrEmpty(textBox1.Text) && (!string.IsNullOrEmpty(textBox2.Text)))
MessageBox.Show("Fields are not filled");
if (!string.IsNullOrEmpty(textBox1.Text))
MessageBox.Show("No Password inserted");
if (!string.IsNullOrEmpty(textBox2.Text))
MessageBox.Show("No username inserted");
if ((textBox1.Text == username) && (textBox2.Text == password))
MessageBox.Show("Login Succeed");
else
MessageBox.Show("Login failed");
}
When I type in Admin
as username and root
as password it gives me login succeed, when I do random Username & Password it gives me Login failed, that part works flawless. It's more about the "not inserted" part and when fields are not filled.
It simply does nothing :') It keeps saying "login failed" any idea how to fix this?
replace
!string.IsNullOrEmpty()
with
string.IsNullOrWhiteSpace()
so the complete solution:
private void button1_Click(object sender, EventArgs e)
{
string username = "Admin";
string password = "root";
if (string.IsNullOrWhiteSpace (textBox1.Text))
MessageBox.Show("No Password inserted");
else if (string.IsNullOrWhiteSpace (textBox2.Text))
MessageBox.Show("No username inserted");
else if ((textBox1.Text == username) && (textBox2.Text == password))
MessageBox.Show("Login Succeed");
else
MessageBox.Show("Login failed");
}
Try following:
private void button1_Click(object sender, EventArgs e)
{
string username = "Admin";
string password = "root";
//When both are empty
if (string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox2.Text))
{
MessageBox.Show("Fields are not filled");
return;
}
//When username is empty
if (string.IsNullOrEmpty(textBox1.Text))
{
MessageBox.Show("No username inserted");
return;
}
//When password is empty
if (string.IsNullOrEmpty(textBox2.Text))
{
MessageBox.Show("No password inserted");
return;
}
if ((textBox1.Text == username) && (textBox2.Text == password))
MessageBox.Show("Login Succeed");
else
MessageBox.Show("Login failed");
}
Main issue is the logic in first IF statement you're asking basically:
IF(Not-IsNullOrEmpty(LOGIN) AND Not-IsNullOrEmpty(PASS))
SHOW MESSAGE
which is why it does not work.
I would probably rewrite it bit to not repeat myself and make it a bit more readable:
private void button1_Click(object sender, EventArgs e)
{
string username = "Admin";
string password = "root";
var message = string.Empty;
bool loginEmpty = string.IsNullOrEmpty(textBox1.Text),
passEmpty = string.IsNullOrEmpty(textBox2.Text),
loggedIn = (textBox1.Text == username) && (textBox2.Text == password);
if (!loggedIn)
{
if (loginEmpty && passEmpty)
message = " - Fields are not filled";
else if (passEmpty)
message = " - No Password inserted";
else if (loginEmpty)
message = " - No username inserted";
MessageBox.Show("Login failed" + message);
return;
}
MessageBox.Show("Login succeded");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.