Skip to main content

Unanswered Question How to get multiline text box cursor positionRSS Feed

(0)

vijchn
vijchn

Member

Member

24 points

37 Posts

How to get multiline text box cursor position

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
K2P2

Participant

Participant

1035 points

347 Posts

Re: How to get multiline text box cursor position

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;

Software life cycle:

Requirements: fix this; Design: break it down; Devel: get it working then fix it; Testing: if it ain't broke don't fix it; Help: it's broke, here's how to fix it; Sales: it will be fixed in the next version.

vijchn
vijchn

Member

Member

24 points

37 Posts

Re: How to get multiline text box cursor position

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

K2P2
K2P2

Participant

Participant

1035 points

347 Posts

Re: How to get multiline text box cursor position

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.

Software life cycle:

Requirements: fix this; Design: break it down; Devel: get it working then fix it; Testing: if it ain't broke don't fix it; Help: it's broke, here's how to fix it; Sales: it will be fixed in the next version.

vijchn
vijchn

Member

Member

24 points

37 Posts

Re: How to get multiline text box cursor position

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
dagaric

Member

Member

4 points

9 Posts

Re: Re: How to get multiline text box cursor position

any solutions?

alan.smithee
alan.smi...

Member

Member

2 points

1 Posts

Re: Re: How to get multiline text box cursor position

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>

code-behind: 

 

 

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
rontch

Member

Member

12 points

6 Posts

Re: Re: How to get multiline text box cursor position

The TextBox control has the following property (that is instead of the 'Multiline' property)

AcceptsReturn="True"

 hope it helps :)

 

codeblock
codeblock

Contributor

Contributor

4070 points

716 Posts

Silverlight MVP

Re: How to get multiline text box cursor position

> 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

If this answers your question, please "mark it as answer"

--
Andrea Boschin
MVP Silverlight & Silverlight Insider
, Me on Twitter
  • Unanswered Question
  • Answered Question
  • Announcement