Click here to Skip to main content
15,917,059 members
Please Sign up or sign in to vote.
1.00/5 (1 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

here is a solution :
C#
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..
 
Share this answer
 
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?
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.
 
Share this answer
 
v2
Comments
jeetveer 5-Jun-12 15:16pm    
yes you are right...but i need help plz
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:

C#
richTextBox.AppendText("Entered text");


all the best,
Perić Željko
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900