Set focus of an element from its lost focus - c#

For some reason focus a text box is need, so the focus is again set to the text box from its lost focus. This leads to stack over flow in the lost focus event.
Say,
A Textbox and a button, now the focus is in Textbox and clicking on the button. In this case, need the focus to be in the textbox, so focus method of text box is invoked from its lost focus. This leads to stack over flow exception.
Why this happens and also how do the focus is retained in text box itself.
By handling PreviewKeyboardLostFocus of textbox, i have retained the Keyboard focus but how to retain the mouse focus in this?

Check if you had handled SetFocus() Event, You might have handled something there leading to StackOverflowException.
Cheers!

Related

WinForms txtTextbox_MouseEnter firing for all textboxes on form

I have a form with five textboxes. On a button click event I check if any of the textboxes are empty and if so, the empty textboxes have the background color set to LightPink.
I want to set the background color back to the default when the user enters the textbox to enter a value. I tried txtTextbox_MouseEnter to do that but the event is firing when other textboxes get focus with a mouse click. Oddly, the txtTextBox_MouseEnter event doesn't always fire when any textbox is clicked on - just sometimes - can't seem to determine a pattern in that behavior.
Why is this happening and what's the correct way to accomplish my goal?

How to keep keyboard focus on textbox?

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.

What is the property that says a button was selected in c#?

i've been searching on internet for the property that says that a normal button was selected or not, i think there must be one because when you click a button, it turns light blue, regardless the mouse is over it or not, and when you click another button, the previous button changes back to normal and the new clicked button is set light blue.
I need it to know which button was just selected and draw a "resizing" square on it, and it gotta last as long as the button remains as the "selected one".
thanks in advance.
What you are looking for is the Focused property. For actually any Control it returns, whether it has input focus or not (so e.g. hitting Enter will cause the button to be clicked as well). Since it sounds like you want to be notified whenever that property changes, you should use the GotFocus and LostFocus events.
You can give a Control focus programmatically by calling Focus.
you can do two things,
you can do it with the events: mouseEnter and MouseLeave,
this events launches when the mouse enters or leaves the visible control area,
also the focused(boolean) property gets and sets the "selected" element
one solution could be to:
private void onElementMousenter(blablabla,sender e);
{
e.Focused=true;
}
and assign the mouseEnter events in all controls to this one so the last element will be the "selected one" until you select another

In Winforms, why is validation not being fired after leaving TextBox and entering a DataGridView?

I'm overriding the OnValidating event in a custom Winforms text box. I'm finding that if the text box (which is bound to an object) has focus and then I give a grid focus using the mouse, the OnValidating event doesn't always get fired. When I first give the grid focus, it gets fired fine. But, if put one of the grid's cell in edit (blinking cursor), from there on out it seems to not get fired when I go back between the text box and grid using the mouse. If I change focus using the tab key, the validating always gets fired. If I give focus to a non-grid control using the mouse, the validation is always getting fired.
I tried to recreate this functionality from scratch in a simple form and I can't recreate the problem. The grid I'm using in the setup where I'm getting the problem is a custom DataGridView with custom column types. I'm wondering if the grid is the problem. But, I don't see how it could affect the text box events. Any ideas?
It probably has to do with the CausesValidation property.
A control's validation is suppressed if focus is going to a control that has CausesValidation set to false. It's just a wild guess, but I'm thinking some control inside the grid has CausesValidation = false;
This property is meant for things like "Cancel" buttons, but can cause lots of confusion.

how to lose control from text box when clicked outside

I have a textbox in a windows form. Currently the focus is on the textbox and i enter some text. Now I click outside the textbox but within the window. This action does not make the text box to lose the focus. The cursor still blinks in the text box. If the click was on another control then the text box would lose the control.
How would I make the text box to lose control when clicked outside of it (not just on another control but anywhere inside the form)?
Thanks in advance.
Datte
Because you click on a control that has no ability of taking the focus (like a form, a label, etc). If you click for instance on another text box the focus should move..
To move the focus programmatically (i.e. in the OnClick event of the Form) use the control.Focus method.

Categories

Resources