C# 返回表单时如何清除文本字段
我想在表单再次聚焦后清除表单上的文本字段。这是我要清除名为C# 返回表单时如何清除文本字段,c#,.net,winforms,C#,.net,Winforms,我想在表单再次聚焦后清除表单上的文本字段。这是我要清除名为user\u textbox和pwd\u textbox namespace RDASMS { public partial class Login : Form { public Login() { InitializeComponent(); user_textbox.Clear(); pwd_textbox.Cle
user\u textbox
和pwd\u 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");
}
}
}
您可以处理Form.Activated事件来处理此问题,激活时清除文本框 您可以将一些代码如下所示:
foreach (Control c in yourForm.Controls)
{
if (c is TextBox)
{
((TextBox)c).Clear();
}
}
由于您在表单上调用了
Hide
,这等于将Visible
设置为false
,因此您可以使用表单的事件来清除文本字段
private void Form_VisibleChanged(object sender, EventArgs e){
if (this.Visible == true) {
user_textbox.Clear();
pwd_textbox.Clear();
}
}
我使用Form Activated来确定表单是否重新聚焦。-您遇到了什么问题?如何返回到主表单?您只显示隐藏main并将另一个窗体带到前台的代码。好吧,不需要在构造函数中清除它们。文本框是在
InitializeComponent()
方法中创建的。你看过吗?