Reputation: 515
I want to clear the text fields on the form once it's in focus again. Here is my code for the form in which I want to clear the text fields named user_textbox
and pwd_textbox
namespace RDASMS
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
user_textbox.Clear();
pwd_textbox.Clear();
}
private void register_Click(object sender, EventArgs e)
{
Registration newuser = new Registration();
newuser.Show();
this.Hide();
}
private void submit_login_Click(object sender, EventArgs e)
{
int checkuser = string.CompareOrdinal(user_textbox.Text, "Admin");
if (checkuser == 0)
{
int checkpwd = string.CompareOrdinal(pwd_textbox.Text, "rnsit123");
if (checkpwd == 0)
{
Admin newuser = new Admin();
newuser.RefToLogin = this;
newuser.Show();
this.Hide();
}
else
MessageBox.Show("Invalid Password");
}
else
MessageBox.Show("Invalid Username");
}
}
}
Upvotes: 1
Views: 220
Reputation: 1
I use Form Activated to determine if the form has come back into focus.
Upvotes: 0
Reputation: 35544
Since you call Hide
on your Form, which is equal to setting Visible
to false
, you can use the event VisibleChanged of the Form to clear your TextFields.
private void Form_VisibleChanged(object sender, EventArgs e){
if (this.Visible == true) {
user_textbox.Clear();
pwd_textbox.Clear();
}
}
Upvotes: 0
Reputation: 2962
You can handle the Form.Activated event to handle this, when activated clear out your textboxes.
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.activated.aspx
you can put some code like this :
foreach (Control c in yourForm.Controls)
{
if (c is TextBox)
{
((TextBox)c).Clear();
}
}
Upvotes: 2