如何将WinForms文本框设置为覆盖模式

oug3syen  于 2023-05-07  发布在  其他
关注(0)|答案(7)|浏览(143)

是否可以强制windows窗体应用程序中的文本框以“覆盖模式”工作,即当用户键入而不是添加字符时,字符是否被替换?
否则,是否有一个标准的方法来获得这种行为?

jgzswidk

jgzswidk1#

尝试使用MaskedTextBox并将InsertKeyMode设置为InsertKeyMode.Overwrite。

MaskedTextBox box = ...;
box.InsertKeyMode = InsertKeyMode.Overwrite;
cnwbcb6i

cnwbcb6i2#

如果您不希望使用Masked文本框,则可以在处理KeyPress事件时执行此操作。

private void Box_KeyPress(object sender, KeyPressEventArgs e)
    {
        TextBox Box = (sender as TextBox);
        if (Box.SelectionStart < Box.TextLength && !Char.IsControl(e.KeyChar))
        {
            int CacheSelectionStart = Box.SelectionStart; //Cache SelectionStart as its reset when the Text property of the TextBox is set.
            StringBuilder sb = new StringBuilder(Box.Text); //Create a StringBuilder as Strings are immutable
            sb[Box.SelectionStart] = e.KeyChar; //Add the pressed key at the right position
            Box.Text = sb.ToString(); //SelectionStart is reset after setting the text, so restore it
            Box.SelectionStart = CacheSelectionStart + 1; //Advance to the next char
        }
    }
s6fujrry

s6fujrry3#

标准的方法是在文本框中选择现有文本,然后当用户键入时,它将自动替换现有文本

zaqlnxep

zaqlnxep4#

此代码似乎有一个错误。我发现你需要在Keypress事件中设置e.Handled,否则字符会被插入两次。下面是我的代码(在VB中)基于上述内容:-

Private Sub txtScreen_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtScreen.KeyPress
    If txtScreen.SelectionStart < txtScreen.TextLength AndAlso Not [Char].IsControl(e.KeyChar) Then
        Dim SaveSelectionStart As Integer = txtScreen.SelectionStart
        Dim sb As New StringBuilder(txtScreen.Text)
        sb(txtScreen.SelectionStart) = e.KeyChar
        'Add the pressed key at the right position
        txtScreen.Text = sb.ToString()
        'SelectionStart is reset after setting the text, so restore it
        'Advance to the next char
        txtScreen.SelectionStart = SaveSelectionStart + 1
        e.Handled = True
    End If
End Sub
798qvoo8

798qvoo86#

谢谢大家!现在这是我的了参考:https://www.syncfusion.com/faq/windowsforms/textbox/how-can-i-place-a-textbox-in-overwrite-mode-instead-of-insert-mode

int surrogate = 0;
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!insertMode)
    {
        if (char.IsSurrogate(e.KeyChar))
        {
            surrogate++;
            if (surrogate % 2 != 0)
            {
                return;
            }
            surrogate = 0;
        }
        if (textBox1.Text.Length != textBox1.MaxLength && textBox1.SelectedText == ""
           && textBox1.Text != "" && textBox1.SelectionStart != textBox1.Text.Length)
        {
            textBox1.SelectionLength = 1;//對於已經輸入完成的 surrogate C#應該會正確判斷其字長度;實際測試非然也
            //for surrogate pairs input this is necessary or it will cut the pairs in wrong way above
            if (char.IsSurrogate(textBox1.SelectedText.ToCharArray()[0]))
            {
                textBox1.SelectionLength = 2;
            }
        }
    }
}

我的应用回购:https://github.com/oscarsun72/TextForCtext.git
WindowsFormsApp1/Form1.cs

llew8vvj

llew8vvj7#

不确定使用KeyPress事件是否会扰乱正常的overtype过程,或者可能是KeyPress中正在检查的特定内容,但这并不是正常的Windows文本框应该如何表现,因为当您开始键入具有高亮文本的控件时,选择应该被删除,允许您键入空的空间。当我看到If语句时,我意识到我正在寻找的行为是这样完成的:

If tb.SelectionStart < tb.TextLength AndAlso Not [Char].IsControl(e.KeyChar) Then
        tb.SelectedText = ""
    End If

我不知道为什么要保留选择,但如果需要的话,前面的代码是理想的
萨尔

相关问题