Understand textbox.Select() - c#

I am trying to understand a piece of code in order to refactor it. There are several verifications for the input value to check if they are valid, and on each verification there is a line of code that I do not understand what it does.Here is the code:
if (IsNotDouble(weight))
{
MessageBox.Show("Weight must be a numeric value!");
txtWeight.Select();
return;
}
txtWeight is a textbox.
Can anyone tell me what txtWeight.Select() does here.I can not understand why this piece of code should be posted here after each time an error is thrown.

It sets the cursor into the textbox where you have to enter the weight.

The TextBox.Select() method from MSDN.
Activates the control. The Select method activates the control if the control's Selectable style bit is set to true in ControlStyles, it is contained in another control, and all its parent controls are both visible and enabled.
In your case, it seems whenever a validation check has failed, the particular text box is selected to activate it in order to set the visual focus to it.

According to the MSDN:
The Select method activates the control if the control's Selectable style bit is set to true in ControlStyles.
It means, that Select sets focus to the Control so in your scenario if IsNotDouble(weight) is true, you set focus to txtWeight so that user can write there a text immediately without seeking the txtWeight through the entire form.

Select method activates the textbox control or you can think this as bringing the focus to the textbox.
It may not be required in your case as when the verification is happpening most probably the focus is already on that textbox

Related

How to disable focus on textbox on load in a view in WinRT?

I have a TextBox in WinRT which got the focus (Pointer focus) on load in a view in WinRT. I want to disable this and change the focus to unfocused at load. When I am in constructor of the view, the TextBox is Unfocused, but when I reach the event Loaded, the TextBox got, I don't know why, automatically, the focus at Pointer.
No instruction are done to put the focus on this control. I don't understand why it got the focus.
I try to change TabIndex, no success, the control got the focus again. When I try to put manually the focus on unfocused, I have an exception: "Value does not fall within the expected range.". I don't understand why I have this exception. I have only one control with the name that I gave to him.
Thank's for reply.
Solution 1:
You can set the textbox TabStop property to false.
Solution 2:
Set another controls TabIndex to a lower value than the TabIndex value of the texbox (the control you wish to recieve focus on startup).

Detecting default button on a control

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.

TextBox - Can I keep the selection highlight when it loses focus?

I would like to have a regular TextBox on my form, where the selected text is still highlighted even if you use another control, e.g. push a button.
Does anyone know a way to achieve this (without using a RichTextBox which is not suitable for what I am doing).
Sounds like you are looking for the HideSelection property:
Gets or sets a value indicating whether the selected text in the text box control remains highlighted when the control loses focus.
The HideSelection property is your friend. Set it to false and you should get what you are looking for. I never quite understood why the default is true.

What is the main difference between ReadOnly and Enabled?

In Windows Forms controls, there are two properties: ReadOnly and Enabled.
What is the difference between these two properties? I feel like they behave the same way.
As it says in the following forum post:
In the context of a TextBox, readonly
allows the user to set focus to and
select and copy the text but not
modify it. A disabled TextBox does not
allow any interaction whatsoever.
Use ReadOnly when you have data that
you want the user to see and copy, but
not modify. Use a disabled textbox,
when the data you are displaying is
not applicable in for the current
state of a dialog or window.
Taken from: MSDN Forums
ReadOnly I generally associate with a TextBox or other control that contains text; it dictates whether or not the user can modify the text displayed by the control. The user can still select the text, though (e.g., to copy and paste it into another program).
Enabled basically controls whether or not any user interaction with the control is possible. For instance a Button with Enabled == false cannot be clicked; a CheckBox with Enabled == false cannot be toggled, etc. Note that a TextBox with Enabled == false also cannot have its text selected (that would be user interaction).
Furthermore, controls with Enabled == false do not raise events related to user interaction such as Click.
Enabled specifies whether user interaction is allowed. If a control is disabled then it will not generate any UI events.
ReadOnly determines whether the user can edit the contents of the control. For example, a ReadOnly TextBox cannot be edited, but you can still click on it, select the text contained within it, etc.
If you take a text box with a scrollbar as an example, ReadOnly does not allow the user to edit text, but the scrollbar is still active (think about licence boxes in installation programs). Enabled = false will cause the entire control to disable, not just the text editing area.
Some controls, e.g., buttons, can only be enabled or disabled. A disabled control will give a visual indication that it cannot be interacted with right now (typically by graying/fading out). Others can also be read-only, in that they can be interacted with in some way but not edited. For example, an up-down control is often like that, which means you can press the buttons to change the value but not edit it by normal typing. (It's also possible to have controls that you can never interact with but which are still usefully disable-able; a label can still be grayed out as part of indicating that a whole area of the GUI is not usable right now, which is a more pleasing visual effect than just changing the controls that are interactive.)
It's virtually always the case that being disabled implies that a control is also read-only. I've seen a few cases where that wasn't true, and it always felt more like a bug than anything else to me. It's best to regard the states as representing a tri-state value where one of the states is not used for some controls.
In addition, I read here that if a textbox is marked Enabled="false", its value isn't preserved in the Viewstate across postbacks.
Don't forget, a screen reader for the visually impaired cannot read the text in a disabled text box, since it cannot receive focus. For ADA compliance, use ReadOnly.

Override tab behavior in WinForms

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.

Categories