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.
Related
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.
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...
}
Does anyone know how I could easily detect if the Windows OS IME (Input Method Editor) is active in the Silverlight framwork. Let me explain the scenario which is causing me issues:
I have hit an issue where using a Chinese (Simplified, China) Microsoft Pinyin causes a CLR exception. The scenario is when a TextBox is handling its TextChanged event. For example:
A TextBox should not accept characters but only numbers. In order to achieve that it is handling its TextChanged event and in that event it handles the input by reversing its Text property to the last correct input character by character. In this scenario if an (Chinese (Simplified, China) Microsoft Pinyin) IME is used an a FatalExecutionEngineError is thrown.
In WPF it is possible to overcome this issue by not reversing the Text in the TextChanged event by using the InputMethod.Current.ImeState to check if an IME is active. Unfortunately this is not available in the Silverlight framework which is why I am posting this question.
Currently the only thing I have found is that I could set IsInputMethodEnabled property of the InputMethod class to the TextBox control in order disable all IME input but this of course will not only disable the incorrect input but also the correct one.
Anyone has any ideas how I could detect if a IME is used/active in the Silverlight platform? Thanks.
I was able to resolve the issue in both the WPF and Silverlight frameworks. The issue was caused by the fact that by handling the TextBox Text while a IME is inputting symbols that Text was making the IME itself change its input which it looks like is not handled gracefully by the Windows OS and was causing a CLR exception.
What I did was:
In the WPF framework as mentioned I used the static InputMethod.Current.ImeState value to determine if IME is active and if it was with On value I skipped reverting the TextBox Text property in its TextChanged event.
In the Silverlight framework I use a combination of the TextInputStart, TextInputUpdate events and a local private field to store if IME was detected. The TextInputUpdate event is only triggered if IME is active and used as input and the TextInputStart is always triggered. What I did was:
Created a bool IsImeActive = false; filed
Hook to the TextInputStart event of the TextBox
In that event set the IsImeActive field to False
Hook to the TextInputUpdate event of the TextBox
In that event set the IsImeActive field to True
Finally in the TextChanged event add a condition that checks the IsImeActive field and if it is False run the logic which handles (reverses) the input.
Hope this is helpful.
I also get FatalExecutionEngineError, error code 0x80131623 when some IME is active.
My fix: I could get IME event with
TextCompositionManager.AddPreviewTextInputHandler(myTextbox, OnPreviewTextInput);
TextCompositionManager.AddPreviewTextInputStartHandler(myTextbox, OnPreviewTextInputStart);
TextCompositionManager.AddPreviewTextInputUpdateHandler(myTextbox, OnPreviewTextInputUpdate);
private void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
ImeFlag = false;
}
private void OnPreviewTextInputStart(object sender, TextCompositionEventArgs e)
{
ImeFlag = true;
}
private void OnPreviewTextInputUpdate(object sender, TextCompositionEventArgs e)
{
if (e.TextComposition.CompositionText.Length == 0)
ImeFlag = false;
}
I remove any TextChanged event when current IME is active, then add back event after an IME text is completed:
private bool _imeFlag = false;
private bool ImeFlag
{
get => _imeFlag;
set
{
if (_imeFlag == value)
return;
if (value)
myTextbox.TextChanged -= MyTextbox_TextChanged;
else
myTextbox.TextChanged += MyTextbox_TextChanged;
_imeFlag = value;
}
}
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.
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.