2
votes

I have an application that allows users to browse through data. I have menu items controlling navigation and a RichTextBox displaying the data. Very straightforward.

tl;dr version

It mostly works except for one strange problem. There are instances where the RichTextControl will replace the first character typed with a random unicode character. Feel free to download this sample app and see for yourself:

http://www.technitivity.com/stackoverflow/RichTextFocusTest.zip

Full Explanation

The issue happens when navigating between rows. It's best described with a few use cases:

Use Case 1

  1. Navigate anywhere into the dataset.
  2. Press back.
  3. Press next.
  4. Type any letter, say, "F".
  5. Result: "F" appears in the RichTextBox as expected.

Use Case 2

  1. Navigate anywhere into the dataset.
  2. Press back twice.
  3. Press next twice.
  4. Type the letter "F".
  5. Result: instead of "F", "ᅲ" appears in the RichTextBox.

Use Case 3

  1. Navigate anywhere into the dataset.
  2. Press next twice.
  3. Press back twice.
  4. Type the letter "F".
  5. Result: instead of "F", "᧴" appears in the RichTextBox.

The navigation process entails nothing more than:

// either forward
i++;
// or backward
i--;
// then update
RichTextBox1.Text = MyData[i];

Procedurally speaking:

// This works
RichTextBox1.Text = MyData[3];

// This works
RichTextBox1.Text = MyData[3];
RichTextBox1.Text = MyData[2];
RichTextBox1.Text = MyData[3];

// This doesn't work
RichTextBox1.Text = MyData[3];
RichTextBox1.Text = MyData[2];
RichTextBox1.Text = MyData[1];
RichTextBox1.Text = MyData[2];
RichTextBox1.Text = MyData[3];

Granted, that's not what's actually happening, but it is what's effectively happening.

It's important to note that this doesn't happen if the RichTextBox loses focus between updates. It only happens if the RichTextBox retains focus while its Text attribute is updated in accordance with the above description.

I'm at a complete loss about what's causing this, how to fix it, or why I can't seem to find anyone else with this problem.

I've reproduced it on 64-bit Windows 7 and 32-bit Windows Vista. This is on .NET Framework 4 though I was also able to reproduce on a .NET Framework 2 project.

Here's hoping someone else has run across this (and solved it!)

Edit:

Here's a screenshot:

http://www.technitivity.com/stackoverflow/RichTextBox-Screenshot1.png

As mentioned in the comments, to reproduce this in the sample app, you have to use the keyboard menu shortcuts. If you click on the menu items (or the toolbar buttons), the RichTextBox loses focus and the problem goes away. But if you navigate through the items using Alt+Left or Alt+Right (back/next) and then type, you should see something like what's shown in the above screenshot.

1
I just downloaded the source code for your demonstration, built it in VS 2008/Framework 3.5/Windows 7/Debug/AnyCPU, and I can't reproduce the problem. (I didn't run the .exe you provided for security reasons.) Can you describe the exact sequence of user actions I need to do with the demo app? Does "Navigate anywhere into the dataset" mean "Click between two letters with the mouse", for instance? - dbc
You need to make sure that you navigate using the menu item shortcut keys. If you click the menu items or click the toolbar buttons, the issue doesn't happen (since you're moving focus away from the RichTextBox). The shortcuts are Alt+Left and Alt+Right. - Charlie Hills

1 Answers

1
votes

I hesitate to call this an "answer", but I couldn't find a "Post a Hack" button and this hack does get me by for now. I'm not thrilled about it, but sometimes you just have to move on. Here it is.

Since the problem went away when the RichTextBox lost focus, I tried an experiment:

  • I created a visible, 0-pixel wide textbox, called Hacktastic.
  • I added a KeyPress event to the RichTextBox.
  • On KeyPress:
    • Hacktastic.Focus();
    • Hacktastic.Text = KeyChar.ToString();
    • MyRichTextBox.Focus();

This worked and (at least for now) I'm sticking with this as a solution. If anyone could still try out my sample project and reproduce and/or solve this, I would love further feedback:

http://www.technitivity.com/stackoverflow/RichTextFocusTest.zip

Steps to repro in the test project:

  1. Using Alt+Right arrow, move through the dataset to, say, the fourth record.
  2. Using Alt+Left arrow, move back through the dataset two places, to the second record.
  3. Using Alt+Right arrow, move back to the fourth record.
  4. Press any KeyChar.
  5. Observe KeyChar is replaced with a "random", large-value unicode character.

I say "random" because a specific set of navigation (back/next) keystrokes will insert the exact same unicode character. However, depending on where you start in the set or how far back you go, you'll get a different character.

Also, note that only going back one record and forward one record does not cause the problem. You have to move at least two records for this to happen.