I have created a program in Visual Basic using Microsoft Visual Studio 2010 Professional. There is a textbox that I use in the program that when the focus is placed on it will take the data encoded on a magnetic striped card such as a driver's license or any card that has this kind of data encoded magnetically. Very simple to get the data as the program takes all of it upon the swipe without having to do really anything, however, the problem is that I don't know which declaration to use when the swipe has stopped - or the last character has been entered. Currently I am using TextBox1_textChanged as my declaration, but that is called every time each character is put into the text box. When you are swiping, this is an ongoing process until the last character is put in. So if I have a magentic card with 10 characters in it, this method will be called 10 times. Is there a declaration that I can use that only fires off whent he swipe has loaded all of the characters or some kind of loop that I can incorporate? Thanks!
-
You can use that event but each time the event fires check if the string is 10 characters long before proceeding. – Reafidy Feb 15 '12 at 03:06
-
If it's a variable length string and there's no specific character at the end of each string such as a return character, then have a look here: [Scanner Timer](http://stackoverflow.com/questions/4614680/problem-with-barcode-scanner-reading-value-into-text-box) – Reafidy Feb 15 '12 at 03:11
-
The problem with trying to see if the string is a certain number of characters, is that if a card has a longer amount of characters, then the code fires off when it reaches the amount you place in. I did try that because my codes are 37 characters long, but credit cards, etc... are longer and fire off code when they reach the 37 mark and I don't want to do that. I also looked at the scanner timer code your presented, so thank you. – MeisterPlans Feb 15 '12 at 14:42
2 Answers
In short, the answer is no - All the card reader does is present itself to the computer as a special keyboard - when you swipe the card, it "types".
Because of this, you don't usually get any additional information about the state (failed swipe, swipe complete, etc.)
Some card readers will perform a carriage return at the end of the data. Depending on your circumstance, you may know the data will always be the same length.
In either case, you'll still need to hook the TextBox.TextChanged
event - just have a little bit of logic which checks if the key press is enter/the textbox text is the correct length.
(NB: If checking for an Enter
or another key, it will probably be easier to use the TextBox.KeyDown
event. Have a look at the e
variable which contains information about the key being "pressed")
As mentioned in the comments on the OP by @Reafidy, the last alternative which is guaranteed to work but a bit messier to implement is to use a timer which will fire your method after n milliseconds without a change to the text.
Incidentally, I'm assuming you mean VB.Net not VBA (VB for Applications - eg Excel Macros). I've edited the tags accordingly.
-
Remember also that some MSR's don't even present as a Keyboard, they still require you to connect to the COM Port, and in that case you could manage the reading etc via your own methods. – Paul Farry Feb 15 '12 at 03:45
-
@PaulFarry Very true - I guessed at a keyboard-style one from the OPs "a textbox that I use in the program that when the focus is placed on it will take the data" - if it's a sifferent style of MSR, the answer is completely different – Basic Feb 15 '12 at 08:53
-
this is a usb magtek swipe reader, so it is acting like a keyboard - I have found that the textBox.keyDown method is not working with this, but I have chosen to look for a character at the end of the string, and also to make sure that the string is of a certain length. I'm using a stored procedure to do this that Visual Basic .net is calling and therefore getting around the limitations of VB. Thanks for all the suggestions! It really got my brain focused on the right answer. – MeisterPlans Feb 15 '12 at 14:45
-
@MeisterPlans This won't be a limitation of VB.Net - If anything, it's an issue with the scanner. If needs be, you can use VB.Net to access the lower-level keyboard API (See the answer to my Q here: http://stackoverflow.com/questions/3441963/capture-all-keyboard-events-using-vb-net). Note that calling a stored proc in a database is a HUGE overhead compared to doing something in VB - especially if you're doing it every time a key is pressed... There is almost certainly a better way to do it than DB Stored Procs! If you'd like more help, either edit your Q or start another. – Basic Feb 15 '12 at 17:24
Further on the answer above, my experience is that most scanners pretty much simulate someone typing on the keyboard and hitting the enter key.
If you make your form work as though someone was typing and hitting enter, the scanner should work correctly.
A good option for this is to create a button (like an OK button) and set it as the Forms AcceptButton (in your form properties, set AcceptButton to your OK button)
Put the code inside your OK button and it'll then be intuitive when the scanner doesn't work and people have to hand type the code in and hit OK.

- 3,989
- 3
- 27
- 41
-
I can't use the OK button option because my customer does not want their customers to interface with the program other than swiping their card. It would be much easier if I could have an OK button, because I wouldn't have this issue. – MeisterPlans Feb 15 '12 at 14:46
-
@MeisterPlans The suggestion here isn't to have a button that the user clicks, but rather to have a button (can be hidden) which the form associates with the user pressing enter. Then, when the reader presses "Enter" at the end of reading a barcode (assuming it does), it would effectively press this button. I'd personally prefer to do it in code and avoid a useless button but it's still a valid solution assuming the reader adds a carriage return. – Basic Feb 15 '12 at 17:26
-
@Basic, ok, thanks. Enter gets pressed somehow before the swipe ends, so I don't think that will work, but as stated above, I have resolved my temporary issue and will probably just delete this question as I kind of steered around an actual solution using Visual Basic. – MeisterPlans Feb 24 '12 at 01:27
-
@MeisterPlans Delete if you feel you must but these answers may save someone else some time. Even better would be to post the proper solution as an answer, then accept it. Either way, good luck – Basic Feb 24 '12 at 09:38
-
@Basic - Yes, you are probably right. I will leave it. Thanks again. – MeisterPlans Feb 25 '12 at 19:04