This is actually one of the easiest solutions that I could think of for the actual issue that I'm having...
I am trying to write a program where the user scans in a barcode on a device, and the program checks the validity of the scanned value, saves the scan in a database, and then prints out a corresponding label for the device. The REAL problem that I'm running into is that if the scan is not valid (the user scanned in the wrong barcode, the barcode has already been used and is in the database, the value scanned doesn't match the regular expression that I'm using to parse the scan, etc.) there is supposed to be an error message that pops up to let the user know what issue the program has run into. However, the problem is that the messagebox (for the error message) flashes on the screen too quickly to actually read and immediately disappears (sometimes it happens so quickly that it looks like nothing has happened at all).
I'm pretty sure that this is because the barcode reader is appending a carriage return at the end of whatever is being scanned. Usually this is a nice feature as many other programs also on that computer require the user to push enter after typing something in. So having the barcode reader automatically append that at the end saves the user from having to hit the enter key after scanning in an item. However for my application, this is causing issues since the data that the user scans in will get processed, determined that there is something invalid about it, post the messagebox, and then accepts the carriage return as the input on the messagebox, thereby executing whatever the default button on that messagebox corresponds to (for most of my error messages, this means it acts as though the "OK" button was pressed and will immediately close the messagebox window; hence why it looks like it's just flashing on the screen for a split second).
I've been trying to capture the key strokes and suppress them if they are like a carriage return, but this doesn't solve the problem (the stuff about checking for ControlKey being pushed is that some barcode readers will append a Ctrl+M to act as a carriage return)...
If I use the PreviewKeyDown event handler, it doesn't recognize "e.SuppressKeyPress = True" since "SuppressKeyPress is not a member of PreviewKeyDownEventArgs". And since I'm using the textbox's TextChanged event handler to process the value scanned, its signature has "e As System.EventArgs" so it doesn't recognize anything (eg it doesn't recognize e.KeyCode, e.SupressKeyPress, etc.).
Because my previous attempts of trying to suppress a carriage return doesn't seem to work, I was trying to think of a different way to ensure that the user is reading the error message. I saw some posts about using a button-less modal window / form that automatically closes after a set amount of time. Although this would be interesting for most of the error messages, I do have one that uses Yes/No buttons; if a user scans in a barcode that is already in the database, it asks the user whether they want to re-print the label for that device. So the other option that I thought of using would be to display a MessageBox but would not have any of the button as default, thereby requiring the user to actually use a mouse to click on their desired answer. But unfortunately I haven't found a way to do that... I've even attempted to set the default button to button3 (thinking that since the messagebox only has 2 buttons, it wouldn't assign either as default), but the program is too smart and will assign button1 as default if it can't find the button listed.
Any ideas on how I can either stop the program from reading the appended carriage return, display a messagebox without having a button as default, or even clearing out the key-strokes buffer after it gets the data that fits the validating regular expression? I'd like to use the existing MessageBox control if at all possible so that I don't have to re-invent something like it just to display the error messages...
I am trying to write a program where the user scans in a barcode on a device, and the program checks the validity of the scanned value, saves the scan in a database, and then prints out a corresponding label for the device. The REAL problem that I'm running into is that if the scan is not valid (the user scanned in the wrong barcode, the barcode has already been used and is in the database, the value scanned doesn't match the regular expression that I'm using to parse the scan, etc.) there is supposed to be an error message that pops up to let the user know what issue the program has run into. However, the problem is that the messagebox (for the error message) flashes on the screen too quickly to actually read and immediately disappears (sometimes it happens so quickly that it looks like nothing has happened at all).
I'm pretty sure that this is because the barcode reader is appending a carriage return at the end of whatever is being scanned. Usually this is a nice feature as many other programs also on that computer require the user to push enter after typing something in. So having the barcode reader automatically append that at the end saves the user from having to hit the enter key after scanning in an item. However for my application, this is causing issues since the data that the user scans in will get processed, determined that there is something invalid about it, post the messagebox, and then accepts the carriage return as the input on the messagebox, thereby executing whatever the default button on that messagebox corresponds to (for most of my error messages, this means it acts as though the "OK" button was pressed and will immediately close the messagebox window; hence why it looks like it's just flashing on the screen for a split second).
I've been trying to capture the key strokes and suppress them if they are like a carriage return, but this doesn't solve the problem (the stuff about checking for ControlKey being pushed is that some barcode readers will append a Ctrl+M to act as a carriage return)...
VB.NET:
Private Sub txtBarcode_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtBarcode.KeyDown
If ((e.KeyCode = Keys.Enter) Or (e.KeyCode = Keys.Tab) Or (e.KeyCode = Keys.Return) Or (e.KeyCode = Keys.LineFeed)) Then e.SuppressKeyPress = True
If Me._boolCtrl Then
Me._boolCtrl = False
If e.KeyCode = Keys.M Then
e.SuppressKeyPress = True
End If
End If
If e.KeyCode = Keys.ControlKey Then Me._boolCtrl = True
End Sub
If I use the PreviewKeyDown event handler, it doesn't recognize "e.SuppressKeyPress = True" since "SuppressKeyPress is not a member of PreviewKeyDownEventArgs". And since I'm using the textbox's TextChanged event handler to process the value scanned, its signature has "e As System.EventArgs" so it doesn't recognize anything (eg it doesn't recognize e.KeyCode, e.SupressKeyPress, etc.).
Because my previous attempts of trying to suppress a carriage return doesn't seem to work, I was trying to think of a different way to ensure that the user is reading the error message. I saw some posts about using a button-less modal window / form that automatically closes after a set amount of time. Although this would be interesting for most of the error messages, I do have one that uses Yes/No buttons; if a user scans in a barcode that is already in the database, it asks the user whether they want to re-print the label for that device. So the other option that I thought of using would be to display a MessageBox but would not have any of the button as default, thereby requiring the user to actually use a mouse to click on their desired answer. But unfortunately I haven't found a way to do that... I've even attempted to set the default button to button3 (thinking that since the messagebox only has 2 buttons, it wouldn't assign either as default), but the program is too smart and will assign button1 as default if it can't find the button listed.
Any ideas on how I can either stop the program from reading the appended carriage return, display a messagebox without having a button as default, or even clearing out the key-strokes buffer after it gets the data that fits the validating regular expression? I'd like to use the existing MessageBox control if at all possible so that I don't have to re-invent something like it just to display the error messages...