I have a RichTextBox with a SpellCheck implemented. I want to place the cursor/caret exactly where the rightclick is positioned. For example, if i have two misspelled words such as :
I belive this is wroking
and I right click on "belive", the context menu that opens is based on Wroking, because my cursor was last positioned there. To open the list of suggested words of "belive", I have to first Left click on the word, to position the caret and then RightClick.
So to make it clear, I want to automatically position the caret where my cursor is on RightClick. Is it possible to do that? Thanks in advance.
I Found a way to solve the problem, by getting the mouseposition on MouseRightButtonUp event and attributing it to a TextPointer. From there, I can select the exact Range of the current word, and then do something with it.
Point mousePoint = Mouse.GetPosition(textBox);
TextPointer current = textBox.GetPositionFromPoint(mousePoint, true);
Related
I am implementing a custom drag and drop interface with winForm Buttons and after viewing several solutions on how to obtain mouse position and check it against control bound have not been able to get it to work.
I have tried:
button.ClientRectangle.Contains(PointToClient(Cursor.Position))
and
button.ClientRectangle.Contains(PointToClient(Control.MousePosition))
Both of these have failed to work. Checking the mouse bounds seem like a simple operation, but I really am stumped.
My speculation of the unexpected values are:
Process of obtaining cursor position may be in wrong corner of cursor image
Method/Function does not work on Buttons for some reason
You are using the wrong object reference, calculating the mouse position relative to the form instead of the button. And you are writing it in a way that make this very hard to debug. Fix:
var pos = button.PointToClient(Cursor.Position);
System.Diagnostics.Debug.WriteLine(pos); // Now it is easy
if (button.ClientRectangle.Contains(pos)) {
// etc...
}
On the CodedUI WpfEdit class there is a way to get the selected text, but I cannot find a way to get the cursor position when nothing is selected (i.e. the index of the caret in the text). Is there anything available for that in the CodedUI framework?
My goal is to assert the position of the cursor in the text contained by the control.
There isn't a codedui method for that but try the following:
add the reference:
using System.Windows.Forms
in the code where you need to get the moue coordinated type:
Point p = new Point(Cursor.Position.X, Cursor.Position.Y);
remember that this is not a relative position to the control but the position of the mouse on the screen.
calculating the position of the point relative to control shouldn't be much problem.
I'm not sure there is a way, and I would imagine that there is a different requirement than actually finding the cursor position.
If you are trying to insert some text, you can always copy the text out to the test method, insert the text, and write it back.
Or, if you need to not do that, you could always use the Keyboard.SendKeys method to send a home command and then any number of right arrow commands you need to place the cursor where you'd like it.
Can you elaborate further as to what exactly you need with the cursor position?
I am making a form which has a ListView. I want to show a "Picture Window" whenever mouse hovers above one of the listView Item in the ListView. I implemented this by using "ListView"'s "ItemMouseHover" event. I want to show this "Picture Window" just near to the mouse position. But , I couldn't get the mouse position from the itemmousehover event. Can anybody say how to achieve this?
Note: I am using .Net 4.0
EDIT:
"Danbystrom" gave the exact answer. But I found the root cause is some other. See my answer for that.
The static method Control.MousePosition will give you the coordinates in screen space. Then you can convert that into client coordinates with the Control.PointToClient instance method.
var pt = listView.PointToClient( Control.MousePosition );
The new window will be placed on your will, only if you choose "Manual" instead of "WindowsDefaultLocation" in the Property "StartPosition". Otherwise "OS" will try to put your new window's location somewhere near to the mouse position. If the new window hides the mouse in that position, then it will try through some nearby points which will result in flickering.
I've implemented "ChangeCase" keyboard shortcut (like Shift+F3 in MS WORD) for RichTextBox, which changes the text either selected by mouse, or the last word before caret's position. The problem is, it SOMETIMES loses the selection, or moves the caret one word left.
Once it changes the textcase without this changing of caret position, then it never changes the caret position (propably some WPF's internal caching.), so it can only happen the first time i run this function to a portion of text.
The code used is the solution mentioned in here WPF Flowdocument "change case" feature .
One problematic section of code is certainly
end = this.CaretPosition;
EditingCommands.MoveLeftByWord.Execute(null, this);
start = this.CaretPosition;
this.CaretPosition = end;
However I have no idea why it only occurs sometimes and how to fix this.
I thing it has something to do with the execution speed of this Execute() method and some side effects, because at my WPF app it only happens sometimes, but when hosting this WPF control in Winforms, moving the caret one word left happens all the time (if I hold Shift+F3, the cursor moves word by word to the very beginning of the document)
Other problem can be related with changing text of a TextRange, which is resulting in losing the selection? But again, it doesnt happen all the time and I have no clue how to fix it.
Any ideas?
I ended up with 2 options, ignoring this error or implementing the
MoveLeftByWord
logic manully without touching the
CaretPosition
-edit- I dont need the answer anymore now that i calculate based on mousedown position.
Basically in relation to this question It seems like e.X,y and e.Location.X,Y is relative to mousedown? I seen negative values when i havent moved it left/above the parent picbox.
So it seems like the values are relative to my mousedown, but i need values relative to the parent because i update the left/top every event and other datat and relative to mousedown is breaking my code.
Did you try to do this ( assuming your control is named picbox )
Point pt = picbox.PointToClient(point)