This seems very simple, but I can find nothing on a web concerning the behaviour I want to add to my custom control.
My custom control is a textBox with a list of choices. When the text entered by the user is not part of the list, a popup will appear with the list allowing the user to select a correct choice.
Sometimes, there may be a default button on the container in wich the custom control has been added. If so, when the enter key has been pressed, if the text is wrong, The popup must been displayed. If there is no default button, on enter, nothing must happen even if the text is wrong.
To be able to create this behaviour, I must be able to detect the presence of a defaultbutton in the container, and it must be done inside the c# code of the cutom control.
I hope the description is clear enough.
Thanks in advance
Have you thought about implementing an MVVM approach and the Command pattern? So long as your view model knows what the choices are, you can bind the default button to a command. So long as the commands CanExecute handler returns false, i.e., an appropriate choice has not been entered/selected, the button will be disabled and won't respond to the user pressing enter.
Since I was unable to know what other controls I had from the custom control I chose to go like this:
I made a recursive function to find the first parent using FrameworkElement.Parent
Having the parent, I could take a look at every controls it contains.
As soon as I saw a button, I had to verify if IsDefault.
For this one, I used the VisualTreeHelper GetChildrenCount(DependencyObject obj_Parent) and GetChild(DependencyObject obj_Parent, int childIndex). Recursivity once again...
It works very well even though it means more code to execute.
Related
I have a Windows 10 mobile uwp app and I am having two issues.
First, I set focus to controls in the app. I do this by using the common call successfully
Control.Focus(FocusState.Programmatic);
However, there are some cases where this does not work. Most times it does but for example, when my page loads, I am trying to set an initial focus in one of the fields and it does not work. I have tried this call in two places. First, in the constructor for the page, after InitializeComponenets and also in the override onNavigatedTo method. Where is the best place to call this and what are some reasons why it may not appear to work, particularly when a new page is instantiated?
Second, related to setting focus. I have a text box on my UI that I set control to with the same Programmatic focus call I listed above. However, the soft (on screen)keyboard shows when this happens. I dont want it to show up when I set focus Programmatically but then have it show if the user selects the field. The scenario is I have a barcode scanner. When the page loads, I set focus in code to the text box and it is therefore ready for me to set the text in the text box from code, based on the barcode scanner result. There is hardly ever a need for the user to type into this field. Therefore, I dont need or want to have the keyboard showing and taking up real estate. There is a rare case when I do allow them to still type the text in manually, for example, in the case the barcode does not read. They would then select the control (even though it may already have focus programmatically) which should set focus again but instead as cursor, touch or something and then I want to show the soft keyboard.
What is the best way to do this?
Thanks!
as far as focusing anything else than the TextBox did not work for me anyway, I found a good solution:
I called:
using Windows.UI.ViewManagement;
InputPane.GetForCurrentView().TryHide();
and the Keyboard gets hidden.
I think the best place to call Focus() is in Loaded event handler of the same control which you trying to focus. When this control is fully loaded, it means it's ready for interaction, including focusing.
As for preventing on-screen keyboard to appear, the TextBox class has PreventKeyboardDisplayOnProgrammaticFocus property. Try to set it to true, this should solve your issue.
In a MasterDetail like page on the detail part it is possible to edit within some TextBoxes. When I select a new item in the master part I have to check validity of all input. Also, when pressing the hardware back button.
My problem is, when I check with CanGoBack I get the answer true, then I call GoBack. Before calling GoBack I'm able to let the page check it's validity and returning false in case it is invalid. Then I do not call GoBack. So far so good. Unfortunately if a TextBox has still the focus from the last input, the LostFocus is only called after GoBack and so the Bindings from that control aren't written back to the model when I check the validity, since it is checked before GoBack.
Is it possible to forbid the GoBack going back somehow?
Determining the control with the focus and call it's BindingManger to update the bindings does not work, since the focus is already on the new selected item of the listview in the master part. And also since I use compiled binding, I have no BindingExpressions I could invoke manually.
Another try was to call Bindings.Update() before the validity check, which is available extra for compiled bindings. But this method works silly. It first writes all the values into the view and then after back to the viewmodel. Thus it overrides my input.
Does someone have an idea how to handle input validation on pages correctly and forbidding GoBack when they are invalid?
I'd like to control which button is focused in my view. User must be able to perform a job without need to use mouse. And job is going through all elements. Depending on element, some buttons will appears, some disappears.
I could do it with dependency properties. To example, if there are buttons Previous and Next, then I can provide IsPreviousFocused and IsNextFocused to set focus to them after certain event. Events can be: showing window for the first time (something should have focus already), reaching 1 element (hiding Previous button, setting IsNextFocused), reaching last element (opposite), etc.
This looks reasonable more or less.
But, if I have, to example, 10 buttons (different operations), then all of them will have to have dependency property!
So I was thinking about much easier approach: when I hide button, there will be no focus
if(FocusManager.FocusedElement == null) { ... }
If I can detect, when there are no focus, then I can try to set it to one of the buttons somehow. This way, I don't really need any complicated focus management
Question is: how to deal with FocusManager.FocusedElement in MVVM scenario? How to detect when there is no focus (when window is shown first time, when certain button is clicked and become invisible, etc)?
P.S.: I actually hate ms for making another technology without thinking fully into it; focus is very basic feature (and everybody care about it in their software), but there is no direct support for it (in xaml); looks like "oh, its too complicated, lets skip it" solution.
You could control your focus from your ViewModel by using the approach shown here:
Set focus on textbox in WPF from view model (C#)
When I switch tabs with the following code
tabControl1.SelectTab("MyNextTab");
It calls the tabPage_Enter for the tab it is switching from and the tab it is switching to. I want it to be called for the tab it is switching to, but not the tab it is switching from. How would I turn this off. I do know when it happens so if there was a call I could make that would turn off calling the enter method for that tab I could implement that.
Yes, I repro if I use a button to change the selected tab. TabControl forces the focus onto itself before it changes SelectedIndex. This appears to have been done to avoid problems with the Validating event. The focus change produces the first Enter event, for the active tab, the tab change then produces the second Enter event.
Knowing this, you could set a helper boolean member, indicating that the first Enter event should be ignored. Be careful to check that the current tab isn't already the one you want to select. In a perfect world, this behavior shouldn't matter. The focus really did move to the active tab first.
Can you check the index of the active tab and workaround using that?
Wrap the code in the event within a check so it only processes when you want it to.
If you can write code to switch it off then you could write code to set a state that prevented the code in the event from running.
I have a UserControl that consists of three TextBoxes. On a form I can have one or more or my UserControl. I want to implement my own tab behavior so if the user presses Tab in the second TextBox I should only move to the third TextBox if the the second TextBox has anything entered. If nothing is entered in the second TextBox the next control of the form should get focus as per the normal tab behavior. If the user hasn't entered anything in the first or second TextBox and the presses tab there is this special case where a control on the form should be skipped.
By using the ProcessDialogKey I have managed to get it work kind of ok but I still have one problem. My question is if there is a way to detect how a WinForms control got focus since I would also like to know if the my UserControl got focus from a Tab or Shift-Tab and then do my weird stuff but if the user clicks the control I don't want to do anything special.
As a general rule, I would say overriding the standard behavior of the TAB key would be a bad idea. Maybe you can do something like disabling the 3rd text box until a valid entry is made in the 2nd text box.
Now, having said this, I've also broken this rule at the request of the customer. We made the enter key function like the tab key, where the enter key would save the value in a text field, and advance the cursor to the next field.
I don't think there's a built-in way that you could do it. All of the WinForms focus events (GotFocus,LostFocus,Enter,Leave) are called with empty EventArgs parameters, which will not give you any additional information.
Personally, I would disable the third textbox, as Rob Thomas said. If you're determined to do this, though, it wouldn't be difficult to set up a manual (read: hackish) solution. Once the tab key is pressed (if the focus is on the second textbox), set a variable inside your form. If the next object focused is then the third textbox, then you know exactly how it happened.
The reason for this odd tab behavior is all about speed in the input process. It was really good to get some input, I hadn't thought about disabling a textbox but that could actually work. But using the Enter key to accept the input hadn't even crossed my mind. That will work so much better. The user can enter the numbers and then press enter to accept the input and the next possible textbox will be the active one. It's like having the cake and eating it too, The speed factor is there since when using the enter key no unnecessary tabing must be done to get to the correct field and using the enter key next to the numeric keyboard makes it really smooth.
Thanks for the input!
I agree with DannySmurf. Messing with the tab order might give you hell later on if the requirements for the application change.
Another thing that you could do is to implement some kind of wizard for the user to go through.
Better than disabling controls, try monkeying around with TabStop - if this is false, the control will be simply skipped when tabbing.
I'd also suggest that the Changed event of the TextBox is the place to be updating TabStop on the other controls.
I've done something similar to this with a login control, where users could enter either a username or an email address (in separate fields), plus their password, and tabStop is what I used to get the job done.