弹出输入框

时间:2013-01-13 21:40:41

标签: c# winforms visual-studio-2010

我正在尝试弹出一个输入框,要求用户输入类似于messagebox.show的密码。但我希望用户能够输入框,以便我可以使用返回数据进行验证。

系统将检查用户是否已通过身份验证,如果不是,则会通过弹出请求身份验证。

2 个答案:

答案 0 :(得分:1)

假设您在WinForms中,您需要做的是创建一个新表单,为其添加一个文本框和按钮,如果未经过身份验证,则调用它。

public partial class Form1
{
    public void Main()
    {
    bool authenticated = ...

    if(!authenticated)
    {
       Your_Form newForm = new Your_Form();
       newForm.Show(this);
       string password = newForm.Password;
       if(password != "")
           ...
    }
    }
    }

public class Your_Form
{
public TextBox textBox1 = new TextBox();
// ...
public string Password = "";

private void button1_Click(object sender, EventArgs e)
{
   this.Password = textBox1.Text;
}
// ...
}

答案 1 :(得分:0)

我将不得不假设您正在使用Winforms,但此代码也可以转换为WPF或Web。您需要使用父表单中的DisplayDialog()来显示您的辅助表单,即弹出窗口。这会将您的新表单显示为对话框。最重要的是,当它关闭时,它总是会向父表单发回DialogResult(解释herehere),您可以使用该表单来确定用户是否经过身份验证。 This文章解释了如何做一些与我想要的非常相似的事情