WPF Avalon Edit Make text upper case - wpf

I am using C# WPF with Avalon Edit Text Box.
I am trying to make all of the text in the text box uppercase and I get an error with additional message 'No undo group should be open at this point'.
I am using the following code:
a.Text = a.Text.ToUpper();
where "a" is the AvalonEdit.TextEditor
Thank you.

Setting the TextEditor.Text property has the side-effect of clearing the undo stack (just as with the normal WPF TextBox). Clearing the undo stack is only allowed when there's no open undo group.
If you did not intend to clear the undo stack, use the methods on textEditor.Document instead to modify the document. You'll want to avoid replacing the whole text, because that would also reset the selection and caret position (after all, AvalonEdit can't know how your new text is related to the old text).
If you do want to clear the undo stack (e.g. you're switching the view to a different document), you'll have to figure out why an undo group is open. Most likely, your code is running from the event handler of an event that is called while the undo group is still open (e.g. document.TextChanged) -- you might want to switch to a different event instead (e.g. document.UpdateFinished is called after the undo group was closed).
If all you want to do is to upper-case text as it is being input, it's better to modify the text before it is added to the document: handle the TextArea.TextEntering event to cancel any lower-case input (set e.Handled = true;), and instead call TextArea.PerformTextInput() to repeat the text input process with the corresponding upper-case text instead.
For copy-paste, you could handle the attached DataObject.PastingEvent and modify the data to be pasted.

Related

how to intercept winform textbox selection right click copy

I need to transform the copied selection in a winform textbox before copying to clipboard. I have the ctrl-c done, but the right click copy would seem to require overriding WinProc,and the link in an example in how to disable copy, Paste and delete features on a textbox using C# for WM_PASTE is no longer is valid. I would add failure may be a feature, because Ctrl-c could be transformed and right click copy could be the literal, both of which I need to do. I had hoped to radio button a state for a transformed or literal selection for choosing behavior.
My question was based on reading that the only way to do this was to handle a WM_COPY, but the better solution would be to implement a context menu strip for each text box as needed.
The right click context would then be handled by event handlers.

Atomic Text Blot in Quill

Let's say I want to have an AtomicText blot that is similar to the default Link blot but is immutable and can only be removed as a whole. More specifically:
The cursor can be between the characters of AtomicText.
It is possible to select parts of AtomicText.
Deleting at least one character of AtomicText leads to the deletion of the whole AtomicText.
Adding characters to AtomicText is not possible once it has been created. Neither via keyboard events nor via copy and paste.
My idea was to make AtomicText extend from the Embed blot. In that case, the whole AtomicText blot is deleted when the cursor is right to its last character and backspace is pressed. But other operations do not work as expected. I assume I need to override some of the Blot methods to achieve the correct behavior but I am a bit at loss here.
Another idea is to listen to text-change events, determine if the cursor is inside an AtomicText blot and act accordingly. E.g., when pressing backspace, find the start and end position of the current AtomicText blot and remove all characters between these indexes. This seems to be a fragile approach.
Any pointers would be appreciated.
Similar questions/requests are the following:
How to make non selectable embed custom format in quilljs
How to add a non editable tag to content in Quill editor
Quill Editor: Restricted Editing based on tags/classes
I my experience setting contenteditable false is problematic. If you position the AtomicText blot at the end of the document you will not be able to append to it. Additionally, moving the cursor over the AtomicText positioned at the end will blur the editor.
I experienced a similar issue with a footnote blot. It's a sup tag that contains [\d+] and we don't want editors to modify the footnote but we do want them to be able to seamlessly move the cursor through them.
I have experimented with two other options in addition to the text-change listener mentioned by the OP. The first option is to attempt to position the cursor (via setSelection) in front or behind using selection-change event listeners. I found this to be problematic because it's possible to sneak characters in before the selection change event fires. I'm also not a fan of this approach because it causes the cursor to jump across the footnote. YMMV
Another option is to intercept keyboard input. You can add a keyboard listener (I used KeyboardJS) in a module that's initialized with quill. If the current selection is "inside" of your atomic text you can stop the input from proceeding to quill using e.preventDefault(). With this approach you'll also have to provide custom keyboard handlers in your quill configuration to override tab, enter, and possibly delete. Tabs are more of a challenge because there's a default tab handler provided by quill that takes precedence unless you override it. Within the custom handler you'll have to detect if the context is within your AtomicText. The context object will contain contain a format map that contains atomictext (your blot name) if it's in the context of AtomicText.
Note that if the user positions the cursor adjacent to your AtomicText, then from quill's perspective that's in the context of AtomicText. Our solution to that problem for input is to insert a zero width non breaking space, then allow the keyboard input to proceed. That "breaks" the cursor out of the AtomicText blot allowing insertion to proceed as expected. We then strip those characters out of the resulting HTML before saving the text.
Hope this helps.
In the blot create(value) function, add this:
node.setAttribute('contenteditable', false);

how can I get selected text

I wonder how can I get selected text. (usually done by mouse dragging or shift + arrow on text)
From notepad, word, Internet explorer addressbar, etc.
sending WM_GETTEXT just copy caption, and unable to copy selected text while I rename file name on file explorer.
So, I am considering simulating Ctrl+C. but simulating key strokes seems not a good idea. because it will make side effects.(in case Ctrl+C assigned to other functionality)
I tried following code, wishing copy currently selected text into clipboard
OpenClipboard(0);
EmptyClipboard();
SetClipboardData(CF_TEXT,0);
CloseClipboard();
but no luck, it just emptyclipboard.
how can I copy currently selected text?
(simulating Ctrl+c with no unpredictable effect)
thanks!
(my environments are Windows 7, C programming language, winapi)
I'm not sure if there is a general answer because the various applications you mentioned use different window classes.
For Notepad in particular: The display area seems to be a simple EDIT control. You can use the EM_GETSEL message to retrieve the begin and end of selected text, then use WM_GETTEXT to get the complete text. Do not use GetWindowText because it does not work with windows of another process.
In general, you can try using the WM_COPY message. This should place the text in the clipboard. However, the result depends on how that message handler is implemented in the other application.
You are setting the clipboard using SetClipboardData(CF_TEXT, 0) -- MSDN Doc says that if the second parameter is NULL, the window must process the WM_RENDERFORMAT and WM_RENDERALLFORMATS messages; the same article has a comment on how to allocate Global memory, fill it with the required text and pass it on to SetClipboardData().

How do you detect incoming text ctrl-v pasting in a winforms textbox?

I'm making my own commentbox control that inherits from a winforms textbox. One of the things it does is prevent users from entering any characters if the limit has been hit. So on the keypress event I just grab the incoming key (excepting delete and backspace) and add it on to what's in the textbox already, then check and see what the length is. If it is over, then I just set the e.Handled = true and the keypress is halted. This scheme fails me though on an incoming ctrl-v paste event. It registers as a single keychar coming in. So the paste won't come through if I'm right at the limit, but it will go over if there is one character of room left and the paste has two or more characters for instance.
I just need a way to detect that this paste is coming in, and when its coming in I need to know what the length of the string is so I can either stop it from happening or paste as much of it as possible in there.
Thanks!
This is such an idle question. You can just set the MaxLength property of the textbox and it prevents all of this. Duh.

Is there a way of undoing a selection a user makes with the combo box?

In WPF 3.5, is there a property of the combo box will allow the user to undo the selection they've made?
Code
If you look to a way to reset the selection from code (you wrote a property), try the following:
cboYourCombo.SelectedIndex=-1
or
cboYourCombo.SelectedItem=null;
Keyboard Shortcut
If you look for a keyboard shortcut to reset, I've never seen. But if you want, you can do it on your own, it's probably easy:
Attach an EventHandler to the PreviewKeyDown-event of your combobox (or register a general event-handler that works for all comboboxes in your window/app), check the key and if its the key you want to reset, use the code above to reset the selection. Please note, in the PreviewKeyDown-event you can also check for special-keys such as the control-key.
Provide an empty Value
However I think, better would be to add an empty entry and then preselect this empty value. If the user has changed the selection and wants to reset, he can select the empty value. Otherwise you change the standard UI-behaviour and not all people like this.
What do you mean by "undo"? Do you mean something like CTRL+Z (or an undo button), or something like CANCEL? Implementing true undo/ctrl+z on a combo box is something very few applications do, and it will surprise the user. This is a very bad idea, unless you have a very good reason.
If you have a very good reason to go against the design of most windows apps, you can add a handler for SelectionChanged, and implement your own history. Then, if the user either uses a keydown (ctrl+z), or clicks an "undo" button, you can set the selection yourself.
Alternately, if you don't really want an UNDO feature, and actually want a CANCEL feature (a common feature in UI apps), then you shouldn't worry about each control individually. Just keep a set of stored settings (in some custom class), and set all the controls back to the values that were stored. In the case of a combo box, you'd want to set the Selection property.

Resources