Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 返回表单时如何清除文本字段_C#_.net_Winforms - Fatal编程技术网

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()
方法中创建的。你看过吗?