Exemplo n.º 1
0
        /// <summary>
        /// Connects to chat server if chat client is not connected else disconnect the chat client.
        /// </summary>
        private async void ConnectChatClient()
        {
            if (chatClient.Connected)
            {
                chatClient.Close();
            }
            else
            {
                pnlChat.Enabled = false;

                if (IPAddress.TryParse(txtIPString.Text, out IPAddress address))
                {
                    try
                    {
                        if (await chatClient.Connect(txtUsername.Text, address))
                        {
                            btnConnect.Text     = "Disconnect";
                            btnSend.Enabled     = true;
                            txtIPString.Enabled = false;
                            txtUsername.Enabled = false;
                            txtMessage.Enabled  = true;
                        }
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.Message);
                    }
                }

                pnlChat.Enabled = true;
            }
        }