Click here to Skip to main content
14,327,298 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am developing a typing tutor....
I want to prevent to write any text In b/w text which is write first user can write at the end of the text only. Not between the text
Posted
Updated 5-Jun-12 9:23am
v4
Rate this:
Please Sign up or sign in to vote.

Solution 3

here is a solution :
richTextBox1.KeyDown += new KeyEventHandler(KeyDown);


void KeyDown(object sender, KeyEventArgs e)
{
richTextBox1.SelectionStart = richTextBox1.TextLength;
}

on every keydown event, the text starting from position richTextBox1.TextLength gets selected but there is no text in that position but what you will see that the cursor comes at that position and hence even if you try to type anything in between since the cursor is at end of text gets added at last..
   
Comments
Perić Željko 5-Jun-12 15:59pm
   
It works fine, but what about deleting text, by using 'backspace' I can delete whole text ?
Silent Guardian 5-Jun-12 16:07pm
   
yes it would work but if u need to clear all text then :
if (e.KeyCode == Keys.Back)
richTextBox1.Clear();

Perić Željko 5-Jun-12 16:15pm
   
Ok.This would be solution for me.
jeetveer 5-Jun-12 22:33pm
   
yes it will work...but it's not on that level, which was i want...
if any other solution can possible plz provide it
Zoltán Zörgő 6-Jun-12 0:24am
   
why have you accepted than?
Silent Guardian 6-Jun-12 0:57am
   
its not on which level?
Rate this:
Please Sign up or sign in to vote.

Solution 2

I don't think that you can do this in the built-in Forms.RichTextBox. But you can derive one (you will probably need some API level work), or find some replacement where you can control the cursor. If I understand your goals, you actually want to prevent the user do place the cursor before the last character present when the input was displayed.

Update: you might try WPF RichTextBox, where you can intercept text input with OnPreviewTextInput event, and deny it if CaretPosition behind the original one.
   
v2
Comments
jeetveer 5-Jun-12 15:16pm
   
yes you are right...but i need help plz
Rate this:
Please Sign up or sign in to vote.

Solution 1

Hello,
if you want to prevent user to type text in the RichTextBox where he wants,
you should consider different approach to that problem. Perhaps with two rich text box controls. One for showing old text, and another for entering new one, or continuation of the old. When user finishes entering text ( or during the typing ), you just need to simply add entered text at the end of the old one in first text box by using:

richTextBox.AppendText("Entered text");


all the best,
Perić Željko
   
Comments
jeetveer 5-Jun-12 14:54pm
   
you are right. this is so simple approach. but I can not apply this approach in my project.
I want a solution in single RichTextBox. its requirement of my project.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100