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.
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.
I'm using a form (FormView) with databinding (ObjectDataSource) and all my input fields are bound by using '<%# Bind("field") %>'.
Everything works fine, but I have two problems (which I found various hints about like using this.Validate() or .EndEdit() - but none seem to work):
Entries are only saved after leaving the input field so it looses focus
Let's say I have a textbox with an ID of Name and enter "George". When I would tab to the next textbox or when I click somewhere else and click save - everything is saved. But when I keep the focus in the textbox the value is not saved. Why is this happening? What magic can I use to circumvent this (JavaScript to the rescue?).
I set a textbox's field value (element.value) via Javascript (upon selecting something in a combobox).
The same problem as above applies, only when I give the textbox focus and tab out the value is saved. This creates the problem that I only want the user to choose something in the combobox (the textbox is updated accordingly) and move on - I don't want the user to click into the textbox afterwards and tab out again.
Edit:
The second problem I resolved now by setting the focus onto my textbox via Javascript (textbox.focus();) and right after set the focus back to the combobox (combobox.focus();) and that does the trick - this seems fairly hackish to me, doesn't it?
I'm assuming this is fairly common, but my mighty Google fu hasn't help me find a simple solution.
A similar issue can crop up in Winforms development when working with DataGridView controls. I typically attach some logic to the submit button's Click event to cause the DataGridView to validate. I suspect a similar solution would work for you here.
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.
I have a list box with Checkboxes in it. I want to prevent the Checkbox from changing its status if the user clicks on the text next to it. I only want it to change if the small box is clicked directly.
Is there any way to do this in windows forms?
Greetings and thanks in advance.
Place the text next to it in a Label, instead of the Text property of the Checkbox. Or you could create your own control which has a Checkbox and a Label. The Text property of the control would then fill the Text in the Label, and you could expose all of the Checkboxes regular properties in your control.
That's fairly non-standard behavior. Users are going to expect to be able to change the checkbox when clicking on its label, and are going to be frustrated, confused, and surprised when it doesn't work. I'd recommend not doing this. I'm not the only one.
(Yes, it's about web design, but many of the concepts are applicable in desktop application design as well.)
You could always not fill in the Text property of the Checkbox and make a completely separate Label control.
Otherwise, you will probably have to do explicit hit testing within the control to see if they hit the box or text. And then you will have to worry about checking the margins, which side the box is on, and other things that can change the position of the box.
I personally was only able to freeze things.
I freeze the check boxes by handling the Click and ItemChecked events,
and change the check state back, when it gets modified.
I use a menu to check/uncheck items and let user decide to use the menu or classic behave.
Cheers, good luck.
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.