R
Ray Mitchell
Hello,
I'm revisiting a problem I've never solved satisfactorily. I have a
multi-line RichTextBox that receives text input from a serial port (not the
keyboard); that input frequently contains backspace characters. To handle
the backspace characters I'm currently calculating the offset of the
character to be backspaced over from the beginning of the entire text box,
selecting that character, then replacing it with no character. The "for"
loop in the code below calculates the length of everything in the text box.
While this scheme has worked correctly, it really slows the system down as
the contents of the text box increases. It seems that ideally I should be
able to deal only with the last line in the text box instead of having to
treat everything in the text box as one big long string, but I haven't
figured out a way to do this. Another brutal alternative to recalculating
the entire length of the text box each time a backspace character is
encountered would be to manually keep a running count of the total number of
characters as they are added and subtracted, and this would doubtless speed
things up significantly. However, it seems that there still should be a
better way.
Thanks,
Ray
***** I execute the following code each time a backspace character is
encountered:
int displayLineCount = display.Lines.Length;
if (displayLineCount > 0)
{
string lastLine = display.Lines[displayLineCount - 1];
int lastLineLength = lastLine.Length;
if (lastLineLength > 0)
{
// Get count of all characters from beginning of display up to the
current line.
// For purposes of calculating character offset of the last display
character from
// the beginning of the display, the length of each line is considered
to be 1 plus
// the number of characters on that line.
int length = 0;
for (int lineNo = 0; lineNo < displayLineCount - 1; ++lineNo)
length += display.Lines[lineNo].Length + 1;
display.Select(length + lastLineLength - 1, 1);
display.SelectedText = "";
display.SelectionStart = length + lastLineLength - 1;
}
}
I'm revisiting a problem I've never solved satisfactorily. I have a
multi-line RichTextBox that receives text input from a serial port (not the
keyboard); that input frequently contains backspace characters. To handle
the backspace characters I'm currently calculating the offset of the
character to be backspaced over from the beginning of the entire text box,
selecting that character, then replacing it with no character. The "for"
loop in the code below calculates the length of everything in the text box.
While this scheme has worked correctly, it really slows the system down as
the contents of the text box increases. It seems that ideally I should be
able to deal only with the last line in the text box instead of having to
treat everything in the text box as one big long string, but I haven't
figured out a way to do this. Another brutal alternative to recalculating
the entire length of the text box each time a backspace character is
encountered would be to manually keep a running count of the total number of
characters as they are added and subtracted, and this would doubtless speed
things up significantly. However, it seems that there still should be a
better way.
Thanks,
Ray
***** I execute the following code each time a backspace character is
encountered:
int displayLineCount = display.Lines.Length;
if (displayLineCount > 0)
{
string lastLine = display.Lines[displayLineCount - 1];
int lastLineLength = lastLine.Length;
if (lastLineLength > 0)
{
// Get count of all characters from beginning of display up to the
current line.
// For purposes of calculating character offset of the last display
character from
// the beginning of the display, the length of each line is considered
to be 1 plus
// the number of characters on that line.
int length = 0;
for (int lineNo = 0; lineNo < displayLineCount - 1; ++lineNo)
length += display.Lines[lineNo].Length + 1;
display.Select(length + lastLineLength - 1, 1);
display.SelectedText = "";
display.SelectionStart = length + lastLineLength - 1;
}
}