Skip to main content
Microsoft Silverlight
Home Forums General Silverlight Getting Started How to get multiline text box cursor position
8 replies. Latest Post by codeblock on May 17, 2009.
(0)
vijchn
Member
24 points
37 Posts
09-05-2008 12:33 AM |
Hi, The following code void hlb_MouseEnter(object sender, MouseEventArgs e) { Point position = e.GetPosition(null); string a =position.X.toString(); string b =position.Y.toString(); } Gives me the mouse cursor position, But I need the cursor position that blinks in text box so please give me right suggestion Thanks
K2P2
Participant
1035 points
347 Posts
09-05-2008 10:55 AM |
I think you have to play around with these:
TextBox TB = new TextBox ();int len = TB.SelectionLength;int start = TB.SelectionStart; //character index.int curLine = TB.GetLineIndexFromCharacterIndex ( start );TB.ScrollToLine ( curLine );String S1 = TB.GetLineText ( curLine );String S2 = TB.SelectedText;
09-09-2008 1:30 AM |
Hi Thanks for replay But I got three errors when I use following lines of code. TextBox TB = new TextBox (); int len = TB.SelectionLength; int start = TB.SelectionStart; //character index. int curLine = TB.GetLineIndexFromCharacterIndex ( start ); TB.ScrollToLine ( curLine ); String S1 = TB.GetLineText ( curLine ); String S2 = TB.SelectedText; Error 1 'System.Windows.Controls.TextBox' does not contain a definition for 'GetLineIndexFromCharacterIndex' and no extension method 'GetLineIndexFromCharacterIndex' accepting a first argument of type 'System.Windows.Controls.TextBox' could be found (are you missing a using directive or an assembly reference?) Error 2 'System.Windows.Controls.TextBox' does not contain a definition for 'ScrollToLine' and no extension method 'ScrollToLine' accepting a first argument of type 'System.Windows.Controls.TextBox' could be found (are you missing a using directive or an assembly reference?) Error 3 'System.Windows.Controls.TextBox' does not contain a definition for 'GetLineText' and no extension method 'GetLineText' accepting a first argument of type 'System.Windows.Controls.TextBox' could be found (are you missing a using directive or an assembly reference?) so please suggest me some other solution thanks
09-09-2008 10:07 AM |
Sorry,
I must have been working in a WPF program and didn't realize it.
I think that all you need to do is comment out these three lines and use the others.
I think SelectionStart is what you want.
09-10-2008 1:00 AM |
hi, SelectionStart method of text box is doesn't give me right cursor(caret) position coordinates to display pop u up list box their Thanks
dagaric
4 points
9 Posts
03-10-2009 9:37 AM |
any solutions?
alan.smi...
2 points
1 Posts
05-13-2009 7:15 PM |
Here’s one approach:
Back the TextBox with a separate TextBox that is very small (20 x 20) and hidden in the layout (ZIndex = -1). Sync the text between the “real” TextBox and the backing TextBox, and set the selectionStart to the same position as the “real” TextBox (this will force the backing TextBox to scroll so that the selection point is in view). Then, you can determine the caret position of the “real” TextBox based on the scroll position of the backing TextBox.
Here's an example that positions a third, floating textbox based on the cursor position:
xaml:
<UserControl x:Class="CaretPosition.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="600" Height="300"> <Canvas x:Name="LayoutRoot" Background="White"> <TextBox HorizontalAlignment="Left" Canvas.Left="50" VerticalAlignment="Top" x:Name="positionTextBox" Canvas.ZIndex="2" Text="position" Canvas.Top="25" Visibility="Visible" Width="100" Height="25"></TextBox> <TextBox x:Name="xamlTextBox" Text="asdfasdfasd asda asdf as adfasdfads adf asdf adfasdf" SelectionForeground="AliceBlue" KeyDown="xamlTextBox_KeyDown" KeyUp="xamlTextBox_KeyUp" TextWrapping="NoWrap" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Visible" AcceptsReturn="True" Height="200" Width="500" /> <TextBox x:Name="shadowTextBox" Canvas.ZIndex="-1" Canvas.Left="350" Height="20" Width="20" HorizontalScrollBarVisibility="Visible" AcceptsReturn="True" VerticalScrollBarVisibility="Visible" Visibility="Visible" TextWrapping="NoWrap"/> </Canvas></UserControl>
using System; using System.Collections.Generic; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Shapes; namespace CaretPosition { public partial class Page : UserControl { int verticalOffsetFromCursor = 25; int horizontalOffsetFromCursor = 0; ScrollViewer sv; public Page() { InitializeComponent(); } private void xamlTextBox_KeyDown(object sender, KeyEventArgs e) { shadowTextBox.Text = xamlTextBox.Text; shadowTextBox.SelectionStart = xamlTextBox.SelectionStart; } private Point getCaretPosition(TextBox textBox, TextBox shadowTextBox) { Point _point = new Point(); // get main textbox's scroll offset, if any getScrollBar(textBox); double initVerticalOffset = sv.VerticalOffset; double initHorizontalOffset = sv.HorizontalOffset; // get shadow box scroll offset getScrollBar(shadowTextBox); double vOffset = sv.VerticalOffset; double hOffset = sv.HorizontalOffset; // caret position is scroll offset of shadaw minus scroll offset of main (if any) _point.Y = vOffset - initVerticalOffset; _point.X = hOffset - initHorizontalOffset; return _point; } private void setPopupShadowBox(TextBox textBox, TextBox shadowTextBox) { shadowTextBox.Text = xamlTextBox.Text; shadowTextBox.SelectionStart = xamlTextBox.SelectionStart; } private void getScrollBar(UIElement src) { // walk visual tree for this object until we get the scrollviewer if (src.GetType().ToString() == "System.Windows.Controls.ScrollViewer") sv = src as ScrollViewer; else { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(src); i++) { UIElement elem = (UIElement)VisualTreeHelper.GetChild(src, i); getScrollBar(elem); } } } private void xamlTextBox_KeyUp(object sender, KeyEventArgs e) { // get caret position Point p = getCaretPosition(xamlTextBox, shadowTextBox); // move "floating" text box to caret position positionTextBox.SetValue(Canvas.LeftProperty, p.X + horizontalOffsetFromCursor); positionTextBox.SetValue(Canvas.TopProperty, p.Y + verticalOffsetFromCursor); positionTextBox.Text = p.ToString(); } } }
rontch
12 points
6 Posts
05-16-2009 11:01 PM |
The TextBox control has the following property (that is instead of the 'Multiline' property) AcceptsReturn="True"
The TextBox control has the following property (that is instead of the 'Multiline' property)
AcceptsReturn
hope it helps :)
codeblock
Contributor
4070 points
716 Posts
05-17-2009 10:04 AM |
> Hi, The following code void hlb_MouseEnter(object sender, MouseEventArgs e) { Point position = e.GetPosition(null); string a =position.X.toString(); string b =position.Y.toString(); } Gives me the mouse cursor position, But I need the cursor position that blinks in text box so please give me right suggestion Thanks
I understand you need the caret position in the text expressed in line of text and charachters from beginnig of line. If I understand fine you have to rely on the SelectionStart property that give you the current position of the caret when there is no selection. Give the position you can count the <newline> before it to know the line number and the characters from last <newline> for the horizontal position...
I don't know why you need it... but hope this helps :)
bye