private void AddMessage(string value) { this.InvokeAction(() => { MessagesTextBox.AppendText(value + "\r\n"); }); }
private void RenderMsg(string msg) { PrintRBot(); MessagesTextBox.AppendText(msg + "\r\n\r\n"); MessagesTextBox.ScrollToCaret(); //позволяет прокрутить окно чата до конца QuestionTextBox.Clear(); }
private void DisplayError(string text) { if (InvokeRequired) { // Invoke UI updates on the UI thread BeginInvoke(new Action <string>(DisplayError), text); return; } // Start selection at the end of the text box // in order to set the color of the appended text MessagesTextBox.SelectionStart = MessagesTextBox.TextLength; MessagesTextBox.SelectionLength = 0; // Append text to the text box MessagesTextBox.SelectionColor = Color.Red; MessagesTextBox.AppendText(text + "\n"); MessagesTextBox.SelectionColor = ForeColor; // Truncate old messages when the text // exceeds the maximum number of lines MessagesTextBox.SelectionStart = 0; MessagesTextBox.SelectionLength = MessagesTextBox.Lines .Take(MessagesTextBox.Lines.Length - m_maxLines) .Aggregate(0, (length, line) => length + line.Length + "\n".Length); MessagesTextBox.ReadOnly = false; MessagesTextBox.SelectedText = ""; MessagesTextBox.ReadOnly = true; // Scroll to bottom MessagesTextBox.SelectionStart = MessagesTextBox.TextLength; MessagesTextBox.ScrollToCaret(); }
// ЗАГРУЗИТЬ ИСТОРИЮ БОТА private void DownloadBotToolStripMenuItem_Click(object sender, EventArgs e) { try { using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*", Multiselect = false, ValidateNames = true }) { if (ofd.ShowDialog() == DialogResult.OK) { string filename = ofd.FileName; string file = System.IO.File.ReadAllText(filename); MessagesTextBox.Text = file; } else { MessagesTextBox.AppendText("Не удалось загрузить историю"); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void MessagesOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs args) { if (args.Action == NotifyCollectionChangedAction.Add) { foreach (string message in args.NewItems.OfType <string>()) { MessagesTextBox.AppendText(message + Environment.NewLine); } Window window = Window.GetWindow(this); if (window == null) { return; } if (window.IsActive && window.WindowState != WindowState.Minimized) { MessagesTextBox.ScrollToEnd(); return; } Task.Run(new Action(FlashWhileMinimized)); window.StartFlashing(); } }
//метод, распечатывающий имя бота синим цветом перед сообщением private void PrintRBot() { var startOfSelection = MessagesTextBox.TextLength; MessagesTextBox.AppendText("R-Bot: "); MessagesTextBox.Select(startOfSelection, "R-Bot:".Length); MessagesTextBox.SelectionColor = Color.Blue; }
//метод, распечатывающий имя бота синим цветом перед сообщением private void PrintRBot() { DateTime date = DateTime.Now; string outputSunny = date.ToString("[dd/MM/yyyy HH:mm:ss] ") + "Лучик: "; var startOfSelection = MessagesTextBox.TextLength; MessagesTextBox.AppendText(outputSunny); MessagesTextBox.Select(startOfSelection, outputSunny.Length); MessagesTextBox.SelectionColor = Color.Blue; }
//метод, отобращающий пользовательское сообщение private void ShowUserMessage() { var startOfSelection = MessagesTextBox.TextLength; MessagesTextBox.AppendText("Вы: "); MessagesTextBox.Select(startOfSelection, "Вы:".Length); MessagesTextBox.SelectionColor = Color.Green; MessagesTextBox.AppendText(QuestionTextBox.Text + "\r\n\r\n"); }
//метод, отобращающий пользовательское сообщение private void ShowUserMessage() { DateTime date = DateTime.Now; string str = Data.Text; string outputPlayer = date.ToString("[dd/MM/yyyy HH:mm:ss] ") + str + ": "; var startOfSelection = MessagesTextBox.TextLength; MessagesTextBox.AppendText(outputPlayer); MessagesTextBox.Select(startOfSelection, outputPlayer.Length); MessagesTextBox.SelectionColor = Color.Green; MessagesTextBox.AppendText(QuestionTextBox.Text + "\r\n\r\n"); }
private void device_Input(object sender, MidiInputEventArgs args) { try { Invoke(new Action(delegate() { MessagesTextBox.AppendText( args.Message.ToString() + Environment.NewLine); })); } catch { } }
//метод, показывающий ответ на вопрос, который есть в базе. private void ShowAnswer() { var rand = new Random(); //берем случайный ответ из списка ответов. CurrentAnswerIndex = rand.Next(QuestionsList[CurrentQuestionIndex].Answers.Count); PrintRBot(); StringBuilder sb = new StringBuilder(); sb.Append(QuestionsList[CurrentQuestionIndex].Answers[CurrentAnswerIndex]); sb.Append("\r\n\r\n"); MessagesTextBox.AppendText(sb.ToString()); }
//обработчик кнопки "Отправить" private void BtnSend_Click(object sender, EventArgs e) { if (QuestionTextBox.Text != "") { ShowUserMessage(); CurrentUserMessage = QuestionTextBox.Text; ClearCurrentMessageFromSymbols(); //обработка поиска в браузере if (CurrentUserMessage.Contains("что такое")) { var stringToFind = CurrentUserMessage.Replace("что такое", ""); System.Diagnostics.Process.Start("https://yandex.ru/search/?text=" + stringToFind); PrintRBot(); MessagesTextBox.AppendText("Открываю поиск по запросу \"" + stringToFind + "\"\r\n\r\n"); MessagesTextBox.ScrollToCaret(); //позволяет прокрутить окно чата до конца. QuestionTextBox.Clear(); return; } //ищем сообщение пользователя в базе FindCurrentQuestionIndex(); if (CurrentQuestionIndex == -1) //если не нашли - показываем ответ по умолчанию { ShowDefaultAnswer(); } else { ShowAnswer(); } } else { MessageBox.Show("Вы пытаетесь отправить пустое сообщение. Вам не ответят."); } MessagesTextBox.ScrollToCaret(); //позволяет прокрутить окно чата до конца. QuestionTextBox.Clear(); }
private void OnPropertyChanged(object sender, PropertyChangedEventArgs args) { switch (args.PropertyName) { case nameof(ChatViewModel.Title): UpdateTitle(); return; case nameof(ChatViewModel.Messages): MessagesTextBox.Clear(); foreach (string message in ((ChatViewModel)DataContext).Messages) { MessagesTextBox.AppendText(message); } break; } }
//метод, показывающий ответ по умолчанию private void ShowDefaultAnswer() { PrintRBot(); MessagesTextBox.AppendText("Я тебя не понимаю.\r\n\r\n"); }