4

I have a textbox which has keydown event when entered key is "return" I have a barcode reader who read text into it but it is not writing more than one key i.e. only one letter is written lets say "a" and if i write second letter "a" is overwritten to become "b" but does not become "ab". Does anyone know what is cause of this ?

private void barcodetexbox_KeyDown(object sender, KeyEventArgs e) { if (scannedString.Text != "" && e.Key==Key.Return) { //do something } } 

and in "MainWindow.xaml"

<TextBox x:Name="scannedString" HorizontalAlignment="Left" Height="50" Margin="468,164,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="450" FontSize="24" Focusable="True" Padding="0,6,0,0" KeyDown="barcodetexbox_KeyDown" /> 
5
  • What does do something do? Commented Jan 28, 2013 at 13:25
  • it opens a new window based on barcode when i am copy pasting it works but when i try to write barcode characters my self it writes only one character any solution to it Commented Jan 28, 2013 at 13:26
  • 3
    sounds like you are doing something like barcodetextbox.Text = e.Key; instead of barcodetextbox.Text += e.Key; Commented Jan 28, 2013 at 13:27
  • It would help if you could show us because I suspect exactly the same as paul just said above. Commented Jan 28, 2013 at 13:31
  • no i am not doing barcodetextbox.Text = e.Key any where Commented Jan 28, 2013 at 13:32

2 Answers 2

4

The KeyDown event is designed to let you know which keys are down at one moment, and your barcode reader seems to simulate a keyboard, so you'll need to concatenate the characters it sends

in your Key_Down event, you would have to do something like that:

this.scannedString += e.Key;

and when you see return:

barcodeTextBox.Text = this.scannedString;

Sign up to request clarification or add additional context in comments.

5 Comments

so i cam i write 9 or 10 characters in my textbox
barcode textbox should show whole barcode and when it reutrn enter show another window , according to you there is no way to show whole barcode in textbox ever
there is a way, you just can't do it immediately if the only way that you get your characters is via the Key_Down event, could you post the code that gets the data from your barcode reader?
there is no code for that it just automatically reads whole barcode character one by one and add enters when it finishes to whereever cursor is located and can be written
btw, if your barcode reader really simulates a keyboard, I think you don't even need to set the string in the textbox, you just have to check if "Enter" has been pressed (since the cursor should be in the textbox at that moment, the text should be written automatically)
2

Not sure if I've understood your problem, but i think this is your solution:

private void scannedString_PreviewKeyDown(object sender, KeyEventArgs e) { if ((sender as TextBox).Text !="" && e.Key == Key.Return) { MessageBox.Show((sender as TextBox).Text); // I mean do some thing (sender as TextBox).Clear(); } } 

I've tested it with a bar code scanner and it works good.

1 Comment

i want to choose yours one answer too but ther is no option for multiple answer :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.