I have a WPF textbox, and perform the following actions
Enter text as "12345"
Move cursor between 3 and 4 (using arrow or
mouseclick)
Enter 0 (so Text is now "123045")
Which event/eventargs can tell me that 0 was typed at location 4.
I need to know this at Preview level so that I can reject the character 0 based on the prefixed and suffixed digits.
In PreviewTextInput event you can use the TextBox's CaretIndex property to know the location where input is being typed.
You can use the PreviewTextInput event. (see here for a complete example)
Related
Facts :
phone:PhoneApplicationPage.ApplicationBar don't change the focused element.
TextBox/PasswordBox is changeing its binded content when losing focus
So if a user is writing something in a TextBox and he click a shell:ApplicationBarIconButton then the TextBox.Text={Binding ...} is not updated.
I found this solution:
http://www.pedrolamas.com/2013/01/11/how-to-force-a-focused-textbox-binding-to-update-when-i-tap-an-app-bar-item/
But c'mon I have 20 screens with inputs, it feels like reinventing the wheel again and again!
I search for a nice/cleaner solution if there is such.
Now I have to go across all click events where inputs are involved and put the magic line:
App.FocusedTextBoxUpdateSource();
Can it be set globally somehow?
Or you could create a custom textbox control with its text changed property bound to a command something that builds over this thread
"UpdateSourceTrigger=PropertyChanged" equivalent for a Windows Phone 7 TextBox
I have a numericUpDown picker that's set to a range of acceptable years, defaulting to the current year.
The user can edit the number in the box with the keyboard: say if the number in there is 2011 to begin with, they can press backspace and the number in the box on screen will be 201. However, as far as I know, the value property of the control will never read a number below the minimum I set (1900 in this case).
I need a way to get the number currently in the box on the screen regardless of whether it is in my acceptable range or not. Anyone know how to do this?
Thanks!
You can use the NumericUpDown.Text property to get the current text, then parse to int. It may not look like the property exists, but it's there.
The Text has no affect on the appearance of the NumericUpDown control; therefore, it is hidden in the designer and from IntelliSense.
I have a little problem - in my Windows Forms program I have a lot of text boxes. They only can get numeric values between 1 - 1024. "Protecting" the text box form non numeric inputs is no problem. But how can I assure that the value doesn't get higher than 1024? Is there any function or any event I could try to catch and then Handle it on my own? I thought about catching the "TextChanged" Event and then check for the value. But how can I know then which Button was the last one pressed?
Besides I wouldn't like to exchange my Textboxes with any other Controls since they're all implemented right now and it would be a lot of work to exchange them all.
Best Regards
Quendras
You should use the NumericUpDown control and set the Maximum property.
You could try using OnLostFocus on each text box. Then verify the input was numeric, and it's value is greater/equal to 0, and less/equal to 1024.
You could check when that textbox loses focus, and then check its value:
public sub Textbox1_lostFocus() handles textbox1.onLostFocus
If cint(textbox1.text) > 1024 then
'whatever you need to do here
End if
end sub
I have the following scenario which I thought would have a simple solution, but I'm stumped...
I have constructed a WPF user control with a set of numeric Buttons and a TextBlock to record what buttons have been selected. When the user control opens, the TextBlock is empty. As a button is selected, the digit it represents is appended to the right-hand-side of the displayed text.
What I need is a solution where the digits keep getting appended on the right-hand-side and if that results in exceeding the fixed displayable size of the TextBlock dropping the left-most character. In effect I am after a one-character-at-a-time marque effect.
I can not use scroll bars. Any thoughts would be welcome
Databindings are you friend here.
suppose you got a ViewModel. and in the ViewModel you got a property of string which is binded to the TextBlock Text propetry.
Now you only need to manipulate that string inside the ViewModel using regular c# string methods to display correctly.
HTH
Ariel
There's nothing special at the TextBlock level... Just pass it the string that is formatted correctly using something like string.SubString.
UPDATE: This code should do the trick:
const int maxLength = 8;
string value = "sdfdfdfsdffdsfsdf";
if (value.Length > maxLength)
value = value.Substring(value.Length - maxLength);
When editing text the user is able to use the keybord shift button and direction keys to modify the selection - one position stays anchored while the other moves. By pressing left it is possible to get the movable part on the left of the anchored part.
I'm trying to dinamicaly modify the users selection in WPF's TextBox (for the purpose of the discussion, lets say I want to select the characters in pairs, eg. when pressing shift+left, 2 characters would get selected, not just one). However, when using SelectionStart/SelectionIndex/CaretIndex/Select the "movebale end" of the selection is always ends up on the far right end of the selection, rendering the shift+left combination useless.
Any way to preserve the "selection direction"?
This is not quite the answer you are looking for but it'll work. Invoke the command and do it the way WPF does it.
EditingCommands.SelectLeftByCharacter.Execute(null, textBox1);
None of those work. SelectionLength doesn't allowed to be negative, and CaretIndex is always identical to SelectionStart. This is a design bug in TB, as you cannot achieve a valid state of it even in the protected scope.
The command actually works, but you still cannot determine the selection's direction of the currently analyzed textbox. There's only one solution, which is an ugly workaround: You can write your own observer code by overriding the mouse and keyboard event handlers.
The only possibility I can think of is to set the SelectionStart and then make the SelectionLength negative.
Have you tried setting the CaretIndex property before or after setting SelectionStart/SelectionLength?