63

我想在按下回车键时从文本框中捕获文本。我正在使用 WPF/visual studio 2010/.NET 4。我不知道要在标签中使用什么事件处理程序?我也想对 maskedtextbox 做同样的事情。

4

5 回答 5

111

KeyDown 或 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
    }
}
于 2010-09-20T14:40:14.720 回答
23

您还可以在 WPF 中使用 PreviewKeyDown:

<TextBox PreviewKeyDown="EnterClicked" />

或在 C# 中:

myTextBox.PreviewKeyDown += EnterClicked;

然后在附加的类中:

void EnterClicked(object sender, KeyEventArgs e) {
    if(e.Key == Key.Return) {
        DoSomething();
        e.Handled = true;
    }
}
于 2012-02-15T20:01:19.493 回答
7

KeyDown 事件仅由“普通”输入键在标准 TextBox 或 MaskedTextBox 触发,而不是 ENTER 或 TAB 等。

可以通过覆盖 IsInputKey 方法获得特殊键,例如 ENTER:

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

然后可以通过以下方式使用 KeyDown 事件:

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);
          }
    }
}
于 2011-03-28T15:38:56.763 回答
3

在 WPF 中,除非您不设置属性:AcceptsReturn="True",否则 TextBox 元素将无法使用“Enter”按钮创建 KeyUp 事件。

但是,它不能解决在 TextBox 元素中处理 KeyUp 事件的问题。按“ENTER”后,您将在 TextBox 中获得一个新的文本行。

我通过使用 Bubble 事件策略解决了使用 TextBox 元素的 KeyUp 事件的问题。它简短而容易。您必须在某些(任何)父元素中附加 KeyUp 事件处理程序:

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# 逻辑部分(KeyUp 事件处理程序附加到网格元素):

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

结果:

带结果的图像

于 2015-12-03T15:30:47.060 回答
1

对于那些难以在 TextBox 或其他输入控件上捕获 Enter 键的人,如果您的 Form 定义了 AcceptButton,您将无法使用 KeyDown 事件来捕获 Enter。

您应该做的是在表单级别捕获 Enter 键。将此代码添加到表单中:

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);
    }
}
于 2019-03-02T16:23:18.343 回答