RichTextBox behaving weirdly

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul E Collins

    RichTextBox behaving weirdly

    I want to colour certain characters as they are typed in a RichTextBox. I
    started with this simple event handler.

    private void rtb_KeyPress(ob ject sender, KeyPressEventAr gs e)
    {
    if (e.KeyChar >= ' ')
    {
    this.SelectedTe xt = e.KeyChar.ToStr ing();
    this.SelectionC olor = Color.Blue;

    e.Handled = true;
    }
    }

    Why does this code not colour the first character typed into the box? It
    works for subsequent characters.

    Eq.


  • Paul E Collins

    #2
    Re: RichTextBox behaving weirdly

    Hmm, okay, it seems that setting SelectedText changes the selection. Here's
    what I used in the end.

    if (e.KeyChar >= ' ')
    {
    this.SelectedTe xt = e.KeyChar.ToStr ing();

    this.SelectionS tart--;
    this.SelectionL ength++;
    this.SelectionC olor = GetColourForCha r(e.KeyChar);

    this.SelectionS tart++;
    this.DeselectAl l();

    e.Handled = true;
    }

    Eq.


    Comment

    Working...