catch input from a magnetic card reader to a variable - c#

I can't seem to find a way to catch the input of a magnetic card reader. When it swipes, the input gets into active text editor, like say a notepad.
Unfortunately, the focus on textbox field won't do the trick, because I'm required to make it a label instead of a textbox. Thus, I need a way to catch the input from the USB device to a variable or label instead.
Does anyone knows of a .NET class I could use to do this or any better ideas?

If it's a winforms app you could do
private void Form1_Load(object sender, EventArgs e)
{
KeyPreview = true;
KeyPress += Form1_KeyPress;
}
private bool inputToLabel = true;
void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (inputToLabel)
{
label1.Text = label1.Text + e.KeyChar;
e.Handled = true;
}
else
{
e.Handled = false;
}
}
and as long as the window has focus, the keypress characters will go to the label's text.

I don't think there will be anyway for you to prevent the user from manual input. I suspect the card reader that you have emulates a keyboard. So, to be able to read from the reader, you must receive keyboard input, and keyboard input means the user can type anything they like.
A possible solution is to change your card reader to one that uses an API to read from cards.
If getting a better card reader isn't an option, I think the best method to do this is to have a button. When the button is clicked, open a new form that contains the code #Bala R provided. But in addition, close the form within 1 second from the first key input. This will prevent users from tampering the input manually, but will provide sufficient time for the reader to complete.

Related

C# Windows Forms - Beginner Guidance Required - Enabling button only when two text boxes have characters entered

I am designing a program in Visual Studio - Windows Forms c#.net framework to log Gamertags and High Scores of entrants into a gaming competition. I have a textbox for a user to enter a Gamertag and a textbox for a user to enter a High Score, and then a button to log the Gamertag and High Score into a separate two lists.
The issue im having is around disabling the button until both the Gamertag and High Score fields have some text in them. This will be insultingly easy for a lot of you but I am not sure how to write the code to make this happen, I will show what I have:
So on initialise component I have the following:
{
btnAdd.Enabled = !string.IsNullOrEmpty(txtHighScore.Text);
btnAdd.Enabled = !string.IsNullOrEmpty(txtUsername.Text);
}
Button is disabled as soon as the program launches until text is entered.
Then further down I have code on the TextChanged sections of each text box
private void txtHighScore_TextChanged(object sender, EventArgs e)
{
btnAdd.Enabled = !string.IsNullOrEmpty(txtHighScore.Text);
}
private void txtGamertag_TextChanged(object sender, EventArgs e)
{
btnAdd.Enabled = !string.IsNullOrEmpty(txtGamertag.Text);
}
From reading this you will see that if text is entered into either box the button will be enabled, but I only want it to be enabled if text has successfully be input into BOTH fields.
You could set both textboxes TextChanged events to this method called DataChanged (you can choose the name you please)
private void DataChanged(object sender, EventArgs e)
{
btnAdd.Enabled = !string.IsNullOrWhiteSpace(txtHighScore.Text)
&& !string.IsNullOrWhiteSpace(txtGamertag.Text);
}
As you can see you have to use both checks together to enable button and for this you can take advantage of && which means and.
Naturally you could write both events with the same commands, but using just one method for both events makes clear what you're doing and there's only one point in code where you can make changes if necessary, so code is more maintainable.
Your code is not working because you enable/disable button using just one textbox at once.

TextBox method for checking numerics in C#

