Guest User

Untitled

a guest
Aug 13th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. Getting Values From a Method In A Form Into Another Form
  2. public class Form1 : Form
  3. {
  4. private string user_code;
  5.  
  6. public string UserCode
  7. {
  8. get { return user_code; }
  9. }
  10.  
  11. public bool LoginUser()
  12. {
  13. user_code = null;
  14.  
  15.  
  16. if(textBox1.Text=="123"){
  17. user_code="usercode";
  18. }
  19. *
  20. */
  21. }
  22. }
  23.  
  24. Form1 form1 = new Form1();
  25. form1.LoginUser();
  26.  
  27. MessageBox.Show(form1.UserName);
  28.  
  29. objfrm1.loginuser(out U, out v, out w, out a,out b, out c);
  30.  
  31. public class Form1 : Form
  32. {
  33. private string user_pass;
  34. private string user_name;
  35. private bool insert_ability;
  36. private bool update_ability;
  37. private bool delete_ability;
  38.  
  39. public string UserPass
  40. {
  41. get { return user_pass; }
  42. }
  43.  
  44. public string UserName
  45. {
  46. get { return user_name; }
  47. }
  48.  
  49. public bool InsertAbility
  50. {
  51. get { return insert_ability; }
  52. }
  53.  
  54. public bool UpdateAbility
  55. {
  56. get { return update_ability; }
  57. }
  58.  
  59. public bool DeleteAbility
  60. {
  61. get { return delete_ability; }
  62. }
  63.  
  64. public bool LoginUser()
  65. {
  66. /*
  67. Your code here
  68. user_pass = "userpass";
  69. user_name = "username";
  70. insert_ability = true;
  71.  
  72. update_ability = false;
  73. delete_ability = false;
  74. *
  75. */
  76. }
  77. }
  78.  
  79. Form1 form1 = new Form1();
  80. form1.LoginUser();
  81.  
  82. MessageBox.Show(form1.UserName);
  83.  
  84. public class Form1 : Form
  85. {
  86. private string user_code;
  87.  
  88. public string UserCode
  89. {
  90. get { return user_code; }
  91. }
  92.  
  93. private string _testData; //THIS IS NEEDED
  94. public string TestData //THIS IS NEEDED
  95. {
  96. set { _testData = value;} //THIS IS NEEDED
  97. }
  98.  
  99. public bool LoginUser()
  100. {
  101. user_code = null;
  102. if(textBox1.Text=="123" || TestData=="123") //THIS IS NEEDED
  103. {
  104. user_code="usercode";
  105. }
  106. }
  107. }
  108.  
  109. Form1 form1 = new Form1();
  110. form1.TestData = "123"; //THIS IS NEEDED
  111. form1.LoginUser();
  112.  
  113. MessageBox.Show(form1.UserName);
  114.  
  115. class FormLogin : Form
  116. {
  117. public string UserName
  118. {
  119. get { return textBoxUserName.Text; }
  120. set { textBoxUserName.Text = value; }
  121. }
  122.  
  123. public string Password
  124. {
  125. get { return textBoxPassword.Text; }
  126. set { textBoxPassword.Text = value; }
  127. }
  128.  
  129. public FormLogin()
  130. {
  131. this.Closing += new System.ComponentModel.CancelEventHandler(FormLogin_Closing);
  132. }
  133.  
  134. void FormLogin_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  135. {
  136. if (this.DialogResult == DialogResult.Cancel) return;
  137.  
  138. if (!LoginUser(UserName, Password))
  139. {
  140. MessageBox.Show("Username or password is incorrect");
  141. e.Cancel = true;
  142. }
  143.  
  144. }
  145.  
  146. bool LoginUser(string userName, string password)
  147. {
  148. // check login and password in database for example
  149.  
  150. return true;
  151. }
  152. }
  153.  
  154. class Form3 :Form
  155. {
  156. void LoginButtonClick()
  157. {
  158. FormLogin formLogin = new FormLogin();
  159. formLogin.UserName = "LastUserName";
  160. //uncomment if you want hide main form
  161. //this.Hide();
  162. if (formLogin.ShowDialog() == DialogResult.OK)
  163. {
  164. MessageBox.Show(string.Format("Congratulation! You are logged as {0}", formLogin.UserName));
  165. }
  166.  
  167. //this.Show();
  168. }
  169. }
Add Comment
Please, Sign In to add comment