Can't seem to get Timepicker to work (C# / WPF) - c#

I'm sure I'm missing something very easy here. Pretty much any internet search for a Timepicker for C#/WPF points to this webpage: http://jobijoy.blogspot.com/2007/10/time-picker-user-control.html and as such I figured it would work correctly. However, when I copy/paste this into a user control and run it, the control shows up as it should, but when I click on the digits and press Up or Down, nothing happens. Even in debug mode with a break point on the switch case for the KeyDown event, nothing happens. It's not registering the KeyDown event. It's supposed to focus on the grid that contains the TextBlock, so I tried changing the KeyDown to the TextBlock, but to no avail. I cannot seem to get this to work! :( I'm using Visual Studio 2008.

There is an official Microsoft DateTimePicker control included in the WPF Toolkit. This will be part of the framework in .NET 4.0

I think you're the control is not getting focused, for some reason. I tried it, too, and the event wouldn't fire for me, either. This post might offer some insight. Manually setting focus to the user control in the Loaded event didn't work for me, though.
The only thing that did work for me was doing all of the following:
changing the TextBlock to a TextBox and moving the event there (focus is pretty evident with a TextBox)
changing the cast in the event to FrameworkElement (which is where the Name property comes from) instead of Grid
changing the case to the TextBox's name (instead of the grid's name)
changing the event to a PreviewKeyDown (to get the cursor keys to register)
Of course, this only got the event to fire and register properly, the values don't seem to show up (even before I changed the code), but it handles the specific issue of the event not firing.

There's a DatePicker and a TimePicker in silverlight 4
If you want you can create your own control with those or simply use each one separated.

Related

How do I trigger a "TextChanged" event using tablet pc input panel?

I am working with a rather complex WPF control; it is a button that, when pressed, creates a Popup, with a TextBox set as the Popup's content. (If it matters, this popup does not seem to be declared in the XAML, but solely within the complex WPF's XAML.CS
I am trying to use this with Window's handwriting recognition software for tablets (installed by default in Accessories/Tablet PC/Tablet PC Input Panel on my non-tablet Windows 7 install)
When I click the button, the Textbox appears. Typing anything into the textbox will trigger the textbox's "TextChanged" event. However, if I were to use the Input Panel's handwriting recognition, and press "insert", the TextChanged event of the textbox does not get triggered. (The TextInput event on the ComplexControl, however, does)
When I use the Input Panel's "insert" on regular WPF Textboxes, it does trigger the TextChanged event.
I assume that some complication of the nested controls is making the Input Panel not work as expected in the former case but not in the latter case, but due to my relative inexperience with WPF (heck, the WPF control itself is legacy code!) I'm not entirely certain why the one would trigger and not the other.
Any ideas? I'm not sure what code I could provide that would be most helpful.

Behaviour kidnapping mouse and keys

I am having a problem with DateTimePicker when I show the UpDown arrows. It actually happens also with the numericUpDown control. It does not happen with the calendar.
If I click with the mouse in any field of the DateTimePicker and then click the arrows to change the value, I cannot then leave the control.
Sorry, I don't really know how to explain this properly (probably the reason why I haven't found anything about this when searching). The software does not hang but nothing works apart from the DateTimePicker; for instance, I can click with the mouse in a button and the visual effects will run but not the click event...
So, the only I can do is to force the program to close. It is like if it was just stealing the focus or the mouse click event and the keys events and not letting it go anywhere else. The tab key doesn't work either.
The dateTimepicker is bonded to a column in a datatable and there is not code into them at all.
I am sure this is something stupid that I am missing, but I am getting already frustrated with it.
Any advise please?
I have found that this only happens when the property Text of the controls with UPDown (DateTimePicker and NumericUpDown) is binded. It does not do it when the property Value is binded instead.
Although this solves the problem in my applycation, the behaviour is there and cannot find an explanation for it.

How to see all TriggeredEvents for control

