C # Usage Examples

I'm having trouble hiding my main form for the login form. As soon as the user logs in to close the login form and show the main form.

I am confused that I deleted all the code and started a new one. I can hide the login form.

I was unable to hide the main form with

Application.Run(new MainForm()); 

The login form is as follows:

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

I cannot figure out how to hide, then show the main form after the login has passed.

Thank you for any great examples.

+7
c # winforms
source share
3 answers
  • Use ShowDialog() to open the login form. Then you do not need to hide or disable Mainform yourself. In fact, if you want the login form to appear at the beginning of the application, consider it before you load 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()); } } 
  • In your login form to button1_Click use

     DialogResult = DialogResult.OK; 

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

+20
source share

So ... Here is my login :) You may or may not like it, but so ... This is my decision.

  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 the 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(); } } 
+1
source share

Use the ShowDialog () function in the login form to load the login form into the main application form. This will prevent the MainForm from being displayed until the input process is complete.

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

All Articles