Control SetCaret(TextArea textArea) { var control = new Button { Text = "Set Caret" }; control.Click += (sender, e) => { textArea.CaretIndex = textArea.Text.Length / 2; textArea.Focus(); }; return control; }
Control ReplaceSelected(TextArea textArea) { var control = new Button { Text = "Replace selected text" }; control.Click += (sender, e) => { textArea.SelectedText = "Some inserted text!"; textArea.Focus(); }; return control; }
Control SelectAll(TextArea text) { var control = new Button { Text = "Select All" }; control.Click += (sender, e) => { text.SelectAll(); text.Focus(); }; return control; }
Control SetSelectedText(TextArea textArea) { var control = new Button { Text = "Set selected text" }; control.Click += (sender, e) => { var text = textArea.Text; // select the last half of the text textArea.Selection = new Range(text.Length / 2, text.Length / 2 + 1); textArea.Focus(); }; return control; }
public NoteView(string dir) { directory = dir; notes = new List<Note>(); ListBox = new ListBox { Style = "ListNative" }; TextArea = new TextArea { Style = "TextConsole" }; var updatingNote = false; TextArea.TextChanged += delegate { if (ListBox.SelectedIndex < 0) return; var note = notes[ListBox.SelectedIndex]; bool changed = note.Changed; note.Content = TextArea.Text; if (changed != note.Changed) ListBox.Invalidate(); }; TextArea.SelectionChanged += delegate { if (ListBox.SelectedIndex < 0 || updatingNote) return; notes[ListBox.SelectedIndex].Selection = TextArea.Selection; }; ListBox.SelectedIndexChanged += delegate { if (ListBox.SelectedIndex < 0) return; var note = notes[ListBox.SelectedIndex]; updatingNote = true; TextArea.Text = note.Content; TextArea.Selection = note.Selection; TextArea.Focus(); updatingNote = false; }; }
/// <summary> /// 查找字符串 /// </summary> /// <param name="textArea"></param> /// <param name="findContent"></param> /// <param name="curIndex"></param> /// <param name="caseSensitive"></param> /// <param name="isUp"></param> public void FindText(TextArea textArea, string findContent, int curIndex, bool caseSensitive, bool isUp) { string searchText; // pre if (isUp) { searchText = textArea.Text.Substring(0, curIndex); } else { curIndex += textArea.Selection.Length(); searchText = textArea.Text.Substring(curIndex); } if (!caseSensitive) { searchText = searchText.ToLower(); findContent = findContent.ToLower(); } // find var index = isUp ? searchText.LastIndexOf(findContent, StringComparison.Ordinal) : searchText.IndexOf(findContent, StringComparison.Ordinal); if (index != -1) { var selectionStart = isUp ? index : index + curIndex; textArea.Selection = new Range<int>(selectionStart, selectionStart + findContent.Length - 1); textArea.Focus(); } else { MessageBox.Show(string.Format("Not Found \"{0}\" ", findContent)); } }