problem handling backspace key

  • Thread starter Thread starter C#
  • Start date Start date
C

C#

I have a richtextbox that i am copying to an invisible richtextbox . during
the keydown and keypress events i am inserting HTML tags into the invisble
richtextbox along with the origional text. The only problem i am running
into is when the user presses the backspace and delete key i have a hard
time knowing how to erase the charater(s) desired from the invisible
richtextbox because it contains additional text. Any help is GREATLY
appreciated. Thanks in advance!
 
This depends on how much data they're entering in... If you're not expecting
them to enter all that much (less that a couple of thousand characters say),
then the solution is a little easier.

if the original text is something like:

"Hello World!"

and you then do this in your second text box

"<html>
<head><title>New Text</title></head>
<body>
<p>Hello World!</p>
</body>
</html>"


The easiest way would definately NOT be to track each key press and alter
the second text box one at a time... If I press the left cursor seven times
and then typed "Blue Skied ", you'd expect the resulting text to be "Hello
Blue Skied World!" not "Hello World!Blue Skied". I presume this is where
your problems are coming in with backspaces and delete presses...

What I would suggest is to simply generate the text in the new text box once
the user has finished typing what they need to (if this is an option) or, if
you MUST, on each key press... You could do this by doing the following:

StringBuilder sb = new StringBuilder ();
sb.Append ( "<html>\r\n" );
sb.Append ( "<head><title>New Text</title></head>\r\n" );
sb.Append ( <body>\r\n" );
sb.Append ( "<p>" );
sb.Append ( firstTextbox.Text );
sb.Append ( "</p>\r\n" );
sb.Append ( "</body>\r\n" );
sb.Append ( "</html>" );
textboxSecond.Text = sb.ToString();

Let me know if you need any more help.

Dan.
 
Dan you dont know how much i appreciate your help!
As for building the html after the user is done i could do that but the
problem is is i am allowing the user to place images into the rtb. what i
was doing was using rtbHiddent.Text += "<img src='"+dlgOpen.FileName+"'>";
to insert the image tag. If i were to place the html tags in afterwards
there isnt any filename or hints of where the image is from in rtf. i.e.-
the rtf coding for an image is something like
"{\pict{.....}02352464F20000000235234210003246023.....}"
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnrtfspec/html/rtfspec.asp
has more info on the rtf syntax. So the solution would work except for the
fact that somehow i would have to track the image filenames and the position
they are to be at. Thank you so much for the help Dan!
 
Back
Top