Login.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace FietsClient
  12. {
  13. public partial class Login : Form
  14. {
  15. private TcpConnection connection;
  16. public const int WM_NCLBUTTONDOWN = 0xA1;
  17. public const int HTCAPTION = 0x2;
  18. [DllImport("User32.dll")]
  19. public static extern bool ReleaseCapture();
  20. [DllImport("User32.dll")]
  21. public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
  22. public Login(TcpConnection connection)
  23. {
  24. this.connection = connection;
  25. InitializeComponent();
  26. }
  27. public Login(string message)
  28. {
  29. InitializeComponent();
  30. errorLBL.Text = message;
  31. }
  32. public void setError(string message)
  33. {
  34. errorLBL.Text = message;
  35. }
  36. private void SubmitButton_Click(object sender, EventArgs e)
  37. {
  38. if (string.IsNullOrWhiteSpace(UsernameBox.Text))
  39. {
  40. errorLBL.Text = "Username is incorrect";
  41. }
  42. else if (string.IsNullOrWhiteSpace(PasswordBox.Text))
  43. {
  44. errorLBL.Text = "Password is incorrect";
  45. }
  46. else
  47. {
  48. connection.SendLogin(UsernameBox.Text, PasswordBox.Text);
  49. PasswordBox.Text = "";
  50. }
  51. }
  52. private void menuBar_MouseDown(object sender, MouseEventArgs e)
  53. {
  54. if (e.Button == MouseButtons.Left)
  55. {
  56. ReleaseCapture();
  57. SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
  58. }
  59. }
  60. private void exit_Click(object sender, EventArgs e)
  61. {
  62. Application.Exit();
  63. }
  64. private void Login_Load(object sender, EventArgs e)
  65. {
  66. checkConnection();
  67. }
  68. private void checkConnection()
  69. {
  70. if (!connection.isConnectedFlag)
  71. connLBL.Text = "No Connection established";
  72. else
  73. connLBL.Text = "";
  74. }
  75. private void reconnectBTN_Click(object sender, EventArgs e)
  76. {
  77. if (!connection.isConnectedFlag)
  78. {
  79. connection.connect();
  80. checkConnection();
  81. this.Refresh();
  82. }
  83. }
  84. private void PasswordBox_KeyDown(object sender, KeyEventArgs e)
  85. {
  86. if (e.KeyCode == Keys.Enter)
  87. {
  88. SubmitButton_Click(this, new EventArgs());
  89. }
  90. }
  91. }
  92. }