Provide Quick Keyboard Access to Buttons : Key Event « Windows Presentation Foundation « VB.Net






Provide Quick Keyboard Access to Buttons

Provide Quick Keyboard Access to Buttons
    
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="100" Width="300">
    <StackPanel HorizontalAlignment="Center" Orientation="Horizontal">
        <Button Click="SharedButtonClickHandler" Height="23" Margin="5" 
                Name="button1" Width="75">Button _One</Button>
        <Button Click="SharedButtonClickHandler" Height="23" Margin="5" 
                Name="button2" Width="75">Button _Two</Button>
        <Button Click="SharedButtonClickHandler" Height="23" Margin="5" 
                Name="button3" Width="75">Button T_hree</Button>
    </StackPanel>
</Window>

//File:Window.xaml.vb
Imports System
Imports System.Windows
Imports System.Windows.Controls

Namespace WpfApplication1
  Public Partial Class Window1
    Inherits Window
    Public Sub New()
      InitializeComponent()
    End Sub

    Private Sub SharedButtonClickHandler(sender As Object, e As RoutedEventArgs)
      Dim source As Button = TryCast(e.OriginalSource, Button)

      If source IsNot Nothing Then
        Dim message As String = [String].Format("{0} was pressed.", source.Content)
        MessageBox.Show(message, Title)
      End If
    End Sub
  End Class
End Namespace

   
    
    
    
  








Related examples in the same category

1.Creating a KeyBinding between the Open command and Ctrl-RCreating a KeyBinding between the Open command and Ctrl-R
2.Command Handler Key BindingCommand Handler Key Binding
3.Use Keyboard.Focus to set the focus to a Text FieldUse Keyboard.Focus to set the focus to a Text Field
4.Reading keyboard modifiersReading keyboard modifiers
5.Reading individual key state with Keyboard.IsKeyDownReading individual key state with Keyboard.IsKeyDown
6.Use InputBinding to bind Application commends to Key eventsUse InputBinding to bind Application commends to Key events
7.Suppress Keyboard and Mouse EventsSuppress Keyboard and Mouse Events
8.Query Left / Right control keyQuery Left / Right control key
9.Is Key.CapsLock ToggledIs Key.CapsLock Toggled
10.Keyboard.IsKeyToggledKeyboard.IsKeyToggled
11.Query Number lock keyQuery Number lock key
12.Use KeyBinding to bind Key event to TextBox.InputBindingsUse KeyBinding to bind Key event to TextBox.InputBindings
13.Custom Command by KeyGesture and RoutedUICommandCustom Command by KeyGesture and RoutedUICommand
14.TextBox PreviewKeyDown, PreviewKeyUp, PreviewTextInput, KeyDown, KeyUp and TextChanged eventsTextBox PreviewKeyDown, PreviewKeyUp, PreviewTextInput, KeyDown, KeyUp and TextChanged events
15.StackPanel PreviewTextInput and PreviewKeyDownStackPanel PreviewTextInput and PreviewKeyDown
16.If input is not a number do not handle the key eventIf input is not a number do not handle the key event
17.RoutedEvents: Tunneled Key PressRoutedEvents: Tunneled Key Press
18.On Key Down HandlerOn Key Down Handler