C# login examples

前端 未结 3 1941
傲寒
傲寒 2020-12-30 11:35

I am having trouble hiding my main form for a login form. Once user has logged in to close login form and show main form.

I have been confusing myself that much I ha

相关标签:
3条回答
  • 2020-12-30 11:55

    Use the ShowDialog() function on the login form to load the login form from within the main form of the application. This will prevent the MainForm being displayed until after the LoginForm has finished.

    private void MainForm_Load(object sender, EventArgs e) {
        var loginForm = new LoginForm();
        loginForm.ShowDialog();
    }
    
    0 讨论(0)
  • 2020-12-30 12:05
    1. Use ShowDialog() to open the login form. Then you don’t need to hide or disable the Mainform yourself. In fact, if you want the login form to appear at the beginning of the application, consider showing it before even loading the 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. Inside your login form, in button1_Click, use

      DialogResult = DialogResult.OK;
      

      to close the login form and pass the OK result to the Mainform. There is no need for Dispose().

    0 讨论(0)
  • 2020-12-30 12:08

    So ... Here is my login :) You can like it or don't like, but so ... This is my solution.

        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() );
        }
    

    and here is some description for code :

        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)
    

    and login form

    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();
        }
    }
    
    0 讨论(0)
提交回复
热议问题