简体   繁体   中英

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

I have this code to act ENTER Key as TAB but when I press Enter key it is selecting textbox value as in image

在此处输入图片说明

You can tell the TextBox to select nothing

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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