35.回车触发事件 C#例子 WPF例子
就是一个简单的回车触发事件的例子。在该例子中按下回车消息框会返回文本框输入的内容
C#代码:
using System.Printing;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace practice
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void TextBox_TextChanged(object sender, KeyEventArgs e)
{
if(e.Key == Key.Enter)
{
MessageBox.Show($"{textbox.Text}");
}
}
//Key Event Arguments键盘事件参数
}
}
Extensible Application Markup Langguage:
<Window x:Class="practice.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:practice"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid Margin="100">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBox x:Name="textbox" KeyDown="TextBox_TextChanged" FontSize="87"/>
</Grid>
</Window>