I am creating a program that has a lot of user inputs. Most of the user inputs are going to be in TextBoxes that need to be only numeric entries.
Currently, I am just using a TextChanged method for getting values, which then make other buttons/checkboxes show/hide based on the entry.
I am wanting to create a method or implement some kind of utilization that checks when is being inputted into the boxes, to either prevent people from making incorrect inputs, to fix changes that they had made, or to create a messagebox that will tell them that their input is invalid.
I have two ways I am currently working with but they don't work with each other.
I have a parse method, that converts the input text into a Double but the problem I am running into, if they utilize the backspace button then re-enter their numbers, it will not recognize the input (which is needed to open/close other textboxes/checkboxes). This does work with the TextChanged method.
I have a regex set that utilizes the PreviewTextInput and KeyDown methods. This works pretty well with not allowing certain inputs but it doesn't work with the textchanged method (or at least I don't understand how to point to it).
I am in need of some guidance on how to create a viable method for checking inputs into textboxes that doesn't require my users to press a button for each entry (aka checking real-time).
I think this is what you are looking for.
Binding.Validation
For an Int it is as easy as just binding to an Int.
If you need to be able to increase/decrease the value via button use NumericUpDown or one of its subclass.
If you just need a textbox, you have to handle PreviewKeyDown() event. You need to manually check for valid/invalid keys pressed. When an invalid key is pressed, you set e.Handled = true; to prevent the key down event from tunneling down.
I really couldn't understand completely, but according to me you are trying to prevent a textbox to take invalid input and at the same time you want to use TextChanged method, so you can do like this:
<TextBox Name="txtAddNumber" TextChanged="txtAddLable_TextChanged" PreviewTextInput="txtAddNumber_PreviewTextInput" />
And txtAddNumber_PreviewTextInput method:
private void txtAddNumber_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
char c = Convert.ToChar(e.Text);
if (!Char.IsLetter(c))
e.Handled = false;
else
e.Handled = true;
base.OnPreviewTextInput(e);
}
And if you want to handle some error message kind of thing on the base of input you can do like this:
private void txtAddNumber_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
char c = Convert.ToChar(e.Text);
if (!Char.IsLetter(c))
{
// Put your Logic here according to requirement
e.Handled = false;
}
else
{
// Put your Logic here according to requirement
e.Handled = true;
}
base.OnPreviewTextInput(e);
}
And
e.Handled = false means input is numeric and e.Handled = true means input is non-numeric.
And your txtAddLable_TextChanged method will bw like:
private void txtAddLable_TextChanged(object sender, TextChangedEventArgs e)
{
// Logics here...
}

Disabling a Textbox Using TextChanged Event

The form I am using requires a copy pasted URL. I am trying to have a textChanged event that will check the url as soon as it is pasted, telling the user whether it is valid or invalid. I also want to be able to lock out the textbox when this happens, with a message saying something like "Processing...".
The problem is with the code below, the textbox is never disabled, the program will do the checkUrl() method and the textbox is never disabled even though it is first to execute (I assume it is but the fact there is a function call right underneath it is messing around with something or getting higher priority).
How do I go about making the control visually disabled while the method runs?
private void urlTxtBx_TextChanged(object sender, EventArgs e)
{
urlTxtBx.Enabled = false;
checkUrl();
urlTxtBx.Enabled = true;
}
I think this is happening because the Application needs to complete all the active threads before disabling the TextBox. Please try the following code:
private void urlTxtBx_TextChanged(object sender, EventArgs e)
{
urlTxtBx.Enabled = false;
Application.DoEvents();
checkUrl();
urlTxtBx.Enabled = true;
}
This will let the UI to be updated. For more details check here.

Unable to suppress keys in a WinForms Application

I am using one RichTextBox in C# Windows Application. I need to restrict the user from entering any key from keyboard at certain conditions. so, I wrote the following line of code in the KeyDown event of the RichTextBox:
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
e.SuppressKeyPress = true;
}
This is working fine for English US keyboard. But when I changed my regional settings to Korea and keyboard language to Korean, I can enter the Korean characters (like ?????) even after executing the above statement.
even i have tried with the following code
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
e.SuppressKeyPress = true;
e.Handled = true;
}
but in vain.
How can I suppress input with all keyboard layouts?
Do you want to actually suppress keys, or do you want to prevent the user from editing the text?
Editing the text can easily be prevented with richTextBox1.ReadOnly = true;
Your code probably doesn't work because input via IMEs doesn't count as key press.
Yet another alternative is trying to block in the WM_CHAR/KeyPress handler.

RichTextBox as output and TextBox as input on one form, how to select from output while maintaining focus on input?

I have a RichTextBox as output and TextBox as input on the main WinForms form. I would like to be able to keep focus on the TextBox while highlighting text in the output with the mouse. That would allow me, as an example, to type something in the input and simultaneously select something in the output with the mouse.
I saw this done in one application which isn't necessarily WinForms based, but it does run on a Windows machine.
How can I do this with WinForms?
You can try something along the lines of
bool selecting;
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
selecting = true;
}
private void richTextBox1_MouseUp(object sender, MouseEventArgs e)
{
if (selecting)
textBox1.Focus();
selecting = false;
}
This resets the focus on the TextBox as soon as you finish selecting from the RichTextBox. The problem however is that, as soon as the focus is restored, the selection is cleared.

Categories

Resources