繁体   English   中英

按Enter时如何避免选择文本框值?

[英]How to avoid selecting textbox value when press Enter?

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 键时,它正在选择文本框值,如图

在此处输入图片说明

您可以告诉 TextBox 不选择任何内容

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

暂无
暂无

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

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