I want to get Keyboard cursor position in TextBox or RichTextBox. On WinFrom I did this by using this code
Point p = rtb.GetPositionFromCharIndex(rtb.SelectionStart);
p.Y += (int)rtb.Font.GetHeight()*2;
lstboxIntelli.Location = p;
lstboxbIntelli.Show();
ActiveControl = lstboxIntelli;
But in WPF I cant get GetPositionFromCharIndex property is there any other way to achieved this
I want to position a Listbox right under the keyboard cursor (Like Intellisense)
Any help will be appreciate
In WPF TextBox you can get caret position in multiple ways:
By accessing TextBox.SelectionStart and TextBox.SelectionLength properties.
TextBox.CaretIndex property.
Unfortunately there is no GetPositionFromCharIndex in TextBox so you'll have to do a little trick using GetRectFromCharacterIndex to get starting point for intellisense:
var rect = rtb.GetRectFromCharacterIndex(rtb.CaretIndex);
var point = rect.BottomRight;
Note: We are using BottomRight to make put intellisense in proper place.
Related
I have a windows form that I can drag around programmatically created control labels. I am using the location property to determine each label position.
Each label is created as a preview with placeholder '#' where a number is to be. I want to know what the form coordinates are of a specific character in that label.
As a code example:
Label myLabel = new Label
{
Text = "Average: ##.#Unit",
Name = "myLabelName"
};
Controls.Add(myLabel);
...
// After label is dragged around
Point myLabelLocation = myLabel.Location;
This gives me the label's coordinates. Let's say I want similar coordinates, but I want them for the first #.
Another way to look at this, I "split" the label right before '#'. How do I get this location?
I need to set Selection start of a text box based on the mouse position, i tried to load the text box on Double Click, once the text box is loaded, i need to set the selection start based on the Mouse position. (i.e) if a text box contains some values like "abcdef", if the mouse cursor is near "c" when textbox is loaded, then the selection start should be after "c".
I have also tried this
textBox.GetCharIndexFromPosition(e.Location);
but i didn't get it right,
Thanks in advance.
Regards,
Venkatesan R
Putting #Reza's code in the correct event will work just fine:
private void textBox_MouseDoubleClick(object sender, MouseEventArgs e)
{
textBox.Text = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // load the text data here
// now position the caret onto the mouse position
textBox.SelectionStart = textBox.GetCharIndexFromPosition(e.Location);
// and clear a selection
textBox.SelectionLength = 0;
}
Note that you need to use the MouseDoubleClick, not the simple DoubleClick or else you miss the e.Location param!
This is the simplest and most direct way to get the mouse coordinates relative to the TextBox.
If your loading method is complex you can call it by passing in the MouseEventArgs e but simply calling it instead of the textBox.Text = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; is the most natural way.
If you want to you could also use
textBox.SelectionStart = textBoxtextBox1.PointToClient(Control.MousePosition));
This will work in any event or method. PointToClient will calulate the relative position from the screen position Control.MousePosition.
I have written a C# wpf program that deals with curves on a canvas.
So I load a curve (a sequence of points in a polylinesegment) and then I operate various operations on it. Each curve is put on the screen through mouse interaction and that works fine. Each curve then comes with a textblock in its center which gives out some information.
So the problem comes when I want to mouse-move the shape.
I first select the shape with the mouse (that works) and then I stick it to the cursor through the OnMouseMove event. Eventually I put it down on the with the OnMouseLeftButtonDown event.
So in short the OnMouseLeftButtonDown always works fine except when I have to move the shape AND the label. In that case I have to press several times (randomly) to fire the event.
I have then searched the part which cause the problem and that is when I move the label.
private void UpdateLabel(int index, PathInfo piToBeAdded)
{
plotCanvas.Children.Remove(names[index]);
TextBlock text = new TextBlock();
text.TextAlignment = TextAlignment.Left;
text.FontSize = 12;
text.Inlines.Add(new Run("(" + (GetPathsIndexFromId(piToBeAdded.ID) + 1) + ")ID:" + piToBeAdded.ID + " " + piToBeAdded.Name) { FontWeight = FontWeights.Bold });
Canvas.SetLeft(text, piToBeAdded.Center.X);<-----those cause the problem
Canvas.SetTop(text, piToBeAdded.Center.Y);<------those cause the problem
text.ReleaseMouseCapture();
names[index] = text;
plotCanvas.Children.Add(text);
}
NB: pathinfo is just a class storing some information among which also coordinates
Specifically it's just the Canvas.SetLeft and Canvas.SetTop that causes the OnMouseLeftButtonDown not to fire properly. I I take them off the label goes in 0,0 and the event
But what's wrong with those instructions? How can I make the OnLeftButtonDownEvent to work properly?
I hope that I have described the problem properly I've tried to put all related information.
Thanx in advance
Patrick
Typically it is better to use the MouseButtonUp event to release mouse capture as the mouse button has definitely been released at that point and the movement has stopped (which is what you are capturing).
Your issue with Canvas.SetLeft is because it can only be called on a child of the Canvas object and you are only adding text to the Children collection after calling Canvas.SetLeft.
Edit:
In answer to your comment, the Canvas.SetLeft can only be called on an existing child of the Canvas, so call Add before calling Canvas.SetLeft.
private void UpdateLabel(int index, PathInfo piToBeAdded)
{
plotCanvas.Children.Remove(names[index]);
TextBlock text = new TextBlock();
text.TextAlignment = TextAlignment.Left;
text.FontSize = 12;
text.Inlines.Add(new Run("(" + (GetPathsIndexFromId(piToBeAdded.ID) + 1) + ")ID:" + piToBeAdded.ID + " " + piToBeAdded.Name) { FontWeight = FontWeights.Bold });
plotCanvas.Children.Add(text); // <---- moved this up
Canvas.SetLeft(text, piToBeAdded.Center.X);
Canvas.SetTop(text, piToBeAdded.Center.Y);
names[index] = text;
}
On the second part of your comment, I would suggest that you attach handlers to the different draggable items and set flags for the appropriate "mode" of operation that you are working in i.e. bool variables for ShapeDragInProgress and LabelDragInProgress. That way you can then conditionally perform the correct release capture procedure based on the ButtonUp event.
Toadflakz thanx in any case I am not sure if my solution has got to do with what you suggest. In case I will give you the flag.
I noticed that the problem is related with the fact that when moving shape+textblock the mouse point is EXACTLY on the center of the shape (I did that on purpose) and therefore EXACTLY on the textblock. Therefore when I click i don't click on the canvas but on the label. This is why the canvas is not firing. I suppose that the label is firing. So in short I just moved the label some pixels away and that did the trick!!
After setting the SelectionStart and SelectionLength, the Caret is at the end of the selection.
How can I move the Caret to the beginning of the selection?
I now tried this: The question is not answered by the other threads
SelectionStart = selStart; // set the desired position
SelectionLength = 0;
MyLibs.WindowsDll.POINT point;
MyLibs.WindowsDll.GetCaretPos(out point); // save the position
SelectionLength = restOfWord.Length; // extend the selection
MyLibs.WindowsDll.SetCaretPos(point.X, point.Y); // restore caret position
GetCaretPos and SetCaretPos are working somehow, but not as it should.
Using the above code snippet, the caret is indeed blinking at the beginning of the selection.
But ... then I hit backspace or cursor left/right, the caret still behaves like it is still at the end of the selection.
Unfortunately, you can not do this using managed code like C#,
but to achieve this goal you may use C++, SetFocus() function,
for example:
//use SetFocus()
pEdit->SetFocus()
//place caret at the end:
pEdit->SendMessage (WM_KEYDOWN,VK_END,0);
//simulate keyup:
m_Edit->SendMessage (WM_KEYUP,VK_END,0);
//send the key code:
m_Edit->SendMessage(WM_CHAR,'A',0);
Have a look to MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/ms646312(v=vs.85).aspx
You may use also CEdit::SetSel to accomplish that:
CEdit* e = (CEdit*)GetDlgItem(IDC_EDIT1);
e->SetFocus();
e->SetSel(0,-1); // move cursor at the end
e->SetSel(-1); // remove selection
See details in Stackoverflow question: CEdit control MFC, placing cursor to end of string after SetWindowText
UPDATED:
If you need to set position to the beginning you can reset the Caret position to start before displaying the text using:
SetSel (0,0);
You can not move the caret to the beginning while keeping the text selected using basic C#/C++ functions, however you can override basic "Caret" logic using C++ or/and Assembler.
I want to move the location of label vertically or horizontally and it should appear from in the windows form and should be invisible at a location by moving. I want to make it through .net application using c# so can any one help me for this?
Buddy, you can use the property Location
MyLabel.Location.x = ??
MyLabel.Location.y = ??
Then hide it by using the propertyVisible
MyLabel.Visible = false`
It's difficult to tell exactly what you're trying to accomplish here. I'm going to assume that you want to move a label from its current position on the form to a new position while the application is running. I also assume that you want to make the label invisible while you're moving it so that you cannot see it move across the form.
You can do this easily by setting the Location property of the label you want to move to its new location. (If necessary, like if you want to move the label a relative number of pixels, you can get the label's current position from the Location property before you set it.) The label control also has a Visible property that you can set to True or False to show/hide the control, respectively:
//Hide the label first
myLabel.Visible = false;
//Move the label to a new location on the form
myLabel.Location = new Point(30, 25);
//Make the label visible again
myLabel.Visible = true;
If I guessed wrong, and you're just trying to move the label during design-time (before you start running your program), you can just drag-and-drop it to a new position on the form.
you just drag and drop
You can make a label invisible by using the Visible Property.
myHiddenLabel.Visible = false;