Anyway to view all the events triggered specifically for a ui control.
I have integrated WPF toolkit extended into my application and currently facing an issue where the TimePicker does not trigger the ValueChanged unless it loses focus. that would be understandable but it has buttons in the control that changes the value ... so losing focus is not ideal as I would require to click on another control.
This is the first time I get this type of issue but there is a lot of times where I am not too positive which event I want to use so I just put a bunch of events with breakpoints and see which one gets hit with the ideal moment.
For that reason, I am curious to know if there is any sort of debugger tool or something similar that can register/show all the events being hit without me putting a breakpoint in every event or modifying the controls code ?
If ValueChanged is a RoutedEvent you could use a tool like snoop. http://snoopwpf.codeplex.com/
the events tab will show you events as they are triggered (note that you'll have to check off the event that you want to listen to in the dropdown.

what is happening when by default when I databind to windows form control.

So I was asked to fix an issue with an old windows form utility that has been around a little while (least before any of my coworkers showed up). The form has a numericUpDown control that is databound. The issue was, when you clicked the up or down arrow the values would change and save OK however, if you just typed in a number and clicked save it wouldn't save. It was like the databinding never saw the change, so coming from a WPF background I guessed that changing the following
TaskDaysToComplete.DataBindings.Add("Value", taskTemplate, "DaysToComplete");
To this
TaskDaysToComplete.DataBindings.Add("Value", taskTemplate, "DaysToComplete", false, DataSourceUpdateMode.OnPropertyChanged);
would solve my problem and it did. You can now either type in a number or use the up/down arrows on the control to set the "Value" property.
My question is this, what was happening in the first place? I am guessing the default DataSourceUpdateMode was OnValidation but when does this happen and why was it OK for when using the up/down arrows but never seemed to happen when typing things in.
Thanks!
numericUD validation
validation is done on losing focus, so when you press the up/down key the textbox loses focus - triggering the validation routine.
when editing text you can make the control lose focus by clicking another control, this will cause it to validate.
the reason that the default is set to onValidate is that on value changed will cause it to validate on each character typed, which can be problematic both for performance and for correct validation.

in WinForms can control focus change "automatically"? if yes, can I somehow distinguish this from user triggered focus change?

I am not fully sure about this, but I seem to be observing cases where focus shifts automatically from one control to another, even after I explicitly programmatically set the focus to control that I want to have focused. Maybe it has to do with the control in question being a panel, and it seems that WinForms is happier to have a textbox focused than a panel.
Well, first of all, can somebody provide expert insight on this matter? And also, if it is indeed possible for the focus to change without explicit order from me (whether user action or programmatic) is it possible to programmatically distinguish the resulting Leave and Enter events? That is, I would like to programmatically counteract Leave/Enter events not caused by myself, but I still want to allow the user to change focus normally as part of work with the GUI.
Yes, that can happen. It is probably a ContainerControl that's messing up your focus, Form is derived from it. A ContainerControl goes hunting for a control to focus when it gets an activation event. It likes nested child controls, it will definitely skip your Panel if it has any controls.
The logic involved in WF to handle focus is very complicated, most of all due to validation. You'd be best off by staying out of trouble and avoid ever giving a Panel the focus. It isn't designed to be a focusable control, it has no way to indicate focus to the user. This is enforced by it having the ControlStyles.Selectable style turned off and the TabStop property set to false so the user can never focus it by tabbing or clicking.
The Enter event won't help, the Panel gets Enter both when it gets the focus or when one of its child controls get the focus. Either by the user tabbing or when you use the Focus() method. You'd have to wait until all focus events are done firing, something you can do with Control.BeginInvoke() or a Timer.
Well, I'm sure that doesn't help much but your problem description is fuzzy. Best way to proceed is to post a sample project that exhibits this behavior to a file sharing service or, as indicated, avoid ever trying to give a parent control the focus.
One thing to keep in mind is many winforms controls cannot be focused at all - winforms provides a CanFocus property to indicate this. Panels have CanFocus set to false, so there is no way to focus directly on a panel without using a derived class that sets that property to true.

Categories

Resources