C# - DateTimePicker, detect up-down click events - c#

I'm re-writing the C# DateTimePicker (nullable) to fit my needs, but I'm stuck at detecting the up-down events from picking time (HH:mm, DateTimePicker.ShowUpDown = true).
I am aware that the OnValueChanged is fired when the either the up or down button is pressed, but I already have some handling there (move to next field automatically when a number is typed in to the DateTimePicker), and therefor I need to block the ValueChanged event when those buttons are pressed, and handle it in a different function.
I'm trying to override the WndProc function, but OnValueChanged are called before WndProc. OnKeyDown is also called after OnValueChanged.
Any ideas?

Well you could try overriding OnDropDown which occurs whenever the drop down calender is requested.

You should not block ValueChanged event, when value of DateTimePicker is changed. And Up Down buttons are used for changing DateTimePicker value, thus pressing them should raise ValueChanged event.
Consider to change your current handling of this event.
BTW Why are you navigating to next field when value changed? Just assign appropriate TabIndex values to your controls and allow user to use Tab button for navigation to next field. This is a common approach for navigation.

Related

GotFocus event of TextBox is not firing

I'm using Windows.UI.Xaml.Controls.TextBox developing Universal Windows App. When text box is enabled GotFocus event normally fired. Otherwise when it is disabled I can't detect user interactions with it. Somehow I need to know that user is trying to access the text box and notify him why he can't do so. Also I have tried to use Tapped event - no success.
A disabled textbox won't fire the GotFocus event (or any other events, from what I can see).
Consider setting the IsReadOnly property to true instead of disabling the whole element. Then events will still be able to fire.

Why does the LostFocus event get called at different times?

In reference to this MSDN page (or any related page on the matter), it states that:
When you change the focus by using the keyboard, focus events occur in
the following order:
Enter
GotFocus
Leave
Validating
Validated
LostFocus
However, when you use the mouse to raise events, the order changes!
When you change the focus by using the mouse or by calling the Focus method, focus events occur in the following order:
Enter
GotFocus
LostFocus
Leave
Validating
Validated
Wouldn't this make the chain of events completely different? My interpretation here is that the keyboard chain ensures everything is in working order, then raises the LostFocus event. Yet, the mouse events seem to raise it before validating for some reason. Why is that?
As noted above:
In the MSDN article you linked worded strong enough? Never use LostFocus, only Leave.
The keyboard navigation must be in this order in order to apply the validations. Those are intended to react to them in order to validate any input strings.
The best example I can think of is the e.Cancel aspect in validation. Using the keyboard for navigation is usually a control to control type of navigation (including child and parent controls). Using the mouse for form navigation does not always result in a control being selected. For example closing a form or simply click outside of the control (i.e. re-positioning the form). It is not always desirable to have the validation occur when a mouse click occurs outside a control. Hope that helps.

Event for "end edit" in a text box

I am using a textbox in winform (c#) and using the text to make consults in a database.
But I need constantly consult the text of the textbox every time that text changes. So for these, I use the KeyUp. But this event is too slow.
Is any event that just fires when the textbox editing has been finished ?. I consider for finish 2 conditions
The control lost focus.
The control has 200ms without keypress
You could use the LostFocus event, to capture when the user clicks on a control outside the textbox.
Otherwise, you'll need to choose from one of the existing events. (Listed here)
Come to think of it, you will likely have to capture multiple events. DragDrop if someone copies/pastes, for example...
You mean something like this?
Control.LostFocus
Provided that you consider finished as being when they click off the textbox.

TextBox.SelectAll() does not work with TAB

I am using maskedTextBox.SelectAll() to highlight the text in the MaskedTextBox in the Enter and MouseDown events.
It works when I use the mouse, but I go to that textbox by pressing the Tab key, it does not work.
What am I missing here?
Have you tried the GotFocus event?
When you change the focus by using the keyboard (TAB, SHIFT+TAB, and so on), by calling the Select or SelectNextControl methods, or by setting the ContainerControl.ActiveControl property to the current form, focus events occur in the following order:
It then goes on to list the events that are fired. It looks like this fires when the mouse is used so you might only need this handler.

Confirmation before closing

net windows form application. I have a combo box and a text box and a close window button. Now If I make any change in the combo box or textbox and click on the close window button, it should prompt the user to save the modifications.. If no modification are made ( The user will just run the application, doesnt make any modification) then it should not prompt the user. It should close directly.. How can I do this?
An easy way to do it is by adding a dirty member to the form, which I set to true whenever anything changes and then check it whenever the form is closing .
Override the OnClosing method of your form (or attach to the Closing event). In the handler check for modifications and display a message box to the user. If you do not want the form to close just set the e.Cancel property to false before returning.
One way is to keep a bool flag called _changed or something like that as a member variable on your form.
Then in the TextChanged event of the TextBox, and the SelectedIndexChanged event of the ComboBox you just set _changed = true.
Then, just before your form closes you prompt the user if _changed is true.
Edit:
If you have many TexBox controls on the form, you could hook them all up to the same TextChanged event handler. Then, no matter which TextBox's text changed, _changed will be set to true.
Then do the same with multiple ComboBox controls and one SelectedIndexChanged event.
If you really have many controls, rather than hooking each up manually, you could even write a method that recursively loops through the Controls collection of your form and hooks each type of control up to the appropriate event handler. Then you could reuse that method in more than 1 form to save you lots of time and maintenance, as when ever you add new controls, they will automatically be taken care of.

Categories