How to keep keyboard focus on textbox? - c#

Throughout the app I need to use ENTER to advance to next control (same as Tab), and I do that by using EventManager.RegisterClassHandler, KeyDownEvent for textboxes in Application_Startup. But for one particular textbox (which is the input for a barcode scanner), I want to keep the focus to allow multiple scannings. So it has to receive a number, process it, then clear the box and keep the focus for the next read. Barcode reader is automatically sending an ENTER at the end of a read. Textbox must still be able to lose focus by user's choice (like mouse clicking in another box)

Explicitly handle the PreviewKeyDown on that box and set Handled to true on the event arguments. It executes before any KeyDown handlers.

Related

Can you make a text box on a C# form act like an excel box?

I want the user to be able to type like = or whatever and then write an equation. Once they click off the box, I want the textbox to show the answer.
For example they could put =2+2 and it would display 4 when not selected.
I have no idea how to do this at all or even where to begin. I can get the textbox to calculate it somewhere else but not in the box itself where the equation is written or without having the user press a button.
What you're probably looking for is the LostFocus event. This event fires whenever a control loses focus (for example when a user tabs out of a text box). If you listen on this event for your text box, you can then call whatever processing code you want to handle any calculations required by the value of the text box and then set its value to the result of the calculation.

Textbox leave event unless tab changes

Using c# winforms vs2008
I'm using a TabControl on a form. Each tab has a different set of input fields. Some of those fields has to be completed. Such as Username. Multiple users will use the same PC, so a the username must remain static. I have a leave event on the require fields which triggers when the textbox loses focus to check if a value was added. In the case of Username, a popup would then presents possible values. That works pretty awesome to ensure accuracy and speed. To problem comes in when the user clicks on another tab, the textbox leave event triggers. How can I prevent the leave_event code from running if the focus changes to a control not on the current Tab?
I have tried different event handlers, but the leave event seem to occur first. I tried textbox validating event handler, but also no success. I tried adding a If-statement in front of the code to check the tab number, or tabcontrol state - no joy - as before anything else changes, the leave event fires.
The only remaining thing I can think of is to loop through all the controls on the tab to check if they have focus, but that just feels messy.
Any other suggestions?
I have found a partial solution. Using a Mouse_Enter and _Leave event on the tab, I set a flag to determine whether the mouse was clicked in the form or outside. If the flag is set to false, the leave event dont run. This will not solve short cut key presses though. But that I can handle later.

textbox with textchanged event firing

I have two textboxes with textchanged events. When I enter characters in first textbox and press enter, the textbox1_textchanged event is fired. Then I enter some characters in the second textbox and press enter, the textbox2_textchanged event fires as well. However, when I move my cursor in first text box without changing any characters and pressed enter, the events won't fire for both textboxes.
I want the respective textbox events to fire without changing any characters in textboxes.
If I press enter with some characters in textbox1/textbox2 its txt_changed events have to fire.
I have tried, but if I enter characters in textbox2 first the textbox1_textchanged get fired.
Any help?
Since you haven't tagged with a webforms/mvc tag I'm going to go ahead and assume this is a WinForms question, in which case look at:
http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox%28v=VS.100%29.aspx
See the "Events" section for a list of all the events a textbox supports.
My guess is you'll want to focus on Enter and Leave events, as well as your text_changed events that you already have.
I'm also assuming (I'm assuming a lot since you've specified little) that you have the latest version of .NET at hand and not something from the dark ages, so I've pointed you at the .NET 4.0 reference.

is there a way to identify the last control that was active in a .net windows forms code-behind?

I have a form where there is group of radio controls and some with textbox.
If the cursor is on textbox and i try switching the cursor to a different radio button, i should be able to identify the last active control( in this case.. the textbox) and do some validation.
The LostFocus() event of textbox pops up message indicating that "this item should be filled in..".
But if i want to go with a different radiobutton option in the same group, i dont want this message popping unnecessarily.
How do i avoid that?
The TextBox has Validating and Validated events-- you should use those instead of the LostFocus event. In the case of Validating, you can stop the user from leaving the TextBox if the criteria isn't correct. If you must use "something" like LostFocus, use the Leave event instead.
There is no "Last Active Control" type function. You would have to track that yourself by setting a variable on the Enter event of those controls.
That will lead to an ugly mess to maintain in my opinion. Validate at the form level is probably the best choice for the end user.

C# Auto Clearing Winform Textbox

I have a user that want to be able to select a textbox and have the current text selected so that he doesn't have to highlight it all in order to change the contents.
The contents need to be handle when enter is pushed. That part I think I have figured out but any suggestions would be welcome.
The part I need help with is that once enter has been pushed, any entry into the textbox should clear the contents again.
Edit: The textbox controls an piece of RF hardware. What the user wants to be able to do is enter a setting and press enter. The setting is sent to the hardware. Without doing anything else the user wants to be able to type in a new setting and press enter again.
Hook into the KeyPress event on the TextBox, and when it encounters the Enter key, run your hardware setting code, and then highlight the full text of the textbox again (see below) - Windows will take care of clearing the text with the next keystroke for you.
TextBox1.Select(0, TextBox1.Text.Length);
OK, are you sure that is wise? I am picturing two scenarios here:
There is a default button on the form, which is "clicked" when enter is pushed".
There is no default button, and you want the user to have to press enter, regardless.
Both of these raise the same questions:
Is there any validation that is taking place on the text?
Why not create a user control to encapsulate this logic?
If you know the enter button is being pushed and consumed fine, how are you having problems with TextBoxName.Text = string.Empty ?
Also, as a polite note, can you please try and break up your question a bit? One big block is a bit of a pain to read..

Categories

Resources