Enter key pressed event handler

后端 未结 5 1276
予麋鹿
予麋鹿 2020-11-30 04:42

I want to capture the text from the textbox when enter key is hit. I am using WPF/visual studio 2010/.NET 4. I dont know what event handler to be used in the tag ? I als

相关标签:
5条回答
  • 2020-11-30 05:08

    For those who struggle at capturing Enter key on TextBox or other input control, if your Form has AcceptButton defined, you will not be able to use KeyDown event to capture Enter.

    What you should do is to catch the Enter key at form level. Add this code to the form:

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if ((this.ActiveControl == myTextBox) && (keyData == Keys.Return))
        {
            //do something
            return true;
        }
        else
        {
            return base.ProcessCmdKey(ref msg, keyData);
        }
    }
    
    0 讨论(0)
  • 2020-11-30 05:09

    The KeyDown event only triggered at the standard TextBox or MaskedTextBox by "normal" input keys, not ENTER or TAB and so on.

    One can get special keys like ENTER by overriding the IsInputKey method:

    public class CustomTextBox : System.Windows.Forms.TextBox
    {
        protected override bool IsInputKey(Keys keyData)
        {
            if (keyData == Keys.Return)
                return true;
            return base.IsInputKey(keyData);
        }
    }
    

    Then one can use the KeyDown event in the following way:

    CustomTextBox ctb = new CustomTextBox();
    ctb.KeyDown += new KeyEventHandler(tb_KeyDown);
    
    private void tb_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
              //Enter key is down
    
              //Capture the text
              if (sender is TextBox)
              {
                  TextBox txb = (TextBox)sender;
                  MessageBox.Show(txb.Text);
              }
        }
    }
    
    0 讨论(0)
  • 2020-11-30 05:13

    In WPF, TextBox element will not get opportunity to use "Enter" button for creating KeyUp Event until you will not set property: AcceptsReturn="True".

    But, it would`t solve the problem with handling KeyUp Event in TextBox element. After pressing "ENTER" you will get a new text line in TextBox.

    I had solved problem of using KeyUp Event of TextBox element by using Bubble event strategy. It's short and easy. You have to attach a KeyUp Event handler in some (any) parent element:

    XAML:

    <Window x:Class="TextBox_EnterButtomEvent.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TextBox_EnterButtomEvent"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid KeyUp="Grid_KeyUp">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height ="0.3*"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Row="1" Grid.Column="1" Padding="0" TextWrapping="WrapWithOverflow">
            Input text end press ENTER:
        </TextBlock>
        <TextBox Grid.Row="2" Grid.Column="1" HorizontalAlignment="Stretch"/>
        <TextBlock Grid.Row="4" Grid.Column="1" Padding="0" TextWrapping="WrapWithOverflow">
            You have entered:
        </TextBlock>
        <TextBlock Name="txtBlock" Grid.Row="5" Grid.Column="1" HorizontalAlignment="Stretch"/>
    </Grid></Window>
    

    C# logical part (KeyUp Event handler is attached to a grid element):

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    
        private void Grid_KeyUp(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.Enter)
            {
                TextBox txtBox = e.Source as TextBox;
                if(txtBox != null)
                {
                    this.txtBlock.Text = txtBox.Text;
                    this.txtBlock.Background = new SolidColorBrush(Colors.LightGray);
                }
            }
        }
    }
    

    Result:

    Image with result

    0 讨论(0)
  • 2020-11-30 05:14

    Either KeyDown or KeyUp.

    TextBox tb = new TextBox();
    tb.KeyDown += new KeyEventHandler(tb_KeyDown);
    
    static void tb_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            //enter key is down
        }
    }
    
    0 讨论(0)
  • 2020-11-30 05:15

    You can also use PreviewKeyDown in WPF:

    <TextBox PreviewKeyDown="EnterClicked" />
    

    or in C#:

    myTextBox.PreviewKeyDown += EnterClicked;
    

    And then in the attached class:

    void EnterClicked(object sender, KeyEventArgs e) {
        if(e.Key == Key.Return) {
            DoSomething();
            e.Handled = true;
        }
    }
    
    0 讨论(0)
提交回复
热议问题