private void button_send_Click(object sender, EventArgs e) { try { if (TextBox_send.Text == "") { MessageBox.Show("发送内容不能为空", "操作错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } string text = TextBox_send.Text; while (is_sending) { } ; is_sending = true; TextBox_chat.SelectionAlignment = HorizontalAlignment.Right; string whole = Aliases[0] + "(" + members[0] + ")" + " " + DateTime.Now.ToString() + "\n" + text + "\n"; TextBox_chat.AppendText(whole); whole = text + "\n"; is_sending = false; TextBox_send.Clear(); foreach (Socket sc in all_sockets) { AsynSend(sc, whole); } } catch (Exception) { MessageBox.Show("连接失效", "网络错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } }
private void ChangeFontStyle(FontStyle style) { if (style != FontStyle.Bold && style != FontStyle.Italic && style != FontStyle.Underline) { throw new System.InvalidProgramException("字体格式错误"); } RichTextBox tempRichTextBox = new RichTextBox(); //将要存放被选中文本的副本 int curRtbStart = TextBox_send.SelectionStart; int len = TextBox_send.SelectionLength; int tempRtbStart = 0; Font font = TextBox_send.SelectionFont; if (len <= 1 && font != null) //与上边的那段代码类似,功能相同 { if (style == FontStyle.Bold && font.Bold || style == FontStyle.Italic && font.Italic || style == FontStyle.Underline && font.Underline) { TextBox_send.SelectionFont = new Font(font, font.Style ^ style); } else if (style == FontStyle.Bold && !font.Bold || style == FontStyle.Italic && !font.Italic || style == FontStyle.Underline && !font.Underline) { TextBox_send.SelectionFont = new Font(font, font.Style | style); } return; } tempRichTextBox.Rtf = TextBox_send.SelectedRtf; tempRichTextBox.Select(len - 1, 1); //选中副本中的最后一个文字 //克隆被选中的文字Font,这个tempFont主要是用来判断 //最终被选中的文字是否要加粗、去粗、斜体、去斜、下划线、去下划线 Font tempFont = (Font)tempRichTextBox.SelectionFont.Clone(); //清空2和3 for (int i = 0; i < len; i++) { tempRichTextBox.Select(tempRtbStart + i, 1); //每次选中一个,逐个进行加粗或去粗 if (style == FontStyle.Bold && tempFont.Bold || style == FontStyle.Italic && tempFont.Italic || style == FontStyle.Underline && tempFont.Underline) { tempRichTextBox.SelectionFont = new Font(tempRichTextBox.SelectionFont, tempRichTextBox.SelectionFont.Style ^ style); } else if (style == FontStyle.Bold && !tempFont.Bold || style == FontStyle.Italic && !tempFont.Italic || style == FontStyle.Underline && !tempFont.Underline) { tempRichTextBox.SelectionFont = new Font(tempRichTextBox.SelectionFont, tempRichTextBox.SelectionFont.Style | style); } } tempRichTextBox.Select(tempRtbStart, len); TextBox_send.SelectedRtf = tempRichTextBox.SelectedRtf; //将设置格式后的副本拷贝给原型 TextBox_send.Select(curRtbStart, len); }
// 清空输入栏 private void button_clear_Click(object sender, EventArgs e) { TextBox_send.Clear(); }