Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Threading;
- namespace Client
- {
- public partial class Form1 : Form
- {
- // note !-> all the consts' values mean this.width (OR height) / value
- private const int ALLIGMENT_FROM_TOP = 20; // the alligment from the top of the screen
- private const int ALLIGMENT_FROM_LEFT = 20; // the alligment from the Left of the screen
- private const int LOG_IN_FORM_TXT_X_SIZE = 4; // the size of the text boxes in x axis which will be shown on the screen
- private const int LOG_IN_FORM_TEXT_SECTOR_HEIGHT = 2; // the height of the username password and email text boxes
- private const int LOG_IN_FORM_ALLGIMENT_FROM_TEXT_TO_LABEL = 50; // alligment from label to textbox
- private const int LOG_IN_FORM_PICBOX_HEIGHT = 4; // the height of the buttons (width / 4)
- private const int SIGN_OUT_FORM_BACK_HEIGHT_WIDTH = 6; // the height and width of the back button
- private const int CLOSE_GAME_BUTTON_WIDTH = 7; // the width of the close button
- private const int CLOSE_GAME_BUTTON_HEIGHT = 16; // the height of the close button
- private const int MAIN_FORM_PICBOX_HEIGHT = 10; // the height of the picture boxes in main form
- private const int MAIN_FORM_PICBOX_WIDTH = 5; // the width of the picture boxes in the main form
- private const int MAIN_FORM_LOG_OUT_HEIGHT = 12; // the height of the log out button
- private const int MAIN_FORM_LOG_OUT_WIDTH = 7; // the width of the log out button
- private const int BEST_SCORES_FORM_PB_HEIGHT = 4; // the height of the best scores pictures
- private const int BEST_SCORES_FORM_PB_WIDTH = 5; // the width of the best scores picture
- private const int JOIN_ROOM_FORM_ROOMS_LB_WIDTH = 4; // the width of the rooms list
- private const int JOIN_ROOM_FORM_USERS_LB_WIDTH = 5; // the width of the users list
- private const int JOIN_ROOM_FORM_BUTTON_SECTOR_Y = 4; // the height of the button sector
- private const int JOIN_ROOM_FORM_REFRESH_HEIGHT = 2; // the height of the refresh button
- private const int JOIN_ROOM_FORM_REFRESH_WIDTH = 8; // the width of the refresh button
- private const int JOIN_ROOM_FORM_JOIN_HEIGHT = 8; // the height of the join button
- private const int JOIN_ROOM_FORM_JOIN_WIDTH = 4; // the width of the join button
- private MyPictureBox PBClose; // the close button, <> needs to be disposed
- private Thread t; // Thread to handle messages from the server
- private Stream stream; // creates a connection with the server by ip and port
- private int alligmentFromSubjectY; // the alligment in y axis from the subject
- private string uid; // the username of the signed in user
- public Form1()
- {
- //this.stream = new Stream("127.0.0.1", 8686);
- //t = new Thread(handleMessage);
- //t.Start();
- InitializeComponent();
- this.Width = Screen.PrimaryScreen.Bounds.Width;
- this.Height = Screen.PrimaryScreen.Bounds.Height;
- InitStaticVars();
- InitJoinRoom(null, EventArgs.Empty);
- changeRoomsList(new List<string> { "tal", "Dolev", "I", "J", "K", "L", "M", "N" });
- changeUsersInRoom(new List<string> { "tal", "yarini" });
- changeRoomsList(new List<string> { "tal", "Dolev", "I", "J", "K", "L", "M", "N" });
- //InitPersonalStatus(null, EventArgs.Empty);
- //ChangePersonalStatus(new string[4] { "4", "10", "5", "3" });
- //InitLoginScreen(null, EventArgs.Empty);
- //InitSignUp(null, EventArgs.Empty);
- //InitMainScreen(null, EventArgs.Empty);
- //InitBestScores(null, EventArgs.Empty);
- //KeyValuePair<string, string>[] arr = new KeyValuePair<string, string>[3];
- //arr[0] = new KeyValuePair<string, string>("tal", "100");
- //arr[1] = new KeyValuePair<string, string>("Dolev", "0");
- //arr[2] = new KeyValuePair<string, string>("yarin", "10");
- //changeBestScores(arr);
- //CleanAllControlers();
- }
- /// <summary>
- /// initializes all static variables of custom classes, and current class
- /// </summary>
- public void InitStaticVars()
- {
- // MyTxtBox Class:
- // the default width of the text box
- MyTxtBox._width = (this.Width / LOG_IN_FORM_TXT_X_SIZE);
- // current class:
- // sets the close button
- PBClose = new MyPictureBox("PBClose", "close", new Size(this.Width / CLOSE_GAME_BUTTON_WIDTH, this.Height / CLOSE_GAME_BUTTON_HEIGHT));
- PBClose.Location = new Point(this.Width / 2 - PBClose.Width / 2, this.Height - PBClose.Height - this.Height / ALLIGMENT_FROM_TOP);
- PBClose.Click += PBClose_Click;
- this.Controls.Add(PBClose);
- // sets the alligment from top of screen
- alligmentFromSubjectY = this.Height / ALLIGMENT_FROM_TOP;
- uid = "tal";
- }
- /// <summary>
- /// makes the login is screen
- /// </summary>
- public void InitLoginScreen(object sender, EventArgs args)
- {
- int reminingY = this.Height;
- int nextYPos = 0;
- int xOfTextBoxes = 0;
- int sizeOfPictureBox = 0;
- int alligmentFromPictureBox = 0;
- int alligmentFromTextBoxes = 0;
- int alligmentFromTextBoxesToLabels = this.Width / LOG_IN_FORM_ALLGIMENT_FROM_TEXT_TO_LABEL;
- int xOfPicBoxes = 0;
- List<Control> cont = new List<Control>();
- // sets the background of the page
- this.BackgroundImage = Client.Properties.Resources.loginBG;
- // sets the color of the letters
- this.ForeColor = Color.White;
- // sets the subject label
- Label LBLSubject = new MyLabel("Please Log In", "LBLSubject"); // i create a label to know the width of its text
- LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
- cont.Add(LBLSubject);
- nextYPos = LBLSubject.Location.Y + alligmentFromSubjectY + LBLSubject.Height; // begining of details section
- reminingY -= nextYPos; // sets the space of details section
- // sets the text box of username
- TextBox TXTuserName = new MyTxtBox("TXTusername"); // i create a label to know the height of its font
- alligmentFromTextBoxes = ((reminingY / LOG_IN_FORM_TEXT_SECTOR_HEIGHT) - (TXTuserName.Height * 2)) / 3; // generates the needed alligment
- nextYPos += alligmentFromTextBoxes;
- reminingY -= reminingY / LOG_IN_FORM_TEXT_SECTOR_HEIGHT; // sets the space of buttons section
- TXTuserName.Location = new Point((this.Width / 2) - (TXTuserName.Width / 2), nextYPos);
- cont.Add(TXTuserName);
- // sets the label of username
- Label LBLusername = new MyLabel("UserName: ", "LBLusername"); // sets the label of the userName
- LBLusername.Location = new Point(TXTuserName.Location.X - LBLusername.Width - alligmentFromTextBoxesToLabels, nextYPos);
- cont.Add(LBLusername);
- nextYPos += (TXTuserName.Height + alligmentFromTextBoxes); // sets the position of the next textBox
- xOfTextBoxes = TXTuserName.Location.X;
- // sets the text box of password
- TextBox TXTpsw = new MyTxtBox(new Point(xOfTextBoxes, nextYPos), "TXTpsw");
- cont.Add(TXTpsw);
- // sets the label of password
- Label LBLpsw = new MyLabel("password: ", "LBLpsw"); // sets the password label
- LBLpsw.Location = new Point(xOfTextBoxes - LBLpsw.Width - alligmentFromTextBoxesToLabels, nextYPos);
- cont.Add(LBLpsw);
- nextYPos += (TXTuserName.Height + alligmentFromTextBoxes); // sets the begining of the buttons sector
- // sets the buttons section
- sizeOfPictureBox = reminingY / LOG_IN_FORM_PICBOX_HEIGHT;
- reminingY -= (sizeOfPictureBox * 2);
- alligmentFromPictureBox = reminingY / 2;
- // sets the sign in button
- PictureBox PBSignIn = new MyPictureBox("PBSignIn", "SignIn", new Size(TXTuserName.Width, sizeOfPictureBox));
- xOfPicBoxes = (this.Width / 2) - (PBSignIn.Width / 2); // sets the x of picture boxes
- PBSignIn.Location = new Point(xOfPicBoxes, nextYPos);
- PBSignIn.Click += (EventHandler)PBSign_In_Click;
- cont.Add(PBSignIn);
- nextYPos += alligmentFromPictureBox;
- // sets the signup button
- PictureBox PBSignUp = new MyPictureBox("PBSignUp", "SignUp", new Size(PBSignIn.Width, sizeOfPictureBox));
- PBSignUp.Location = new Point(xOfPicBoxes, nextYPos);
- PBSignUp.Click += (EventHandler)(CleanAllControlers);
- PBSignUp.Click += (EventHandler)InitSignUp;
- cont.Add(PBSignUp);
- this.Controls.AddRange(cont.ToArray());
- }
- /// <summary>
- /// sets the form to be sign up page
- /// </summary>
- public void InitSignUp(object sender, EventArgs args)
- {
- int reminingY = this.Height;
- int nextYPos = 0;
- int xOfTextBoxes = 0;
- int sizeOfPictureBox = 0;
- int alligmentFromPictureBox = 0;
- int alligmentFromTextBoxes = 0;
- int alligmentFromTextBoxesToLabels = this.Width / LOG_IN_FORM_ALLGIMENT_FROM_TEXT_TO_LABEL;
- int xOfPicBoxes = 0;
- List<Control> cont = new List<Control>();
- // sets the background
- this.BackgroundImage = Properties.Resources.signUpBG;
- // sets the color of the letters
- this.ForeColor = Color.Black;
- // sets the subject label
- Label LBLSubject = new MyLabel("Please Sign Up", "LBLSubject"); // i create a label to know the width of its text
- LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
- cont.Add(LBLSubject);
- nextYPos = LBLSubject.Location.Y + alligmentFromSubjectY + LBLSubject.Height; // begining of details section
- reminingY -= nextYPos; // sets the space of details section
- // sets the text box of username
- TextBox TXTuserName = new MyTxtBox("TXTusername"); // i create a label to know the height of its font
- alligmentFromTextBoxes = ((reminingY / LOG_IN_FORM_TEXT_SECTOR_HEIGHT) - (TXTuserName.Height * 3)) / 4; // generates the needed alligment
- nextYPos += alligmentFromTextBoxes;
- reminingY -= reminingY / LOG_IN_FORM_TEXT_SECTOR_HEIGHT; // sets the space of buttons section
- TXTuserName.Location = new Point((this.Width / 2) - (TXTuserName.Width / 2), nextYPos);
- cont.Add(TXTuserName);
- // sets the label of username
- Label LBLusername = new MyLabel("UserName: ", "LBLusername"); // sets the label of the userName
- LBLusername.Location = new Point(TXTuserName.Location.X - LBLusername.Width - alligmentFromTextBoxesToLabels, nextYPos);
- cont.Add(LBLusername);
- nextYPos += (TXTuserName.Height + alligmentFromTextBoxes); // sets the position of the next textBox
- xOfTextBoxes = TXTuserName.Location.X;
- // sets the text box of password
- TextBox TXTpsw = new MyTxtBox(new Point(xOfTextBoxes, nextYPos), "TXTpsw");
- cont.Add(TXTpsw);
- // sets the label of password
- Label LBLpsw = new MyLabel("password: ", "LBLpsw"); // sets the password label
- LBLpsw.Location = new Point(xOfTextBoxes - LBLpsw.Width - alligmentFromTextBoxesToLabels, nextYPos);
- cont.Add(LBLpsw);
- nextYPos += (TXTuserName.Height + alligmentFromTextBoxes); // sets the position of the next textBox
- // sets the text box of email
- TextBox TXTemail = new MyTxtBox(new Point(xOfTextBoxes, nextYPos), "TXTemail");
- cont.Add(TXTemail);
- // sets the label of email
- Label LBLemail = new MyLabel("email: ", "LBLemail");
- LBLemail.Location = new Point(xOfTextBoxes - LBLemail.Width - alligmentFromTextBoxesToLabels, nextYPos);
- cont.Add(LBLemail);
- nextYPos += (TXTuserName.Height + alligmentFromTextBoxes); // sets the begining of the buttons sector
- // sets the buttons section
- sizeOfPictureBox = reminingY / LOG_IN_FORM_PICBOX_HEIGHT;
- reminingY -= (sizeOfPictureBox * 2);
- alligmentFromPictureBox = reminingY / 2;
- // sets the sign up button
- PictureBox PBSignUp = new MyPictureBox("PBSignUp", "SignUp", new Size(TXTemail.Width, sizeOfPictureBox));
- xOfPicBoxes = (this.Width / 2) - (PBSignUp.Width / 2); // sets the x of picture boxes
- PBSignUp.Location = new Point(xOfPicBoxes, nextYPos);
- PBSignUp.Click += (EventHandler)PBSign_Up_Click;
- //PBSignUp.Click += (EventHandler)CleanAllControlers;
- cont.Add(PBSignUp);
- // sets the back button
- int PCHeight = this.Height / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
- int PCWidth = this.Width / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
- PictureBox PBBack = new MyPictureBox("PBBack", "back", new Size(PCWidth, PCHeight));
- PBBack.Location = new Point(this.Width - PCWidth, this.Height / ALLIGMENT_FROM_TOP);
- PBBack.Click += (EventHandler)CleanAllControlers;
- PBBack.Click += (EventHandler)InitLoginScreen;
- cont.Add(PBBack);
- this.Controls.AddRange(cont.ToArray());
- }
- /// <summary>
- /// builds the main screen
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="args"></param>
- public void InitMainScreen(object sender, EventArgs args)
- {
- int nextYPos = 0;
- int reminingY = PBClose.Location.Y;
- int heightOfPB = this.Height / MAIN_FORM_PICBOX_HEIGHT;
- int widthOfPB = this.Width / MAIN_FORM_PICBOX_WIDTH;
- int alligmentFromButtonsY = 0;
- int alligmentFromButtonsX = 0;
- // sets the background of the screen
- this.BackgroundImage = Properties.Resources.mainScreenBG;
- // sets the font color to black
- this.ForeColor = Color.Black;
- // sets the subject label
- Label LBLSubject = new MyLabel("Hello " + uid, "LBLSubject"); // i create a label to know the width of its text
- LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
- this.Controls.Add(LBLSubject);
- // configures all the propeties needed for the button
- nextYPos = LBLSubject.Location.Y + alligmentFromSubjectY;
- reminingY -= (nextYPos - heightOfPB * 2);
- alligmentFromButtonsY = reminingY / 3;
- nextYPos += (alligmentFromButtonsY / 2);
- alligmentFromButtonsX = (this.Width - widthOfPB * 3) / 3;
- // sets the Join room button
- Button BTNJoin = new MyButton("BTNJoin", new Size(widthOfPB, heightOfPB), "Join Room");
- BTNJoin.Location = new Point(alligmentFromButtonsX, nextYPos);
- this.Controls.Add(BTNJoin);
- // sets the Create room button
- Button BTNCreate = new MyButton("BTNCreate", new Size(widthOfPB, heightOfPB), "Create Room");
- BTNCreate.Location = new Point(this.Width - widthOfPB - alligmentFromButtonsX, nextYPos);
- this.Controls.Add(BTNCreate);
- nextYPos += alligmentFromButtonsY;
- // sets the best scores button
- Button BTNBest = new MyButton("BTNBest", new Size(widthOfPB, heightOfPB), "Best Scores");
- BTNBest.Location = new Point(alligmentFromButtonsX, nextYPos);
- BTNBest.Click += (EventHandler)CleanAllControlers;
- BTNBest.Click += (EventHandler)InitBestScores;
- BTNBest.Click += (EventHandler)BTNBest_Click;
- this.Controls.Add(BTNBest);
- // sets the personal status
- Button BTNPersonal = new MyButton("BTNPersonal", new Size(widthOfPB, heightOfPB), "My Status");
- BTNPersonal.Location = new Point(this.Width - widthOfPB - alligmentFromButtonsX, nextYPos);
- BTNPersonal.Click += (EventHandler)CleanAllControlers;
- BTNPersonal.Click += (EventHandler)InitPersonalStatus;
- this.Controls.Add(BTNPersonal);
- // sets the logout button
- PictureBox PBLogOut = new MyPictureBox("PBLogOut", "signOut", new Size(this.Height / MAIN_FORM_LOG_OUT_WIDTH, this.Width / MAIN_FORM_LOG_OUT_HEIGHT));
- PBLogOut.Location = new Point(this.Width / ALLIGMENT_FROM_LEFT, this.Height / ALLIGMENT_FROM_TOP);
- PBLogOut.Click += (EventHandler)CleanAllControlers;
- PBLogOut.Click += (EventHandler)InitLoginScreen;
- PBLogOut.Click += (EventHandler)PBLogOut_Click;
- this.Controls.Add(PBLogOut);
- }
- /// <summary>
- /// inits the join room screen
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="args"></param>
- private void InitJoinRoom(object sender, EventArgs args)
- {
- int nextYPos = 0;
- int alligmentFromClose = this.Height - this.PBClose.Location.Y - this.PBClose.Height;
- int alligmentFromButtonsY = 0;
- int alligmentFromButtonsX = 0;
- int buttonSectorY = 0;
- int alligmentFromListsX = 0;
- int refreshY = 0;
- int joinY = 0;
- int reminingY = this.PBClose.Location.Y;
- // sets the form to join room bg
- this.BackgroundImage = Properties.Resources.joinRoomBG;
- // changes the fore color to black
- this.ForeColor = Color.Black;
- // sets the subject label
- Label LBLSubject = new MyLabel("Choose Room", "LBLSubject"); // i create a label to know the width of its text
- LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
- this.Controls.Add(LBLSubject);
- nextYPos = LBLSubject.Location.Y + LBLSubject.Height + alligmentFromSubjectY;
- reminingY -= nextYPos;
- // configures the button sector properties
- buttonSectorY = reminingY / JOIN_ROOM_FORM_BUTTON_SECTOR_Y;
- refreshY = buttonSectorY / JOIN_ROOM_FORM_REFRESH_HEIGHT;
- joinY = buttonSectorY / JOIN_ROOM_FORM_JOIN_HEIGHT;
- alligmentFromButtonsY = (buttonSectorY - refreshY - joinY) / 2;
- alligmentFromButtonsX = (this.Width - this.Width / JOIN_ROOM_FORM_REFRESH_WIDTH - this.Width / JOIN_ROOM_FORM_JOIN_WIDTH) / 3;
- // configures and adds the refresh button
- PictureBox PBRefresh = new MyPictureBox("PBRefresh", "refresh", new Size(this.Width / JOIN_ROOM_FORM_REFRESH_WIDTH, refreshY));
- PBRefresh.Location = new Point(alligmentFromButtonsX, PBClose.Location.Y - alligmentFromButtonsY - refreshY);
- PBRefresh.Click += (EventHandler)PBRefresh_Click;
- this.Controls.Add(PBRefresh);
- // configures and adds the join button
- PictureBox PBJoin = new MyPictureBox("PBJoin", "joinBTN", new Size(this.Width / JOIN_ROOM_FORM_JOIN_WIDTH, this.Height / JOIN_ROOM_FORM_JOIN_HEIGHT));
- PBJoin.Location = new Point(this.Width - PBJoin.Width - alligmentFromButtonsX, PBRefresh.Location.Y);
- PBJoin.Click += (EventHandler)PBJoin_Click;
- this.Controls.Add(PBJoin);
- reminingY = PBRefresh.Location.Y - LBLSubject.Location.Y - LBLSubject.Height - alligmentFromSubjectY;
- ListBox LBRooms = new MyListBox("LBRooms", new Size(this.Width / JOIN_ROOM_FORM_ROOMS_LB_WIDTH, reminingY / 4));
- ListBox LBUsers = new MyListBox("LBUsers", new Size(this.Width / JOIN_ROOM_FORM_USERS_LB_WIDTH, reminingY / 4));
- alligmentFromListsX = (this.Width - LBRooms.Width - LBUsers.Width) / 3;
- Label LBLRooms = new MyLabel("Rooms:", "LBLRooms");
- Label LBLUsers = new MyLabel("Users:", "LBLUsers");
- // adds the rooms list box
- LBRooms.Location = new Point(this.Width / 2 - LBRooms.Width / 2, nextYPos + LBLRooms.Height);
- this.Controls.Add(LBRooms);
- // sets the users list box
- LBUsers.Location = new Point(alligmentFromListsX, nextYPos + LBLUsers.Height);
- LBUsers.Visible = false;
- this.Controls.Add((LBUsers));
- // configures and adds the label of rooms
- LBLRooms.Location = new Point(LBRooms.Location.X + LBRooms.Width / 2 - LBLRooms.Width / 2, nextYPos);
- this.Controls.Add(LBLRooms);
- // configures and adds the label of users
- LBLUsers.Location = new Point(LBUsers.Location.X + LBUsers.Width / 2 - LBLRooms.Width / 2, nextYPos);
- LBLUsers.Visible = false;
- this.Controls.Add(LBLUsers);
- }
- /// <summary>
- /// changes the room list in the join room form
- /// should be called when the join room button or refresh button were clicked
- /// </summary>
- /// <param name="roomsNames">the names of the rooms that will be shown on the list box</param>
- private void changeRoomsList(List<string> roomsNames)
- {
- if (roomsNames.Count < 1) // so that the list will still be shown even if there are no available rooms
- {
- roomsNames.Add("");
- }
- ListBox LBUsers = this.Controls.Find("LBUsers", false).FirstOrDefault() as ListBox;
- Label LBLUsers = this.Controls.Find("LBLUsers", false).FirstOrDefault() as Label;
- // makes the users lists to disapear
- LBUsers.Visible = false;
- LBLUsers.Visible = false;
- ListBox LBRooms = this.Controls.Find("LBRooms", false).FirstOrDefault() as ListBox;
- if (LBRooms != null)
- {
- LBRooms.Items.Clear();
- foreach (string name in roomsNames)
- {
- LBRooms.Items.Add(name);
- }
- Label LBLRooms = this.Controls.Find("LBLRooms", false).FirstOrDefault() as Label;
- PictureBox PBRefresh = this.Controls.Find("PBRefresh", false).FirstOrDefault() as PictureBox;
- // sets the height of the list box
- if (LBLRooms != null && PBRefresh != null)
- {
- int maxHeight = PBRefresh.Location.Y - LBLRooms.Location.Y - LBLRooms.Height - alligmentFromSubjectY;
- int height = TextRenderer.MeasureText("k", LBRooms.Font).Height;
- int maxObj = maxHeight / height;
- LBRooms.Height = roomsNames.Count > maxObj ? height * maxObj : roomsNames.Count * height;
- }
- // moves the list box to its proper place
- LBRooms.Location = new Point(this.Width / 2 - LBRooms.Width / 2, LBRooms.Location.Y);
- LBLRooms.Location = new Point(LBRooms.Location.X + LBRooms.Width / 2 - LBLRooms.Width / 2, LBLRooms.Location.Y);
- this.Update();
- }
- }
- /// <summary>
- /// changes the users in room list box
- /// should be called when an item on the rooms list is clicked
- /// </summary>
- /// <param name="users">the users that will be presented on the users list</param>
- private void changeUsersInRoom(List<string> users)
- {
- if(users.Count < 1)
- {
- users.Add("");
- }
- ListBox LBUsers = this.Controls.Find("LBUsers", false).FirstOrDefault() as ListBox;
- Label LBLUsers = this.Controls.Find("LBLUsers", false).FirstOrDefault() as Label;
- Label LBLRooms = this.Controls.Find("LBLRooms", false).FirstOrDefault() as Label;
- PictureBox PBRefresh = this.Controls.Find("PBRefresh", false).FirstOrDefault() as PictureBox;
- ListBox LBRooms = this.Controls.Find("LBRooms", false).FirstOrDefault() as ListBox;
- // sets the height of the list box
- if (LBLUsers != null && PBRefresh != null && LBUsers != null)
- {
- int maxHeight = PBRefresh.Location.Y - LBLUsers.Location.Y - LBLUsers.Height - alligmentFromSubjectY;
- int height = TextRenderer.MeasureText("k", LBUsers.Font).Height;
- int maxObj = maxHeight / height;
- // changes the items in the list box
- LBUsers.Items.Clear();
- foreach(string user in users)
- {
- LBUsers.Items.Add(user);
- }
- LBUsers.Height = users.Count > maxObj ? height * maxObj : users.Count * height;
- // sets the rooms stuff to their proper place
- LBRooms.Location = new Point(this.Width - LBRooms.Width - LBUsers.Location.X, LBRooms.Location.Y);
- LBLRooms.Location = new Point(LBRooms.Location.X + LBRooms.Width / 2 - LBLRooms.Width / 2, LBLRooms.Location.Y);
- LBUsers.Visible = true;
- LBLUsers.Visible = true;
- this.Update();
- }
- }
- /// <summary>
- /// changes the form to personal status
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="args"></param>
- private void InitPersonalStatus(object sender, EventArgs args)
- {
- int reminingY = this.PBClose.Location.Y;
- int nextYPos = 0;
- int alligmentFromLabelsY = 0;
- // sets the background of the form to personal status background
- this.BackgroundImage = Properties.Resources.personalStatusBG;
- // sets the font of the form to black
- this.ForeColor = Color.Black;
- // sets the subject label
- Label LBLSubject = new MyLabel("My Performance: ", "LBLSubject"); // i create a label to know the width of its text
- LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
- this.Controls.Add(LBLSubject);
- nextYPos = LBLSubject.Location.Y + LBLSubject.Height + alligmentFromSubjectY;
- reminingY -= nextYPos;
- Label LBLnumOfGames = new MyLabel("number of Games: ", "LBLnumOfGames"); // i create a label so i can know the height of it
- alligmentFromLabelsY = (reminingY - LBLnumOfGames.Height * 4) / 5;
- nextYPos += alligmentFromLabelsY;
- // configures and adds the number of games label
- LBLnumOfGames.Location = new Point(0, nextYPos); // i put 0 in x of all labels because i change it to the middle when the real text arrives from server
- this.Controls.Add(LBLnumOfGames);
- nextYPos += alligmentFromLabelsY;
- // configures and adds the number of right answers label
- Label LBLnumOfRightAns = new MyLabel("number of right answers: ", "LBLnumOfRightAns");
- LBLnumOfRightAns.Location = new Point(0, nextYPos);
- this.Controls.Add(LBLnumOfRightAns);
- nextYPos += alligmentFromLabelsY;
- // configures and adds the number of wrong answers label
- Label LBLnumOfWrongAns = new MyLabel("number of wrong answers: ", "LBLnumOfWrongAns");
- LBLnumOfWrongAns.Location = new Point(0, nextYPos);
- this.Controls.Add(LBLnumOfWrongAns);
- nextYPos += alligmentFromLabelsY;
- // configures and adds the average time per answer
- Label LBLavgTimePerAns = new MyLabel("average time for answer: ", "LBLavgTimePerAns");
- LBLavgTimePerAns.Location = new Point(0, nextYPos);
- this.Controls.Add(LBLavgTimePerAns);
- // sets the back button
- int PCHeight = this.Height / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
- int PCWidth = this.Width / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
- PictureBox PBBack = new MyPictureBox("PBBack", "back", new Size(PCWidth, PCHeight));
- PBBack.Location = new Point(this.Width - PCWidth, this.Height / ALLIGMENT_FROM_TOP);
- PBBack.Click += (EventHandler)CleanAllControlers;
- PBBack.Click += (EventHandler)InitMainScreen;
- this.Controls.Add(PBBack);
- }
- /// <summary>
- /// changes the labels of personal status' form, has to be be used after InitPersonalStatus function used
- /// </summary>
- /// <param name="arr">an array that contains the values of the fields example { 4, 5, 10, 3 }</param>
- private void ChangePersonalStatus(string[] arr)
- {
- if(arr.Length > 3)
- {
- Label[] labels = new Label[4];
- labels[0] = this.Controls.Find("LBLnumOfGames", false).FirstOrDefault() as Label;
- labels[1] = this.Controls.Find("LBLnumOfRightAns", false).FirstOrDefault() as Label;
- labels[2] = this.Controls.Find("LBLnumOfWrongAns", false).FirstOrDefault() as Label;
- labels[3] = this.Controls.Find("LBLavgTimePerAns", false).FirstOrDefault() as Label;
- for(int i = 0; i < 4; i++)
- {
- if(labels[i] != null)
- {
- labels[i].Text += arr[i];
- labels[i].Width = TextRenderer.MeasureText(labels[i].Text, labels[i].Font).Width;
- labels[i].Location = new Point(this.Width / 2 - labels[i].Width / 2, labels[i].Location.Y);
- }
- }
- }
- }
- /// <summary>
- /// changes the form to show the best scores
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="args"></param>
- private void InitBestScores(object sender, EventArgs args)
- {
- int reminingY = PBClose.Location.Y;
- int nextYPos = 0;
- int pictureBoxHeight = 0;
- int pictureBoxWidth = 0;
- int alligmentFromPicturesY = 0;
- int alligmentFromPictureX = 0;
- // sets the background image to best scores BG
- this.BackgroundImage = Properties.Resources.winners;
- // sets the font of the form to black
- this.ForeColor = Color.Black;
- // sets the subject label
- Label LBLSubject = new MyLabel("Best Scores: ", "LBLSubject"); // i create a label to know the width of its text
- LBLSubject.Location = new Point((this.Width / 2) - (LBLSubject.Width / 2), alligmentFromSubjectY);
- this.Controls.Add(LBLSubject);
- // creates the label that contains the first player's scores
- Label LBL1st = new MyLabel("ABCD", "LBL1st"); // i put text in the label so i can know its properties
- // sets the next position for the picture boxes sector
- nextYPos += (LBLSubject.Location.Y + LBLSubject.Height + alligmentFromSubjectY);
- reminingY -= nextYPos;
- // configures the properties of the picture boxes
- pictureBoxHeight = reminingY / BEST_SCORES_FORM_PB_HEIGHT;
- pictureBoxWidth = this.Width / BEST_SCORES_FORM_PB_WIDTH;
- // configures the picture box sector
- alligmentFromPicturesY = (reminingY - pictureBoxHeight * 2 - LBL1st.Height * 2) / 2;
- alligmentFromPictureX = (this.Width - pictureBoxWidth * 3) / 4;
- nextYPos += alligmentFromPicturesY;
- // sets the first place picture box
- PictureBox PB1st = new MyPictureBox("PB1st", "_1st", new Size(pictureBoxWidth, pictureBoxHeight));
- PB1st.Location = new Point(alligmentFromPictureX * 2 + pictureBoxWidth, nextYPos);
- this.Controls.Add(PB1st);
- LBL1st.Location = new Point(PB1st.Location.X + PB1st.Width / 2, nextYPos - LBL1st.Height);
- this.Controls.Add(LBL1st);
- // sets the postion to the next picture box
- nextYPos += alligmentFromPicturesY;
- // sets the second best score picture box
- PictureBox PB2nd = new MyPictureBox("PB2nd", "_2nd", new Size(pictureBoxWidth, pictureBoxHeight));
- PB2nd.Location = new Point(alligmentFromPictureX, nextYPos);
- this.Controls.Add(PB2nd);
- // sets the label of the second best score
- Label LBL2nd = new MyLabel("ABCD", "LBL2nd"); // i put text in the label so i can know its properties
- LBL2nd.Location = new Point(PB2nd.Location.X + PB2nd.Width / 2, PB2nd.Location.Y - LBL2nd.Height);
- this.Controls.Add(LBL2nd);
- // sets the picture box of the second best score
- PictureBox PB3rd = new MyPictureBox("PB3rd", "_3rd", new Size(pictureBoxWidth, pictureBoxHeight));
- PB3rd.Location = new Point(this.Width - alligmentFromPictureX - PB3rd.Width, nextYPos);
- this.Controls.Add(PB3rd);
- // sets the label of th ethird best score
- Label LBL3rd = new MyLabel("ABCD", "LBL3rd"); // i put text in the label so i can know its properties
- LBL3rd.Location = new Point(PB3rd.Location.X + PB3rd.Width / 2, nextYPos - LBL3rd.Height);
- this.Controls.Add(LBL3rd);
- // sets the back button
- int PCHeight = this.Height / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
- int PCWidth = this.Width / SIGN_OUT_FORM_BACK_HEIGHT_WIDTH;
- PictureBox PBBack = new MyPictureBox("PBBack", "back", new Size(PCWidth, PCHeight));
- PBBack.Location = new Point(this.Width - PCWidth, this.Height / ALLIGMENT_FROM_TOP);
- PBBack.Click += (EventHandler)CleanAllControlers;
- PBBack.Click += (EventHandler)InitMainScreen;
- this.Controls.Add(PBBack);
- }
- /// <summary>
- /// changes the best scores labels
- /// should be used when clicked on get best scores button, after InitBestScores was called
- /// </summary>
- /// <param name="arr">an array of pairs in which the key is the name of the user and the value is his score</param>
- private void changeBestScores(KeyValuePair<string, string>[] arr)
- {
- Label[] labels = new Label[3];
- labels[0] = this.Controls.Find("LBL1st", false).FirstOrDefault() as Label;
- labels[1] = this.Controls.Find("LBL2nd", false).FirstOrDefault() as Label;
- labels[2] = this.Controls.Find("LBL3rd", false).FirstOrDefault() as Label;
- for (int i = 0; i < 3; i++ )
- {
- if(labels[i] != null)
- {
- labels[i].Text = arr[i].Key + ": " + arr[i].Value;
- labels[i].Width = TextRenderer.MeasureText(labels[i].Text, labels[i].Font).Width;
- labels[i].Location = new Point(labels[i].Location.X - labels[i].Width / 2, labels[i].Location.Y);
- }
- }
- }
- /// <summary>
- /// cleans the screen from all the controls on it
- /// </summary>
- private void CleanAllControlers(object sender, EventArgs args)
- {
- //foreach (Control c in this.Controls)
- //{
- // MessageBox.Show(c.GetType().ToString());
- // this.Controls.Remove(c);
- //}
- this.Controls.Clear();
- this.Controls.Add(PBClose);
- }
- private void PBClose_Click(object sender, EventArgs args)
- {
- this.Close();
- }
- private void BTNBest_Click(object sender, EventArgs args)
- {
- // ... executed when best scores button clicked
- }
- private void PBLogOut_Click(object sender, EventArgs args)
- {
- // ... executed when log out button is clicked
- }
- private void PBRefresh_Click(object sender, EventArgs args)
- {
- // .. executed when refresh button is clicked
- }
- private void PBJoin_Click(object sender, EventArgs args)
- {
- //... executed when join button clicked (on join rooms form)
- }
- /// <summary>
- /// sends the username, password and email to server according to the format for sign up
- /// </summary>
- private void PBSign_Up_Click(object sender, EventArgs args)
- {
- TextBox txtUserName = (TextBox)this.Controls.Find("TXTusername", false)[0];
- TextBox txtPassword = (TextBox)this.Controls.Find("TXTpsw", false)[0];
- TextBox txtEmail = (TextBox)this.Controls.Find("TXTemail", false)[0];
- if (txtUserName != null && txtPassword != null && txtEmail != null)
- {
- this.stream.sendData(String.Format("203{0:00}{1}{2:00}{3}{4:00}{5}", txtUserName.Text.Length, txtUserName.Text, txtPassword.Text.Length, txtPassword.Text, txtEmail.Text.Length, txtEmail.Text));
- }
- }
- /// <summary>
- /// sends the username and password to server according to the format for sign in
- /// </summary>
- private void PBSign_In_Click(object sender, EventArgs e)
- {
- TextBox txtUserName = (TextBox)this.Controls.Find("TXTusername", false)[0];
- TextBox txtPassword = (TextBox)this.Controls.Find("TXTpsw", false)[0];
- if (txtUserName != null && txtPassword != null)
- {
- this.stream.sendData(String.Format("200{0:00}{1}{2:00}{3}", txtUserName.Text.Length, txtUserName.Text, txtPassword.Text.Length, txtPassword.Text));
- }
- }
- private void handleMessage()
- {
- string msg = "";
- while (true)
- {
- msg = this.stream.recvData();
- switch (int.Parse(msg))
- {
- case 1020:
- MessageBox.Show("Success Sign in");
- uid = ((TextBox)this.Controls.Find("TXTusername", false)[0]).Text;
- Invoke((MethodInvoker) (() => CleanAllControlers(null, EventArgs.Empty)));
- Invoke((MethodInvoker) (() => InitMainScreen(null, EventArgs.Empty)));
- break;
- case 1021:
- MessageBox.Show("Wrong Details Sign in");
- break;
- case 1022:
- MessageBox.Show("User is already connected Sign in");
- break;
- case 1040:
- MessageBox.Show("Success Sign up");
- Invoke((MethodInvoker) (() => CleanAllControlers(null, EventArgs.Empty)));
- Invoke((MethodInvoker) (() => this.InitLoginScreen(null, EventArgs.Empty)));
- break;
- case 1041:
- MessageBox.Show("WPass illegal Sign up");
- break;
- case 1042:
- MessageBox.Show("Username is already exists Sign up");
- break;
- case 1043:
- MessageBox.Show("Username is illegal Sign up");
- break;
- case 1044:
- MessageBox.Show("Other Sign up");
- break;
- }
- }
- }
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- //this.t.Abort();
- //this.stream.sendData("299");
- //this.stream.CloseStream();
- this.PBClose.Dispose();
- }
- }
- }
- // TODO:
- // set font according to resulution
- // remove the size of the Close button from the login and signup pages
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement