Mostly new devices like magnetic card scanner,barcode scanner they connect via usb and are treated like HID(Human interface device) or roughly as a keyboard ,who perform two operations during reading card value or barcode value.
1)The first operation is reading and returning output as string
2)Sending an key event,It's mostly enter key ()
So now you should have a wider picture of their implementation in your software,and your final solution would be next:
1)Define the control that is to receive the output of magnetic card or barcode scanner as an input
2)Do not let that control lose keyboard focus at any moment
3)Do a HID key filtering for numbers or letters in your case for numbers
4)Do a HID enter key filtering and rise logic for submit in order to save record in the database
a simple solution would be:
xaml
Code
<Window x:Class="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:hidtest"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded" WindowStartupLocation="CenterScreen" >
<Grid>
<StackPanel Margin="20" Width="300">
<TextBlock Text="Barcode or magnetic card input: "/>
<TextBox x:Name="BarcodeOrCardInput" Height="20" KeyDown="BarcodeOrCardInput_KeyDown" LostKeyboardFocus="BarcodeOrCardInput_LostKeyboardFocus" />
<Button x:Name="btnSubmit" Content="Submit" Margin="5" Click="btnSubmit_Click"/>
</StackPanel>
</Grid>
</Window>
code
Code
Class MainWindow
Inherits Window
Public Sub New()
InitializeComponent()
End Sub
Private Sub BarcodeOrCardInput_KeyDown(sender As Object, e As KeyEventArgs)
Dim key = CType(e.Key, System.Windows.Input.Key)
e.Handled = Not (key >= System.Windows.Input.Key.D0 AndAlso key <= System.Windows.Input.Key.D9 OrElse key >= System.Windows.Input.Key.NumPad0 AndAlso key <= System.Windows.Input.Key.NumPad9 OrElse key = System.Windows.Input.Key.Back OrElse key = System.Windows.Input.Key.OemPeriod OrElse key = System.Windows.Input.Key.Decimal OrElse key = System.Windows.Input.Key.Tab OrElse key = System.Windows.Input.Key.F1 OrElse key = System.Windows.Input.Key.F2 OrElse key = System.Windows.Input.Key.F3 OrElse key = System.Windows.Input.Key.F4)
If e.Key = Key.Enter Then
Me.btnSubmit.[RaiseEvent](New RoutedEventArgs(Button.ClickEvent))
BarcodeOrCardInput.Text = String.Empty
Keyboard.Focus(BarcodeOrCardInput)
End If
End Sub
Private Sub btnSubmit_Click(sender As Object, e As RoutedEventArgs)
MessageBox.Show("record is saved in db")
End Sub
Private Sub BarcodeOrCardInput_LostKeyboardFocus(sender As Object, e As KeyboardFocusChangedEventArgs)
Keyboard.Focus(BarcodeOrCardInput)
End Sub
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
Keyboard.Focus(BarcodeOrCardInput)
End Sub
End Class
if you need a different way of implementation you must use one of barcode api to achieve the goal