// File => Disconnect private void DisconnectToolStripMenuItem_Click(object sender, EventArgs e) { if (!ClientNetworkEngine.Status) { return; } foreach (TabPage tabPage in TabControlClient.TabPages) { if (TabPagePrivateChatReceiveClientEvent != null) { TabPagePrivateChatReceiveClientEvent.Invoke(tabPage.Name, null, null, TabPagePrivateChatClient.TabCommand.Disconnected); } } ClientNetworkEngine.Disconnect(); ClientStatistics.Stop(); Thread.Sleep(50); BtnPubSnd.Enabled = false; Text = @"Chat: " + Client.Name + @"[Disconnected]"; RichTextClientPub.SelectionStart = _CursorPosition; RichTextClientPub.SelectionColor = Color.Black; RichTextClientPub.SelectionBackColor = Color.Crimson; RichTextClientPub.SelectedText = @"You are disonnected now " + Environment.NewLine; _CursorPosition = RichTextClientPub.SelectionStart; ListBoxClientList.Items.Clear(); _ClientChatHistoryList.Clear(); }
//Send private Photo method event private void TabPagePrivateChatSendImageClient(string namePrivate) { try { using (OpenFileDialog openFileDialog = new OpenFileDialog()) { openFileDialog.Title = @"Open ImageMessage"; openFileDialog.Filter = @"Images|*.png;*.bmp;*.jpg;*.gif*"; if (openFileDialog.ShowDialog() != DialogResult.OK) { return; } long fileSize = new FileInfo(openFileDialog.FileName).Length; if (fileSize > 2097152) { MessageBox.Show(@"This file is too big, you can send files no larger than 3MB", @"Chat: " + Client.Name, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } ClientNetworkEngine.SendImagePrivate(File.ReadAllBytes(openFileDialog.FileName), Client.Name, namePrivate); } } catch (Exception ex) { MessageBox.Show(ex.Message + @" -> BtnColorPick_Click", @"Chat: " + Client.Name, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
// Register button private void btnRegister_Click(object sender, EventArgs e) { ClientNetworkEngine.Register(txtBoxRegUserName.Text, txtBoxServerIp.Text, int.Parse(txtBoxPort.Text)); btnConnect.Enabled = false; btnColorPick.Enabled = false; txtBoxPort.Enabled = false; txtBoxName.Enabled = false; txtBoxServerIp.Enabled = false; }
// Send private message private void TabPagePrivateChatSendClient(string clientNamePrivate, string message) { ClientNetworkEngine.SendPrivateMessage(clientNamePrivate, message); ++ClientStatistics.MessagesPrivateSent; if (FrmStatisticsUpdateEvent != null) { FrmStatisticsUpdateEvent.Invoke(StatisticsEntry.MessagePrivateSent); } }
// Button connect private void btnConnect_Click(object sender, EventArgs e) { btnConnect.Enabled = false; btnColorPick.Enabled = false; txtBoxPort.Enabled = false; txtBoxName.Enabled = false; txtBoxServerIp.Enabled = false; ClientNetworkEngine.AttemptConnect(txtBoxServerIp.Text, int.Parse(txtBoxPort.Text), txtBoxName.Text, txtBoxUserName.Text, Client.Color); //ClientConnection.Connect(txtBoxServerIp.Text, int.Parse(txtBoxPort.Text), txtBoxName.Text); }
// Form closing event private void FrmChat_FormClosing(object sender, FormClosingEventArgs e) { if (!ClientNetworkEngine.Status) { return; } if (MessageBox.Show(@"Are you sure you want to exit?", @"Chat: " + Client.Name, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.No) { return; } ClientNetworkEngine.Disconnect(); }
// File => Reconnect private void ReconnectToolStripMenuItem_Click(object sender, EventArgs e) { if (ClientNetworkEngine.Status) { return; } ClientNetworkEngine.Reconnect(); Thread.Sleep(5); //FrmChat_Load(this, null); BtnPubSnd.Enabled = true; Text = @"Chat: " + Client.Name; }
// Color Button private void BtnColorPick_Click(object sender, EventArgs e) { if (!ClientNetworkEngine.Status) { return; } DialogResult pickColor = ColorPicker.ShowDialog(); if (pickColor != DialogResult.OK) { return; } ClientNetworkEngine.ChangeColor(ColorPicker.Color); Client.Color = ColorPicker.Color; }
// List box Clients double click private void ListBoxClientList_DoubleClick(object sender, EventArgs e) { if (ListBoxClientList.SelectedItem.ToString() == Client.Name) { MessageBox.Show(@"You can't start a private chat with yourself", @"Chat: " + Client.Name, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (TabControlClient.TabPages.OfType <TabPagePrivateChatClient>().Any(tabPagePrivateChat => tabPagePrivateChat.Name == ListBoxClientList.SelectedItem.ToString())) { MessageBox.Show(@"That private chat already opned", @"Chat: " + Client.Name, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } ClientNetworkEngine.StartPrivateChat(ListBoxClientList.SelectedItem.ToString()); NewTabPagePrivateChatClient(ListBoxClientList.SelectedItem.ToString()); TabFormat.ItemEvenSize(TabControlClient); }
// Public Chat Send button private void BtnSend_Click(object sender, EventArgs e) { if (TextBoxPubMsg.Text.Length <= 0) { return; } ClientNetworkEngine.SendMessage(TextBoxPubMsg.Text); // Reset the TextBoxPubMsg TextBoxPubMsg.Text = null; // Update statistics ++ClientStatistics.MessagesSent; if (FrmStatisticsUpdateEvent != null) { FrmStatisticsUpdateEvent.Invoke(StatisticsEntry.MessageSent); } }
// Chat => Change Name private void ChangeNameToolStripMenuItem_Click(object sender, EventArgs e) { if (!ClientNetworkEngine.Status) { return; } using (ChangeName changeName = new ChangeName(Client.Name)) { if (changeName.ShowDialog() != DialogResult.OK) { return; } if (ListBoxClientList.Items.Cast <object>().Any(item => changeName.NameNew == item.ToString())) { MessageBox.Show(@"The name " + changeName.NameNew + @"already taken", @"Chat: 5" + Client.Name, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } ClientNetworkEngine.NameChange(changeName.NameNew); } }
// Click event on TabPage, checks whenever the click was in the X rectangle area private void TabControlClient_MouseClick(object sender, MouseEventArgs e) { for (int i = 1; i < TabControlClient.TabPages.Count; i++) { Rectangle tabRect = TabControlClient.GetTabRect(i); //Getting the position of the "x" mark. //Rectangle tabXarea = new Rectangle(tabRect.Right - TabControlClient.TabPages[i].Text.Length, tabRect.Top, 9, 7); Rectangle closeXButtonArea = new Rectangle(tabRect.Right - 23, 6, 16, 16); //Rectangle closeButton = new Rectangle(tabRect.Right - 13, tabRect.Top + 6, 9, 7); if (!closeXButtonArea.Contains(e.Location)) { continue; } if (MessageBox.Show(@"Would you like to Close this Tab?", @"Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) { continue; } ClientNetworkEngine.PrivateClose(TabControlClient.TabPages[i].Name); TabControlClient.TabPages.RemoveAt(i); break; } }
// Button Cancel private void btnCancel_Click(object sender, EventArgs e) { Close(); DialogResult = DialogResult.Cancel; ClientNetworkEngine.Disconnect(); }