【问题标题】:Reading text in Text box after hitting enter C# WPF [duplicate]输入 C# WPF 后在文本框中读取文本 [重复]
【发布时间】:2013-01-23 04:50:58
【问题描述】:

我对 C# WPF 有一个简单的要求。我有一个文本框,我将在其中输入一些文本。当我按 Enter 时,我希望触发一个事件。我的代码是

 private void AddKeyword(object sender, KeyEventArgs e)
  {
     if(e.Key == Key.Enter)
      {
         //DO something
      }
  } 

我已经设置了我的文本框 AcceptReturn =True;该方法根本不起作用,当我按 Enter 时,我没有看到任何事件被触发。请帮帮我。提前致谢

【问题讨论】:

  • AddKeyword 是否订阅了 KeyUp 事件?
  • Yes.. AddKeyword 订阅了 KeydDown 事件。其他键是触发事件。只有当我按 Enter 时,它才会保持空闲状态。它不是触发事件,而是在文本框中开始新的一行文本
  • 请分享您的代码,有助于发现问题!
  • key up 不同于 key down 尝试使用 keyup 就像@AppDeveloper 的响应

标签: c# wpf textbox keydown enter


【解决方案1】:

这对我来说很好

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="textBox1" VerticalAlignment="Top" Width="190" KeyUp="textBox1_KeyUp" />
    </Grid>
</Window>

using System.Windows;
using System.Windows.Input;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                e.Handled = true;
                MessageBox.Show("Enter Fired!");
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-05
    • 1970-01-01
    • 2014-05-07
    • 1970-01-01
    • 1970-01-01
    • 2010-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多