Solved

How to position the cursor inside a string?

Posted on 2008-06-12
18
254 Views
Last Modified: 2010-10-27
Hi Experts!
How can I set the position of the cursor to a precise point on a string?
For example: Between two html tags (<a href="blah">*</a>) I need the cursor where the asterisk is. Is there a way to get that to work?
Thanks in advance.
0
Comment
Question by:jorgemgonzalez
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 7
18 Comments
 
LVL 48

Expert Comment

by:jpaulino
ID: 21776390
If you're using that on WinForms you can use SelectionStart and SelectionLenght. You just need to find the position you want.

Look to this example how to use that:
http://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.selectionstart(VS.85).aspx
0
 

Author Comment

by:jorgemgonzalez
ID: 21776514
Thanks jpaulino for your fast reply.

Let me clarify for you.
What I need is to make the I beam to appear in a given part of a text line, let's say that we have a string like this"
strAddr = "<a href=""""></a>"
Then on a click of a button the string is passed to a textbox
TextBox1.SelectedText = strAddr

Now, in TextBox1 I need to have the I beam between ">" and "<" so the user can input text without having to position the I beam.
 The result should look like this: <a href="""">cursor</a> (Where "cursor" is the I beam)
Can that be done programmaticaly?
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 21776572
You can look (using IndexOf) the position of the tag "</a>" and then position the cursor there.

Something like:
        Dim str As String = Me.TextBox1.Text
        Me.TextBox1.SelectionLength = 0
        Me.TextBox1.SelectionStart = str.IndexOf("</a>")
        Me.TextBox1.Select()

Open in new window

0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:jorgemgonzalez
ID: 21776746
It gives me an error: InvalidArgument=Value of '-1' is not valid for 'SelectionStart'. Parameter name: SelectionStart
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 21776763
-1 is because it couldn't find the expression that you have entered. Check if you really have that tag in the text or if it has spaces.
0
 

Author Comment

by:jorgemgonzalez
ID: 21776799
Yup, the tag is there, it's passed to the text box by another form.

However, the length of the string may vary, on the other hand, I wonder what would happen if the tag is inserted again some text after.

This is what I have there:


Private Sub cmdOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOk.Click
        Dim Doc As frmDoc
        Doc = frmMain.ActiveMdiChild
        If Me.CheckBox1.Checked = True And Me.RadioButton1.Checked = True Then
            strAddr = "<a href=" & """" & Me.TextBox1.Text & """" & ">" & Me.TextBox3.Text & "</a>"
        ElseIf Me.CheckBox2.Checked = True And Me.RadioButton1.Checked = True Then
            strAddr = "<a href=" & """" & Me.TextBox1.Text & """" & ">" & Me.lblImg.Text & "</a>"
        ElseIf Me.CheckBox1.Checked = True And Me.RadioButton2.Checked = True Then
            strAddr = "<a href=" & """" & "mailto:" & Me.TextBox1.Text & "?subject= " & Me.TextBox2.Text & """" & ">" & Me.TextBox3.Text & "</a>"
        ElseIf Me.CheckBox2.Checked = True And Me.RadioButton2.Checked = True Then
            strAddr = "<a href=" & """" & "mailto:" & Me.TextBox1.Text & "?subject= " & Me.TextBox2.Text & """" & ">" & Me.lblImg.Text & "</a>"
        End If
        Doc.TextBox1.SelectedText = strAddr
        Me.Close()
    End Sub

Open in new window

0
 
LVL 48

Expert Comment

by:jpaulino
ID: 21776828

And where do you use the SelectionStart ?

>> I wonder what would happen if the tag is inserted again some text after.
If will find the first that it found and place the cursor there.

0
 

Author Comment

by:jorgemgonzalez
ID: 21776836
I remember a program I tried long ago, called "Edit+", you could write your own "snippets" and you could place the cursor in that snippet by typing ^!, and when you imported your snippet the cursor appeared where the "^!" was. I which I know how they did it! :)
0
 

Author Comment

by:jorgemgonzalez
ID: 21776840
No, I gave you what I had originally so you can analize the string
0
 

Author Comment

by:jorgemgonzalez
ID: 21776846
That's what I was thinking, but the thing is that the tag might be repeated several times.

There mught be a trick tough.
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 21776946
You open a MDI chield and add the text using the above code. Then you try to place the cursor where ? After this line ?
Doc.TextBox1.SelectedText = strAddr

Try this way:
Doc.TextBox1.SelectedText = strAddr
Doc.TextBox1.SelectionLength = 0
Doc.TextBox1.SelectionStart = strAddr.IndexOf("</a>")
Me.Close()

0
 

Author Comment

by:jorgemgonzalez
ID: 21776997
It works my friend, but only is there's no other text in the textbox, if there is it places the cursor somewhere else, that's exactly my dilemma.

I really appreciate your effort.
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 21777082
>> only is there's no other text in the textbox, if there is it places the cursor somewhere else, that's exactly my dilemma.

But should place before the tag. If you have <a href="">fdsfdfs1</a> it should place after the 1 and before the tag.

Maybe I'm understanding wrongly! Can you give one or two examples of what you have an what you need (not the code but the text you have)
0
 

Author Comment

by:jorgemgonzalez
ID: 21777127
What i'm doing is a simple html editor, as such, before an anchor tag there goes a lot of text. Once that text is there, by setting the selection length at 0, the cursor goes to the place equivalent to the lenght of the tag string before the </a> (strAddr.IndexOf("</a>"))

Now, if the textbox in the parent form is empty (selection lenght = 0) then it works like a charm.

What I need is a way to get the cursor appear before </.a> anytime the tag is inserted regardless of the amount of text in the editor (textbox) if there's any.

Thanks a lot for your patience.
0
 
LVL 48

Accepted Solution

by:
jpaulino earned 500 total points
ID: 21777266
And this way:

Doc.TextBox1.SelectionLength = 0
Doc.TextBox1.SelectionStart = Doc.TextBox1.Text.IndexOf("</a>")
Me.Close()

I really think this is an easy thing but I'm missing something. Do you want that I upload an example or can you upload your projetc (www.ee-stuff.com) ? you can delete the unecessary stuff if you want.
0
 

Author Comment

by:jorgemgonzalez
ID: 21784191
With this other approach:
Doc.TextBox1.SelectionLength = 0
Doc.TextBox1.SelectionStart = Doc.TextBox1.Text.IndexOf("</a>")
Me.Close()
it works fine, the problem I have still is that if the user needs to add another link tag after some other text then the selection will be at the first </a>
What can be done?
0
 

Author Comment

by:jorgemgonzalez
ID: 21784375
Well, I figured out that using:
Dim StartPos As Integer = Doc.TextBox1.SelectionStart
Doc.TextBox1.SelectedText = strAddr
        Doc.TextBox1.SelectionLength = StartPos
        Doc.TextBox1.SelectionStart = StartPos + strAddr.IndexOf("</a>")
        Doc.TextBox1.Select()
        Doc.TextBox1.SelectionLength = 0
it works on every occasion, I just don't know how orthodox or appropriate the method is, but it's working!

Thanks a million jpaulino for pointing me in the right direction.
0
 

Author Closing Comment

by:jorgemgonzalez
ID: 31466832
Thanks for your patience
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question