Ejemplo n.º 1
0
        // Event - Send Message - Click - Asynchronously send message to all the users in collection
        async private void UI_btn_SendMessage_Click(object sender, EventArgs e)
        {
            // Checking if textbox is emtpy or in placeholder state
            if (UI_tb_MessageOut.Text.Length == 0 || UI_tb_MessageOut.ForeColor == Color.Gray)
            {
                return;
            }

            // Grabbing input
            string username = "";
            string message  = UI_tb_MessageOut.Text;

            // Check if a username is to be embedded in message
            if (UI_tb_Username.Text.Length != 0 && UI_tb_Username.ForeColor != Color.Gray)
            {
                username = $"<{UI_tb_Username.Text}> ";
            }

            // Converting package into byte array to send out
            byte[] messageOut = Encoding.Unicode.GetBytes(username + message);

            // Asynchronously send message to all the users in collection
            await Task.Run(() =>
            {
                // Itterate through user collection and grab IP addresses
                (from i in users select new { i.Key }).ToList().ForEach(x =>
                {
                    try
                    {
                        // Sending message to isolated IP address
                        Task <int> send = clientSocket.SendAsync(messageOut, messageOut.Length, x.Key, 1666);
                        Console.WriteLine($"Bytes Sent: {send.Result}");
                    }
                    catch (Exception exception)
                    {
                        Console.WriteLine($"Invalid IP address at {users[x.Key]} : " + exception.Message.ToString());
                    }
                });
            });

            // After sucessful send, clear message box and regain focus
            UI_tb_MessageOut.Clear();
            UI_tb_MessageOut.Focus();
        }
Ejemplo n.º 2
0
        // Event - Textbox - Enter - Changes textbox format state from placeholder
        private void Textbox_Enter(object sender, EventArgs e)
        {
            TextBox t = (TextBox)sender;

            // Depending on event sender, changing font color to black and clearing placeholder text
            switch (t.Name)
            {
            case "UI_tb_Username":
            {
                if (UI_tb_Username.ForeColor == Color.Gray)
                {
                    UI_tb_Username.Clear();
                    UI_tb_Username.ForeColor = Color.Black;
                }
            }
            break;

            case "UI_tb_MessageOut":
            {
                if (UI_tb_MessageOut.ForeColor == Color.Gray)
                {
                    UI_tb_MessageOut.Clear();
                    UI_tb_MessageOut.ForeColor = Color.Black;
                }
            }
            break;

            case "UI_tb_IPAddress":
            {
                if (UI_tb_IPAddress.ForeColor == Color.Gray)
                {
                    UI_tb_IPAddress.Clear();
                    UI_tb_IPAddress.ForeColor = Color.Black;
                }
            }
            break;
            }
        }