private void deleteClick(object sender, EventArgs e) // remove client from list, this function is triggered from "DELETE" button
        {
            if (connected)
            {
                connected = false;
                AsynchronousServer.ServerSetControlPropertyThreadSafe(AsynchronousServer.console, "Text", AsynchronousServer.console.Text + "Client " + index + " (" + key + "): Lost connection" + "  (" + index + ")" + "\n");
                AsynchronousServer.ServerSetControlPropertyThreadSafe(AsynchronousServer.console, "Text", AsynchronousServer.console.Text + "Client " + index + " (" + key + "): Deleted" + "  (" + index + ")" + "\n");
                AsynchronousServer.clients.Remove(this.key);
                client.Shutdown(SocketShutdown.Both);
                client.Close();
                TestFormCotrolHelper.ControlInvike(AsynchronousServer.list, () => AsynchronousServer.list.Controls.Remove(itemPanel));
            }
            else
            {
                AsynchronousServer.clients.Remove(this.key);
                TestFormCotrolHelper.ControlInvike(AsynchronousServer.list, () => AsynchronousServer.list.Controls.Remove(itemPanel));
                AsynchronousServer.ServerSetControlPropertyThreadSafe(AsynchronousServer.console, "Text", AsynchronousServer.console.Text + "Client " + index + " (" + key + "): Deleted" + "  (" + index + ")" + "\n");
            }
            AsynchronousServer.StoreData();

            Console.WriteLine("My Index: " + this.index);
            foreach (KeyValuePair <string, Client> item in AsynchronousServer.clients) //move all items below this with 25 units upper, for not leaving blank space in list
            {
                Console.WriteLine("item: " + item.Value.index);
                if (item.Value.index > this.index)
                {
                    item.Value.itemPanel.Location = new System.Drawing.Point(0, item.Value.itemPanel.Location.Y - 25);
                }
            }
        }
Example #2
0
        public static void ClientSetControlPropertyThreadSafe(
            Control control,
            string propertyName,
            object propertyValue)
        {
            if (control.InvokeRequired)
            {
                control.Invoke(new ClientSetControlPropertyThreadSafeDelegate
                                   (ClientSetControlPropertyThreadSafe),
                               new object[] { control, propertyName, propertyValue });
            }
            else
            {
                control.GetType().InvokeMember(
                    propertyName,
                    BindingFlags.SetProperty,
                    null,
                    control,
                    new object[] { propertyValue });
            }

            //get new console height and set scroll to bottom, using helper class to do this, because this is usually called from another thread (listenThreadClass)
            TestFormCotrolHelper.ControlInvike(consoleContainer, () => consoleContainer.VerticalScroll.Maximum = console.Size.Height + 20);
            TestFormCotrolHelper.ControlInvike(consoleContainer, () => consoleContainer.VerticalScroll.Value   = consoleContainer.VerticalScroll.Maximum);
        }
        public void FillList() //render current client in all clients list
        {
            itemPanel  = new System.Windows.Forms.Panel();
            itemIndex  = new System.Windows.Forms.Label();
            itemAdress = new System.Windows.Forms.Label();
            itemStatus = new System.Windows.Forms.PictureBox();
            itemDelete = new System.Windows.Forms.Button();

            itemPanel.SuspendLayout();

            itemPanel.ResumeLayout(false);
            itemPanel.PerformLayout();

            Console.WriteLine("FillList : " + AsynchronousServer.clients.Count + "   key: " + this.key);

            itemPanel.Location = new System.Drawing.Point(0, 25 * AsynchronousServer.clients.Count + 5);
            itemPanel.Size     = new System.Drawing.Size(250, 25);

            itemIndex.Location  = new System.Drawing.Point(0, 0);
            itemIndex.Size      = new System.Drawing.Size(25, 25);
            itemIndex.Text      = index.ToString();
            itemIndex.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // itemIndex.Click += new System.EventHandler(this.label3_Click);

            itemStatus.Location      = new System.Drawing.Point(25, 0);
            itemStatus.Size          = new System.Drawing.Size(25, 25);
            itemStatus.ImageLocation = "red.bmp";
            itemStatus.BackColor     = System.Drawing.Color.Gray;

            itemAdress.Location  = new System.Drawing.Point(50, 0);
            itemAdress.Size      = new System.Drawing.Size(125, 25);
            itemAdress.Text      = this.key;
            itemAdress.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;

            itemDelete.Location  = new System.Drawing.Point(175);
            itemDelete.Size      = new System.Drawing.Size(75, 25);
            itemDelete.Text      = "DELETE";
            itemDelete.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            itemDelete.Click    += new System.EventHandler(deleteClick); //set function to "DELETE" click

            itemPanel.Controls.Add(itemIndex);
            itemPanel.Controls.Add(itemStatus);
            itemPanel.Controls.Add(itemAdress);
            itemPanel.Controls.Add(itemDelete);

            //add item using helper method, because it is usually called from another thread
            TestFormCotrolHelper.ControlInvike(AsynchronousServer.list, () => AsynchronousServer.list.Controls.Add(itemPanel));
        }