Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- c# pass value from a form that is closed
- namespace RepSalesNetAnalysis
- {
- public partial class LoginForm : Form
- {
- public bool letsGO = false;
- public LoginForm()
- {
- InitializeComponent();
- }
- private static DataTable LookupUser(string Username)
- {
- const string connStr = "Server=server;" +
- "Database=dbname;" +
- "uid=user;" +
- "pwd=*****;" +
- "Connect Timeout=120;";
- const string query = "Select password From dbo.UserTable (NOLOCK) Where UserName = @UserName";
- DataTable result = new DataTable();
- using (SqlConnection conn = new SqlConnection(connStr))
- {
- conn.Open();
- using (SqlCommand cmd = new SqlCommand(query, conn))
- {
- cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = Username;
- using (SqlDataReader dr = cmd.ExecuteReader())
- {
- result.Load(dr);
- }
- }
- }
- return result;
- }
- private void buttonLogin_Click(object sender, EventArgs e)
- {
- if (string.IsNullOrEmpty(textUser.Text))
- {
- //Focus box before showing a message
- textUser.Focus();
- MessageBox.Show("Enter your username", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
- //Focus again afterwards, sometimes people double click message boxes and select another control accidentally
- textUser.Focus();
- textPass.Clear();
- return;
- }
- else if (string.IsNullOrEmpty(textPass.Text))
- {
- textPass.Focus();
- MessageBox.Show("Enter your password", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
- textPass.Focus();
- return;
- }
- //OK they enter a user and pass, lets see if they can authenticate
- using (DataTable dt = LookupUser(textUser.Text))
- {
- if (dt.Rows.Count == 0)
- {
- textUser.Focus();
- MessageBox.Show("Invalid username.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- textUser.Focus();
- textUser.Clear();
- textPass.Clear();
- return;
- }
- else
- {
- string dbPassword = Convert.ToString(dt.Rows[0]["Password"]);
- string appPassword = Convert.ToString(textPass.Text); //we store the password as encrypted in the DB
- Console.WriteLine(string.Compare(dbPassword, appPassword));
- if (string.Compare(dbPassword, appPassword) == 0)
- {
- DialogResult = DialogResult.OK;
- this.Close();
- }
- else
- {
- //You may want to use the same error message so they can't tell which field they got wrong
- textPass.Focus();
- MessageBox.Show("Invalid Password", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
- textPass.Focus();
- textPass.Clear();
- return;
- }
- }
- }
- }
- private void emailSteve_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
- {
- System.Diagnostics.Process.Start("mailto:stevesmith@shaftec.co.uk");
- }
- }
- namespace RepSalesNetAnalysis
- {
- static class Program
- {
- /// <summary>
- /// The main entry point for the application.
- /// </summary>
- [STAThread]
- static void Main()
- {
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- //Application.Run(new LoginForm());
- LoginForm fLogin = new LoginForm();
- if (fLogin.ShowDialog() == DialogResult.OK)
- {
- Application.Run(new Form1());
- }
- else
- {
- Application.Exit();
- }
- }
- }
- }
- public string UserName
- {
- get
- {
- return textUser.Text;
- }
- }
- private _userName;
- public class Form1(string userName)
- {
- _userName = userName;
- }
- namespace RepSalesNetAnalysis
- {
- static class Program
- {
- /// <summary>
- /// The main entry point for the application.
- /// </summary>
- [STAThread]
- static void Main()
- {
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- //Application.Run(new LoginForm());
- LoginForm fLogin = new LoginForm();
- if (fLogin.ShowDialog() == DialogResult.OK)
- {
- Application.Run(new Form1(fLogin.UserName));
- }
- else
- {
- Application.Exit();
- }
- }
- }
- LoginForm fLogin = new LoginForm();
- if (fLogin.ShowDialog() == DialogResult.OK)
- {
- Application.Run(new Form1(fLogin.Username));
- }
- LoginForm fLogin = new LoginForm();
- if (fLogin.ShowDialog() == DialogResult.OK)
- {
- Form1 frm = new Form1();
- frm.SomeValue = fLogin.txtUser.Text;
- Application.Run(frm);
- }
- else
- {
- Application.Exit();
- }
- class LoginFormResult {
- public string UserName { get; set; }
- // ....
- public DialogResult Result { get; set; }
- }
- public static void Execute(LoginFormData data) {
- using (var f = new LoginForm()) {
- f.txtUserName.Text = data.UserName ?? "";
- // ...
- data.Result = f.ShowDialog();
- if (data.Result == DialogResult.OK) {
- data.UserName = f.txtUserName.Text;
- // ....
- }
- }
- }
- public void Main() {
- // ...
- var loginData = new LoginData { UserName = "test" };
- LoginForm.Execute(loginData);
- if (loginData.Result == DialogResult.OK) {
- // ....
- }
- // ...
- }
Add Comment
Please, Sign In to add comment