First: string.IsNullOrEmpty was implemented starting in .NET 2.0, but, since, as SAK observes, the 'Text property of a TextBox is
never 'null, there is no point using either string.IsNullOrEmpty, or string.IsNullOrWhiteSpace: but using them won't "hurt you" :) Using string.Empty, in combination with the string.Trim() method, makes more sense.
The person who implements a LogIn form has some choices to make:
1. to allow white-space entry in UserName or Password fields, or not, is one choice: [
^], [
^].
If you want to block the user from entering white-space: write an EventHandler for a Control KeyPress Event, and wire-up both UserName and Password TextBoxes to that EventHandler:
private void LogInTextBoxes_KeyPress(object sender, KeyPressEventArgs e)
{
}
2. most modern LogIn dialogs show password characters masked out: in Windows Forms you can use a MaskedTextBox to achieve this; I am not familiar with what C# Web applcations provide, but would certainly use one.
3. Deciding when to enable the "attempt login" Button: I like the idea that the UserName and Password TextBoxes start off showing "UserName" and "Password" in their respective TextBoxes, and I like the idea that when the user clicks in either one, their initial display goes away.
In the following example the names of the UserName and Password TextBoxes are 'tbUserName, and 'tbPassword; the name of the Button used to perform the LogIn is 'btnLogIn.
private bool tbPasswordFirstClick = true;
private bool tbUserNameFirstClick = true;
private void LogInTextBoxes_Click(object sender, EventArgs e)
{
if (ActiveControl == tbUserName && tbUserNameFirstClick)
{
tbUserName.Clear();
tbUserNameFirstClick = false;
}
else if (ActiveControl == tbPassword && tbPasswordFirstClick)
{
tbPassword.Clear();
tbPasswordFirstClick = false;
}
}
private void LogInTextBoxes_TextChanged(object sender, EventArgs e)
{
btnLogIn.Enabled = !
(tbUserName.Text.Trim == string.Empty
|| tbPassword.Text.Trim == string.Empty);
}
4. In case you are curious what I actually do in WinForms when the LogIn button is enabled, and it gets a Click:
public string UName { get; set; }
public string PWord { get; set; }
private void btnLogIn_Click(object sender, EventArgs e)
{
UName = tbUserName.Text;
PWord = tbPassword.Text;
this.DialogResult = DialogResult.OK;
Close();
}
Yes, there is a 'Cancel Button on the LogIn Form, and if it is clicked, the DialogResult property of the Form is set to 'false, and the Form is closed, which passes control back to the point where the Form was shown modally.
If you are really curious, read on: in the Windows Form application I modify the Program.cs file so the log in form is shown first: if the log in attempt fails. the Application never runs. Here's a simple example:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
LogInForm lg = new LogInForm();
Application.Run(lg);
if (lg.DialogResult == DialogResult.OK)
{
if(lg.PWord == "secret" && lg.UName == "user") Application.Run(new MainForm());
}
}