C#登录示例

时间:2010-08-17 23:46:46

标签: c# winforms

我无法隐藏登录表单的主表单。用户登录后关闭登录表单并显示主表单。

我一直在困惑自己,我删除了所有代码,并开始新鲜。我可以隐藏登录表单。

我无法隐藏使用

调用的主窗体
Application.Run(new MainForm());

登录表单如下所示:

namespace WindowsFormsApplication1
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string username;
            string password;

            username = TB_username.Text;
            password = TB_password.Text;

            if (User.Login(username, password))
            {
                Globals._Login = true;

                // Close login form
                this.Dispose(false);

            }
            else
            {
                MessageBox.Show("Login Failed");
            }

        }
    }
}

我无法弄清楚如何隐藏然后在登录过后显示主表单。

谢谢任何例子都很棒

3 个答案:

答案 0 :(得分:22)

  1. 使用ShowDialog()打开登录表单。然后您不需要自己隐藏或禁用Mainform。实际上,如果您希望登录表单出现在应用程序的开头,请考虑在加载Mainform之前显示它:

    static void Main()
    {
        Application.SetCompatibleTextRenderingDefault(false);
        Application.EnableVisualStyles();
        DialogResult result;
        using (var loginForm = new LoginForm())
            result = loginForm.ShowDialog();
        if (result == DialogResult.OK)
        {
            // login was successful
            Application.Run(new Mainform());
        }
    }
    
  2. 在您的登录表单中,在button1_Click中,使用

    DialogResult = DialogResult.OK;
    

    关闭登录表单并将OK结果传递给Mainform。 不需要Dispose()

答案 1 :(得分:0)

使用登录表单上的ShowDialog()函数从应用程序的主窗体中加载登录表单。这将阻止在LoginForm结束之前显示MainForm。

private void MainForm_Load(object sender, EventArgs e) {
    var loginForm = new LoginForm();
    loginForm.ShowDialog();
}

答案 2 :(得分:0)

所以...这是我的登录:)你可以喜欢或不喜欢,但是......这是我的解决方案。

    private void FRIIB_Load(object sender, EventArgs e)
    {
        try
        {
            QueryBuilder.insql = Crypto.DecryptStringAES(Model.DecryptRegisteryValue("inSQL"), "inSQL");
        }
        catch (Exception exc)
        {
            MessageBox.Show(exc.Message);
        } // getting connection string
        if (!(new Func<bool>(() =>
                {
                    Func<bool> l = null; l = () =>
                    {
                        using (LoginForm loginDialog = new LoginForm())
                        {
                            loginDialog.ShowDialog();
                            loginDialog.Focus();
                            if (loginDialog.IsExit) return false;
                            else
                                if (loginDialog.IsAuthorized) return true;
                                else return l();
                        }
                    }; return l();
                }
              )()
            )) Close(); 
        else w8( () => LoadData() );
    }

以下是代码的一些描述:

    private void w8(Action action)
    {
        Cursor.Current = Cursors.WaitCursor;
        Application.DoEvents();
        action();
        Cursor.Current = Cursors.Default;
    }

Public Class DamnLogin
    Private db As FRIIB

    Public Sub New(ByVal connection As String)
        db = New FRIIB(connection)
    End Sub

    Public Function Login(ByVal name As String, ByVal password As String) As Boolean
        Dim GetUser = _
           From u In db.GetTable(Of [User])() _
           Where u.Name = name _
           And u.Password = password _
           Select u
        Return GetUser.Count = 0
    End Function
End Class

let Login usename password = 
    new LinqBase.DamnLogin(insql) |> fun damn ->
        not <| damn.Login(usename,password)

和登录表单

public partial class LoginForm : Form
{
    bool isAuthorized;
    bool exit;

    public bool IsAuthorized    { get { return this.isAuthorized;   } }
    public bool IsExit          { get { return this.exit;           } }

    public LoginForm()
    {
        isAuthorized    = false;
        exit            = false;

        InitializeComponent();
    }

    private void Close_Click(object sender, EventArgs e)
    {
        exit = true; 
        this.Close();
    }

    private void LoginButton_Click(object sender, EventArgs e)
    {
        if (Login.Text != "")
        {
            if (Login.Text.ToUpper() == "ADMIN")
            {
                if (Password.Text == Crypto.DecryptStringAES(Model.DecryptRegisteryValue("Password"), "Password"))
                    isAuthorized = true;
            }
            else
            {
                if (QueryBuilder.Login(Login.Text, Password.Text))
                    isAuthorized = true;
            }
        }

        this.Close();
    }
}