Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Texteditor FAQs

siegi44 edited this page Jan 16, 2008 · 6 revisions

Q: How can I access the currently selected text?

**A: ** For accessing the currently selected text you can either use the simple version or the more complicated version. The more complicated version has the advantage, that it can easily be expanded to multiple selections, which will be possible in future.

Simple: (assuming "textEditor1" is the name of your TextEditor control) {{textEditor1.ActiveTextAreaControl.SelectionManager.SelectedText;}}

Improved: {{<esc> TextArea textArea = textEditor1.ActiveTextAreaControl; // access the current text area. if (textArea.SelectionManager.HasSomethingSelected) { // if something is selected ISelection currentSelection = textArea.SelectionManager.SelectionCollection&#0091;0&#0093;; // Get the (first) selection string selectedText = currentSelection.SelectedText; // get the selected text. } </esc>}}

For more information about ISelection and SelectionManager see About selections.

Q: How can I handle KeyPress, KeyUp, KeyDown events?

**A: ** It's less complicated than it seems to be, you have just to find the right location to attach your Handlers to, an example:

`{{ public partial class MainForm : Form { public MainForm() { // // The InitializeComponent() call is required for Windows Forms designer support. // InitializeComponent();

        this.textEditorControl1.ActiveTextAreaControl.TextArea.KeyDown += new KeyEventHandler(this.TextEditor_KeyDown);
        this.textEditorControl1.ActiveTextAreaControl.TextArea.KeyUp += new KeyEventHandler(this.TextEditor_KeyUp);
        this.textEditorControl1.ActiveTextAreaControl.TextArea.KeyPress += new KeyPressEventHandler(this.TextEditor_KeyPress);
    }
   
    private void TextEditor_KeyDown(object sender, KeyEventArgs e)
    {
        System.Diagnostics.Debug.Print("KeyDown: " + e.KeyData.ToString());
    }
   
    private void TextEditor_KeyUp(object sender, KeyEventArgs e)
    {
        System.Diagnostics.Debug.Print("KeyDown: " + e.Modifiers.ToString());
    }
   
    private void TextEditor_KeyPress(object sender, KeyPressEventArgs e)
    {
        System.Diagnostics.Debug.Print("KeyDown: " + e.KeyChar.ToString());
    }
}

`}}

Clone this wiki locally