I have a UltraNumericEditor of NumericType Integer.The Value is 0(control default).
When the control gains focus, I want the default value of 0 to be highlighted.(Similar to what happens when we tab into a textbox).
I tried SelectAll() . SelectionStart etc: , to no avail. I tried Enter, Focus events..
Or I need to blank out the default value of 0. I couldn't find a way to do this.
I am expecting the user to enter an integer other than zero. Making Nullable true is not an option.I have been specifically asked not to do this.
Any help would be appreciated.Thanks!
Hi Eliza,
The NumericEditor behaves the same way the TextBox control does by default. If you tab into the control, it selected all of the text. If you click on the control, it will place the caret at the point you clicked. Once you have entered the control and established a SelectionStart point, subsequently tabbing into the control will remember the last position of the caret and use that instead of selecting everything. All of this is just like a TextBox.
If you want to the control to always select the text, regardless of whether the user tabs into or clicks on it, then it's a little tricky. Calling SelectAll from AfterEnterEditMode would seem to be the obvious solution, but it doesn't work because the control enters edit mode on the MouseDown and then it positions the caret after that operation is complete. So the only way to to do this is to create some sort of delay and then select all of the text after the control's internal processing is complete.
One easy way to do that is by using a BeginInvoke:
private void ultraNumericEditor1_AfterEnterEditMode(object sender, EventArgs e) { this.BeginInvoke(new MethodInvoker(this.SelectAllTextInNumericEditor)); } private void SelectAllTextInNumericEditor() { this.ultraNumericEditor1.SelectAll(); }
Thank you.This solved it. I always need to select the text.
Can you please explain why I need to use BeginInvoke? Is it the delay that you mentioned?
Yes. Synchronously selecting all of the text works, but then the control continues to do some processing after that and positions the cursor, which blows away the selection.
By using a BeginInvoke, the SelectAll is performed asynchronously, after the controls internal processing and caret positioning are completed.
In theory, you could also start a Timer inside the event and then select all of the text in the time tick. But determining the exactly interval would be tricky, so using BeginInvoke is much easier.
Thank you very much! :)