简体   繁体   English

C#RichTextBox ReadOnly事件

[英]C# RichTextBox ReadOnly Event

I have a read only rich text box and an editable text box. 我有一个只读的RTF文本框和一个可编辑的文本框。 The text from the read only is from the editable. 只读文本来自可编辑文本。 They can't be viewed at the same time. 不能同时查看它们。 When the user presses a key it hides the read only and then selects that position in the editable. 当用户按下一个键时,它将隐藏只读内容,然后在可编辑内容中选择该位置。

I would like it to enter the key that was pressed into the editable without playing the error "ding" 我希望它输入按下可编辑内容的键,而不播放错误“ ding”

I'm thinking that an override of the read only error function would be ideal but i'm not sure what that is. 我想重写只读错误函数将是理想的选择,但我不确定那是什么。

    private void EditCode(object sender, KeyPressEventArgs e)
    {
        int cursor = txtReadOnly.SelectionStart;
        tabText.SelectedIndex = 0;
        ToggleView(new object(), new EventArgs());
        txtEdit.SelectionStart = cursor;
        txtEdit.Text.Insert(cursor, e.KeyChar.ToString());
    }

Answer: 回答:

    private void EditCode(object sender, KeyPressEventArgs e)
    {
        e.Handled = true;
        int cursor = txtCleanCode.SelectionStart;
        tabText.SelectedIndex = 0;
        ToggleView(new object(), new EventArgs());

        txtCode.Text = txtCode.Text.Insert(cursor, e.KeyChar.ToString());

        txtCode.SelectionStart = cursor + 1;
    }

I'll have to make it check if it's a non-control char but that's another deal. 我必须检查它是否是非控制字符,但这是另一回事。 Thanks everyone! 感谢大家!

One idea is to make the rich text box editable but canceling out all keys: 一种想法是使富文本框可编辑,但取消所有键:

private void richtextBox1_KeyDown(object sender, KeyEventArgs e)
{
    // Stop the character from being entered into the control
    e.Handled = true;
    // add any other code here
}

Here is one way: Check for <Enter> so the user can still use the navigation keys: 这是一种方法:检查<Enter>以便用户仍然可以使用导航键:

private void txtReadOnly_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        e.Handled = true;  // no ding for normal keys in the read-only!
        txtEdit.SelectionStart = txtReadOnly.SelectionStart;
        txtEdit.SelectionLength = txtReadOnly.SelectionLength;
    }
}

No need to fiddle with a cursor. 无需摆弄光标。 Make sure to have: 确保具有:

txtEdit.HideSelection = false;

and maybe also 也许也

txtReadOnly.HideSelection = false;

Obviously to keep the two synchronized : 显然要保持两个同步:

private void txtEdit_TextChanged(object sender, EventArgs e)
{
    txtReadOnly.Text = txtEdit.Text;
}

You will need o decide on some way fo r the user to return from editing to viewing. 您需要确定用户从编辑返回查看的某种方式。 Escape should be reserved for aborting the edit! 应保留转义以中止编辑! Maybe Control-Enter? 也许是Control-Enter?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM