如何防止在RichtextBox中书写(这是一个挑战) [英] How to prevent to write in RichtextBox(It's a challenge)

查看:60
本文介绍了如何防止在RichtextBox中书写(这是一个挑战)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发打字导师....
我想防止在首先写的黑白文本中写任何文本,用户只能在文本结尾处书写. 不在文本之间

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

推荐答案

这是一种解决方案:
here is a solution :
richTextBox1.KeyDown += new KeyEventHandler(KeyDown);


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


在每个keydown事件中,从位置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..


我认为您不能在内置的Forms.RichTextBox中执行此操作.但是您可以派生一个(可能需要做一些API级别的工作),或者在可以控制游标的位置找到一些替代品.如果我了解您的目标,则实际上是要防止用户将光标放置在显示输入内容时出现的最后一个字符之前.

更新:您可以尝试使用WPF RichTextBox,在其中可以拦截带有 OnPreviewTextInput 事件的文本输入,如果CaretPosition位于原始事件的后面,则可以拒绝它.
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.


您好,
如果要阻止用户在所需的RichTextBox中键入文本,
您应该考虑针对该问题的不同方法.也许有两个富文本框控件.一个用于显示旧文本,另一个用于输入新文本或旧文本的延续.当用户完成输入文本时(或在键入过程中),您只需使用以下命令将输入​​的文本添加到第一个文本框中的旧文本的末尾即可:

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");



一切顺利,
PerićŽeljko



all the best,
Perić Željko


这篇关于如何防止在RichtextBox中书写(这是一个挑战)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