【问题标题】:How to avoid selecting textbox value when press Enter?按 Enter 时如何避免选择文本框值?
【发布时间】:2015-06-20 05:02:55
【问题描述】:
Control nextControl;
if (e.KeyCode == Keys.Enter)
{     
    nextControl = GetNextControl(ActiveControl, !e.Shift);
    if (nextControl == null)
    {
        nextControl = GetNextControl(null, true);
    }
    nextControl.Focus();               
    e.SuppressKeyPress = true;
 }

我有这段代码可以将 ENTER 键用作 TAB,但是当我按 Enter 键时,它正在选择文本框值,如图像中所示

【问题讨论】:

    标签: c# windows enter


    【解决方案1】:

    你可以告诉文本框什么都不选择

    Control nextControl;
    
    if (e.KeyCode == Keys.Enter)
    {     
        nextControl = GetNextControl(ActiveControl, !e.Shift);
        if (nextControl == null)
        {
            nextControl = GetNextControl(null, true);
        }
        nextControl.Focus();
    
        TextBox box = nextControl as TextBox;
        if (box != null)
            box.Select(box.Text.Length, 0);
    
        e.SuppressKeyPress = true;
    }
    

    【讨论】:

    • 光标聚焦在文本框值的开头。它可以在文本框值的末尾吗?
    • @Vampire 当然,编辑了样本。只需告诉它在字符串末尾选择 0 个字符
    猜你喜欢
    • 2021-06-17
    • 1970-01-01
    • 2022-01-22
    • 2018-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    • 1970-01-01
    相关资源
    最近更新 更多