WinForms txtTextbox_MouseEnter firing for all textboxes on form - c#

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?

Related

Triggering event in C#.NET Form by tabbing into field

I am designing a form using C#.NET. Whenever a user selects a text field to fill out an event triggers that shows a picture corresponding to the field they are filling out. However, some users like to use the tab key to move through the form. When tabbing through the text fields, the event is not triggered and the picture does not show up. I need to know how to get .NET to trigger an event when a user uses the tab key to move into a field.
Any event with the word "Click" in it will generally only be fired by a mouse action which is why it is not being fired by the user using the tab key.
The Enter or GotFocus event will be fired by both the mouse and the keyboard when the cursor enters the control. Just create the _Enter event for your textbox and move your code into it.
If you want to remove the image when the textbox loses the cursor just use the _Leave event.

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

TextChange in Gridview Textbox prevent button event

I have a text box on a Gridview which I'm allowing some data to be edited. When the enter key is hit, the TextChanged event happens, like I'd expect. But when i change the data and click a button, the button effect not happen. I must click the button again to fire button event. I think it happen because the grid didnot lost it focus.
How to make it possible fire the button event just in one click?
You can change your gridview element's IsTabStop property to false to gain that effect.
Also if you want to cycle with Tab you can TabNavigation="Cycle" .
Both changes are in xaml.
Not much we can do to help you here without the code itself but here are couple things you could try to debug this:
Try different browsers – it may be a browser issue
Try some client side debugging – just open console and see if anything happens when you click the button for the first time

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.

problem with radio buttons

The problem like this, I have a groupBox which contains two radio buttons, when I run the form, the first radio button get checked immediately, so I tried the following:
Set the check property for this radio button to false in Load form.
Set the check property for this radio button to false in the form constructor.
Change the tab index property for this radio button, the selection moved to the next radio button in the form.
None of the above worked with me, any suggestions??
You could try setting it to false in the form SHOWN event instead of the form LOAD event as outlined in this question.
As soon as any of the radio buttons get focus it'll be selected, so you need to set initial focus in the form to another control than any of those radio buttons (worst case I suppose you could have a hidden radio button or other control and give that focus, but I'd not recommend it since it looks funny).
The intent of a radio button set is to provide choice between a set of distinctive and exhaustive values.
That means, that at all times, one and only one radio button should be selected.
If this functionality does not suite your application logic, maybe the logic is flawed, or maybe radio-buttons are not the best UI solution.
As mentioned, a radio button group displays its behavior as soon as any of the radios get focus, which can happen even with just tabbing around the form, so basically the behaviour of the form depends on the user behaving nicely.

Categories

Resources