Guest User

Untitled

a guest
Aug 10th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 KB | None | 0 0
  1. c# pass value from a form that is closed
  2. namespace RepSalesNetAnalysis
  3. {
  4. public partial class LoginForm : Form
  5. {
  6. public bool letsGO = false;
  7. public LoginForm()
  8. {
  9. InitializeComponent();
  10. }
  11.  
  12. private static DataTable LookupUser(string Username)
  13. {
  14. const string connStr = "Server=server;" +
  15. "Database=dbname;" +
  16. "uid=user;" +
  17. "pwd=*****;" +
  18. "Connect Timeout=120;";
  19.  
  20.  
  21. const string query = "Select password From dbo.UserTable (NOLOCK) Where UserName = @UserName";
  22. DataTable result = new DataTable();
  23. using (SqlConnection conn = new SqlConnection(connStr))
  24. {
  25. conn.Open();
  26. using (SqlCommand cmd = new SqlCommand(query, conn))
  27. {
  28. cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = Username;
  29. using (SqlDataReader dr = cmd.ExecuteReader())
  30. {
  31. result.Load(dr);
  32. }
  33. }
  34. }
  35. return result;
  36. }
  37.  
  38. private void buttonLogin_Click(object sender, EventArgs e)
  39. {
  40. if (string.IsNullOrEmpty(textUser.Text))
  41. {
  42. //Focus box before showing a message
  43. textUser.Focus();
  44. MessageBox.Show("Enter your username", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
  45. //Focus again afterwards, sometimes people double click message boxes and select another control accidentally
  46. textUser.Focus();
  47. textPass.Clear();
  48. return;
  49. }
  50. else if (string.IsNullOrEmpty(textPass.Text))
  51. {
  52. textPass.Focus();
  53. MessageBox.Show("Enter your password", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
  54. textPass.Focus();
  55. return;
  56.  
  57. }
  58. //OK they enter a user and pass, lets see if they can authenticate
  59. using (DataTable dt = LookupUser(textUser.Text))
  60. {
  61. if (dt.Rows.Count == 0)
  62. {
  63. textUser.Focus();
  64. MessageBox.Show("Invalid username.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  65. textUser.Focus();
  66. textUser.Clear();
  67. textPass.Clear();
  68. return;
  69. }
  70. else
  71. {
  72. string dbPassword = Convert.ToString(dt.Rows[0]["Password"]);
  73. string appPassword = Convert.ToString(textPass.Text); //we store the password as encrypted in the DB
  74.  
  75. Console.WriteLine(string.Compare(dbPassword, appPassword));
  76.  
  77. if (string.Compare(dbPassword, appPassword) == 0)
  78. {
  79. DialogResult = DialogResult.OK;
  80. this.Close();
  81. }
  82. else
  83. {
  84. //You may want to use the same error message so they can't tell which field they got wrong
  85. textPass.Focus();
  86. MessageBox.Show("Invalid Password", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
  87. textPass.Focus();
  88. textPass.Clear();
  89. return;
  90. }
  91. }
  92. }
  93. }
  94.  
  95. private void emailSteve_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  96. {
  97. System.Diagnostics.Process.Start("mailto:stevesmith@shaftec.co.uk");
  98. }
  99. }
  100.  
  101. namespace RepSalesNetAnalysis
  102. {
  103. static class Program
  104. {
  105. /// <summary>
  106. /// The main entry point for the application.
  107. /// </summary>
  108. [STAThread]
  109. static void Main()
  110. {
  111. Application.EnableVisualStyles();
  112. Application.SetCompatibleTextRenderingDefault(false);
  113. //Application.Run(new LoginForm());
  114.  
  115. LoginForm fLogin = new LoginForm();
  116. if (fLogin.ShowDialog() == DialogResult.OK)
  117. {
  118. Application.Run(new Form1());
  119. }
  120. else
  121. {
  122. Application.Exit();
  123. }
  124. }
  125. }
  126. }
  127.  
  128. public string UserName
  129. {
  130. get
  131. {
  132. return textUser.Text;
  133. }
  134. }
  135.  
  136. private _userName;
  137. public class Form1(string userName)
  138. {
  139. _userName = userName;
  140. }
  141.  
  142. namespace RepSalesNetAnalysis
  143. {
  144. static class Program
  145. {
  146. /// <summary>
  147. /// The main entry point for the application.
  148. /// </summary>
  149. [STAThread]
  150. static void Main()
  151. {
  152. Application.EnableVisualStyles();
  153. Application.SetCompatibleTextRenderingDefault(false);
  154. //Application.Run(new LoginForm());
  155.  
  156. LoginForm fLogin = new LoginForm();
  157. if (fLogin.ShowDialog() == DialogResult.OK)
  158. {
  159. Application.Run(new Form1(fLogin.UserName));
  160. }
  161. else
  162. {
  163. Application.Exit();
  164. }
  165. }
  166. }
  167.  
  168. LoginForm fLogin = new LoginForm();
  169. if (fLogin.ShowDialog() == DialogResult.OK)
  170. {
  171. Application.Run(new Form1(fLogin.Username));
  172. }
  173.  
  174. LoginForm fLogin = new LoginForm();
  175. if (fLogin.ShowDialog() == DialogResult.OK)
  176. {
  177. Form1 frm = new Form1();
  178. frm.SomeValue = fLogin.txtUser.Text;
  179. Application.Run(frm);
  180. }
  181. else
  182. {
  183. Application.Exit();
  184. }
  185.  
  186. class LoginFormResult {
  187. public string UserName { get; set; }
  188. // ....
  189. public DialogResult Result { get; set; }
  190. }
  191.  
  192. public static void Execute(LoginFormData data) {
  193. using (var f = new LoginForm()) {
  194. f.txtUserName.Text = data.UserName ?? "";
  195. // ...
  196. data.Result = f.ShowDialog();
  197. if (data.Result == DialogResult.OK) {
  198. data.UserName = f.txtUserName.Text;
  199. // ....
  200. }
  201. }
  202. }
  203.  
  204. public void Main() {
  205. // ...
  206.  
  207. var loginData = new LoginData { UserName = "test" };
  208. LoginForm.Execute(loginData);
  209. if (loginData.Result == DialogResult.OK) {
  210. // ....
  211. }
  212.  
  213. // ...
  214. }
Add Comment
Please, Sign In to add comment