title: c# pass value from a form that is closed
body: <p>I have a OOP question.  I have 3 classes.  Program (main class), login form and form1.
  program runs login that checks for authentication, if this is successful form1 is then run while login closes.
  Now the problem I have is, what if I need to pass a variable value to my form1?<br>
  The reason is it I wish to use the username login as a variable, pass it to my form1 that then runs the appropiate code to execute specific things.<br>
  Basically admin would have full access to controls, but a normal user would have limited access.</p>
  
  <p>here's my code excluding my form1(not needed unless someone wish's to see it).</p>
  
  <pre><code>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");
      }
  }
  </code></pre>
  
  <p>and heres my main class</p>
  
  <pre><code>namespace RepSalesNetAnalysis
  {
  static class Program
  {
      /// &lt;summary&gt;
      /// The main entry point for the application.
      /// &lt;/summary&gt;
      [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();
          }
      }
  }
  }
  </code></pre>
  
  <p>So to summarise, I need to pass a value from my login class to my form1 class so i can use a condition inside form1 to limit the users interaction.</p>