Skip to content

Commit 2e17961

Browse files
authored
Change RETURN to ENTER in WPF example (#1867)
* update article; move snippets * Update dotnet-desktop-guide/framework/wpf/advanced/how-to-detect-when-the-enter-key-pressed.md * Update dotnet-desktop-guide/framework/wpf/advanced/how-to-detect-when-the-enter-key-pressed.md
1 parent 9d4ba79 commit 2e17961

34 files changed

+824
-281
lines changed

dotnet-desktop-guide/framework/wpf/advanced/how-to-detect-when-the-enter-key-pressed.md

+21-18
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
11
---
22
title: "How to: Detect When the Enter Key Pressed"
33
description: Detect when the Enter key is selected on the keyboard in Windows Presentation Foundation. This example consists of XAML and a code-behind file.
4-
ms.date: "03/30/2017"
4+
ms.date: 07/16/2024
55
dev_langs:
66
- "csharp"
77
- "vb"
88
helpviewer_keywords:
99
- "Enter key [WPF], detecting"
1010
- "keys [WPF], Enter"
1111
ms.assetid: a66f39d2-ef4a-43a5-b454-a4ea0fe88655
12+
# TODO:
13+
# When upgrading this article to .NET, add more examples such as doing a global handler, attached behavior, input command, etc>
14+
#
1215
---
1316
# How to: Detect When the Enter Key Pressed
1417

15-
This example shows how to detect when the <xref:System.Windows.Input.Key.Enter> key is pressed on the keyboard.
16-
17-
This example consists of a Extensible Application Markup Language (XAML) file and a code-behind file.
18-
19-
## Example
20-
21-
When the user presses the <xref:System.Windows.Input.Key.Enter> key in the <xref:System.Windows.Controls.TextBox>, the input in the text box appears in another area of the user interface (UI).
22-
23-
The following XAML creates the user interface, which consists of a <xref:System.Windows.Controls.StackPanel>, a <xref:System.Windows.Controls.TextBlock>, and a <xref:System.Windows.Controls.TextBox>.
24-
25-
[!code-xaml[keydown#KeyDownUI](~/samples/snippets/csharp/VS_Snippets_Wpf/KeyDown/CSharp/Window1.xaml#keydownui)]
26-
27-
The following code behind creates the <xref:System.Windows.UIElement.KeyDown> event handler. If the key that is pressed is the <xref:System.Windows.Input.Key.Enter> key, a message is displayed in the <xref:System.Windows.Controls.TextBlock>.
28-
29-
[!code-csharp[keydown#KeyDownSample](~/samples/snippets/csharp/VS_Snippets_Wpf/KeyDown/CSharp/Window1.xaml.cs#keydownsample)]
30-
[!code-vb[keydown#KeyDownSample](~/samples/snippets/visualbasic/VS_Snippets_Wpf/KeyDown/VisualBasic/Window1.xaml.vb#keydownsample)]
31-
18+
This example shows how to detect when the <xref:System.Windows.Input.Key.Enter> key is pressed on the keyboard.
19+
20+
This example consists of a Extensible Application Markup Language (XAML) file and a code-behind file.
21+
22+
## Example
23+
24+
When the user presses the <xref:System.Windows.Input.Key.Enter> key in the <xref:System.Windows.Controls.TextBox>, the input in the text box appears in another area of the user interface (UI).
25+
26+
The following XAML creates the user interface, which consists of a <xref:System.Windows.Controls.StackPanel>, a <xref:System.Windows.Controls.TextBlock>, and a <xref:System.Windows.Controls.TextBox>.
27+
28+
:::code language="xaml" source="./snippets/how-to-detect-when-the-enter-key-pressed/csharp/MainWindow.xaml" id="example":::
29+
30+
The following code behind creates the <xref:System.Windows.UIElement.KeyDown> event handler. If the key that is pressed is the <xref:System.Windows.Input.Key.Enter> key, a message is displayed in the <xref:System.Windows.Controls.TextBlock>.
31+
32+
:::code language="csharp" source="./snippets/how-to-detect-when-the-enter-key-pressed/csharp/MainWindow.xaml.cs" id="handler":::
33+
:::code language="vb" source="./snippets/how-to-detect-when-the-enter-key-pressed/vb/MainWindow.xaml.vb" id="handler":::
34+
3235
## See also
3336

3437
- [Input Overview](input-overview.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Application x:Class="SDKSamples.App"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
StartupUri="MainWindow.xaml">
5+
<Application.Resources>
6+
7+
</Application.Resources>
8+
</Application>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Configuration;
2+
using System.Data;
3+
using System.Windows;
4+
5+
namespace SDKSamples
6+
{
7+
/// <summary>
8+
/// Interaction logic for App.xaml
9+
/// </summary>
10+
public partial class App : Application
11+
{
12+
}
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Windows;
2+
3+
[assembly:ThemeInfo(
4+
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
5+
//(used if a resource is not found in the page,
6+
// or application resource dictionaries)
7+
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
8+
//(used if a resource is not found in the page,
9+
// app, or any theme specific resource dictionaries)
10+
)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Window x:Class="SDKSamples.MainWindow"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
Title="Example" Width="360" Height="180" SizeToContent="Height" ResizeMode="NoResize" Loaded="Window_Loaded">
5+
<Grid>
6+
<!--<example>-->
7+
<StackPanel>
8+
<TextBlock Width="300" Height="20" Text="Type some text into the TextBox and press the Enter key." />
9+
<TextBox Width="300" Height="30" Name="textBox1" KeyDown="textBox1_KeyDown" />
10+
<TextBlock Width="300" Height="100" Name="textBlock1" />
11+
</StackPanel>
12+
<!--</example>-->
13+
</Grid>
14+
</Window>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//<full>
2+
using System;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using System.Windows;
6+
using System.Windows.Input;
7+
8+
namespace SDKSamples
9+
{
10+
public partial class MainWindow : Window
11+
{
12+
public MainWindow() =>
13+
InitializeComponent();
14+
15+
private void Window_Loaded(object sender, RoutedEventArgs e)
16+
{
17+
}
18+
19+
//<handler>
20+
private void textBox1_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
21+
{
22+
if (e.Key == Key.Enter)
23+
{
24+
textBlock1.Text = $"You Entered: {textBox1.Text}";
25+
}
26+
}
27+
//</handler>
28+
}
29+
}
30+
//</full>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>WinExe</OutputType>
5+
<TargetFramework>net481</TargetFramework>
6+
<UseWPF>true</UseWPF>
7+
<RootNamespace>SDKSamples</RootNamespace>
8+
<ApplicationManifest>app.manifest</ApplicationManifest>
9+
</PropertyGroup>
10+
11+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
3+
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
4+
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
5+
<security>
6+
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
7+
<!-- UAC Manifest Options
8+
If you want to change the Windows User Account Control level replace the
9+
requestedExecutionLevel node with one of the following.
10+
11+
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
12+
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
13+
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
14+
15+
Specifying requestedExecutionLevel element will disable file and registry virtualization.
16+
Remove this element if your application requires this virtualization for backwards
17+
compatibility.
18+
-->
19+
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
20+
</requestedPrivileges>
21+
</security>
22+
</trustInfo>
23+
24+
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
25+
<application>
26+
<!-- A list of the Windows versions that this application has been tested on
27+
and is designed to work with. Uncomment the appropriate elements
28+
and Windows will automatically select the most compatible environment. -->
29+
30+
<!-- Windows Vista -->
31+
<!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />-->
32+
33+
<!-- Windows 7 -->
34+
<!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />-->
35+
36+
<!-- Windows 8 -->
37+
<!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />-->
38+
39+
<!-- Windows 8.1 -->
40+
<!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />-->
41+
42+
<!-- Windows 10 -->
43+
<!--<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />-->
44+
45+
</application>
46+
</compatibility>
47+
48+
<!-- Indicates that the application is DPI-aware and will not be automatically scaled by Windows at higher
49+
DPIs. Windows Presentation Foundation (WPF) applications are automatically DPI-aware and do not need
50+
to opt in. Windows Forms applications targeting .NET Framework 4.6 that opt into this setting, should
51+
also set the 'EnableWindowsFormsHighDpiAutoResizing' setting to 'true' in their app.config.
52+
53+
Makes the application long-path aware. See https://docs.microsoft.com/windows/win32/fileio/maximum-file-path-limitation -->
54+
<!--
55+
<application xmlns="urn:schemas-microsoft-com:asm.v3">
56+
<windowsSettings>
57+
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
58+
<longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
59+
</windowsSettings>
60+
</application>
61+
-->
62+
63+
<!-- Enable themes for Windows common controls and dialogs (Windows XP and later) -->
64+
<dependency>
65+
<dependentAssembly>
66+
<assemblyIdentity
67+
type="win32"
68+
name="Microsoft.Windows.Common-Controls"
69+
version="6.0.0.0"
70+
processorArchitecture="*"
71+
publicKeyToken="6595b64144ccf1df"
72+
language="*"
73+
/>
74+
</dependentAssembly>
75+
</dependency>
76+
77+
</assembly>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8.1" />
5+
</startup>
6+
</configuration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Application x:Class="Application"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
StartupUri="MainWindow.xaml">
5+
<Application.Resources>
6+
7+
</Application.Resources>
8+
</Application>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Class Application
2+
3+
' Application-level events, such as Startup, Exit, and DispatcherUnhandledException
4+
' can be handled in this file.
5+
6+
End Class
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Imports System.Windows
2+
3+
'The ThemeInfo attribute describes where any theme specific and generic resource dictionaries can be found.
4+
'1st parameter: where theme specific resource dictionaries are located
5+
'(used if a resource is not found in the page,
6+
' or application resource dictionaries)
7+
8+
'2nd parameter: where the generic resource dictionary is located
9+
'(used if a resource is not found in the page,
10+
'app, and any theme specific resource dictionaries)
11+
<Assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Window x:Class="MainWindow"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
Title="Example" Width="360" Height="180" SizeToContent="Height" ResizeMode="NoResize" Loaded="Window_Loaded">
5+
<Grid>
6+
<!--<example>-->
7+
<StackPanel>
8+
<TextBlock Width="300" Height="20" Text="Type some text into the TextBox and press the Enter key." />
9+
<TextBox Width="300" Height="30" Name="textBox1" KeyDown="textBox1_KeyDown" />
10+
<TextBlock Width="300" Height="100" Name="textBlock1" />
11+
</StackPanel>
12+
<!--</example>-->
13+
</Grid>
14+
</Window>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'<full>
2+
Imports System.Threading
3+
4+
Public Class MainWindow
5+
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
6+
End Sub
7+
8+
'<handler>
9+
Private Sub textBox1_KeyDown(sender As Object, e As System.Windows.Input.KeyEventArgs)
10+
11+
If e.Key = Key.Return Then
12+
textBlock1.Text = "You Entered: " + textBox1.Text
13+
End If
14+
15+
End Sub
16+
'</handler>
17+
End Class
18+
'</full>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
Imports System
2+
Imports System.Globalization
3+
Imports System.Reflection
4+
Imports System.Resources
5+
Imports System.Runtime.InteropServices
6+
Imports System.Windows
7+
8+
' General Information about an assembly is controlled through the following
9+
' set of attributes. Change these attribute values to modify the information
10+
' associated with an assembly.
11+
12+
' Review the values of the assembly attributes
13+
14+
<Assembly: AssemblyTitle("SDKSamples")>
15+
<Assembly: AssemblyDescription("")>
16+
<Assembly: AssemblyCompany("")>
17+
<Assembly: AssemblyProduct("SDKSamples")>
18+
<Assembly: AssemblyCopyright("Copyright © 2023")>
19+
<Assembly: AssemblyTrademark("")>
20+
<Assembly: ComVisible(false)>
21+
22+
'In order to begin building localizable applications, set
23+
'<UICulture>CultureYouAreCodingWith</UICulture> in your .vbproj file
24+
'inside a <PropertyGroup>. For example, if you are using US english
25+
'in your source files, set the <UICulture> to "en-US". Then uncomment the
26+
'NeutralResourceLanguage attribute below. Update the "en-US" in the line
27+
'below to match the UICulture setting in the project file.
28+
29+
'<Assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)>
30+
31+
32+
'The ThemeInfo attribute describes where any theme specific and generic resource dictionaries can be found.
33+
'1st parameter: where theme specific resource dictionaries are located
34+
'(used if a resource is not found in the page,
35+
' or application resource dictionaries)
36+
37+
'2nd parameter: where the generic resource dictionary is located
38+
'(used if a resource is not found in the page,
39+
'app, and any theme specific resource dictionaries)
40+
<Assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)>
41+
42+
43+
44+
'The following GUID is for the ID of the typelib if this project is exposed to COM
45+
<Assembly: Guid("28ffbf70-7284-4e8e-9904-e70933dc2b00")>
46+
47+
' Version information for an assembly consists of the following four values:
48+
'
49+
' Major Version
50+
' Minor Version
51+
' Build Number
52+
' Revision
53+
'
54+
' You can specify all the values or you can default the Build and Revision Numbers
55+
' by using the '*' as shown below:
56+
' <Assembly: AssemblyVersion("1.0.*")>
57+
58+
<Assembly: AssemblyVersion("1.0.0.0")>
59+
<Assembly: AssemblyFileVersion("1.0.0.0")>

0 commit comments

Comments
 (0)