我有一个只读的富文本框和一个可编辑的文本框。只读的文本来自可编辑。它们无法同时查看。当用户按下某个键时,它会隐藏只读,然后在可编辑的中选择该位置。
我希望它能输入被压入可编辑状态的键,而不会播放错误“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;
}
我必须检查它是否是非控制字符,但这是另一笔交易。谢谢大家!
答案 0 :(得分: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
}
答案 1 :(得分:1)
以下是一种方法:检查<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;
}
}
无需摆弄光标。一定要:
txtEdit.HideSelection = false;
也许
txtReadOnly.HideSelection = false;
显然要保持两者同步:
private void txtEdit_TextChanged(object sender, EventArgs e)
{
txtReadOnly.Text = txtEdit.Text;
}
您需要决定用户从编辑返回查看的某种方式。应保留Escape以中止编辑!也许Control-Enter?