C# RichTextBox 只读事件

C# RichTextBox ReadOnly Event

我有一个只读的富文本框和一个可编辑的文本框。只读文本来自可编辑文本。不能同时查看。当用户按下一个键时,它会隐藏只读,然后在可编辑中选择该位置。

我希望它把按下的键输入到editable中而不弹错"ding"

我认为重写只读错误函数是最理想的,但我不确定那是什么。

    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());
    }

答案:

    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;
    }

我必须让它检查它是否是非控制字符,但这是另一回事。谢谢大家!

一个想法是使富文本框可编辑但取消所有键:

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
}

这是一种方法:检查 <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;
    }
}

不需要fiddle用光标。确保拥有:

txtEdit.HideSelection = false;

也许还有

txtReadOnly.HideSelection = false;

显然要保持两者同步:

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

您需要确定某种方式让用户return从编辑到查看。 Escape 应该保留用于中止编辑!也许控制输入?