Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8106553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:30:17+00:00 2026-06-06T00:30:17+00:00

I am trying to work out how I can insert the string End into

  • 0

I am trying to work out how I can insert the string “End” into my textbox at a specific cursor point?
(where the ‘???’ is in the code below)

As you can see by the code below this will happen when the user clicks return on the keyboard within the textbox.

I have the code to get the cursor index which is being stored as integer ‘intcurrentcolumn’.

Thanks

 Private Sub Enter_Click(ByVal Sender As System.Object, ByVal k As System.Windows.Forms.KeyEventArgs)

     Dim MyTextBox As TextBox = sender
     Dim intindex As Integer
     Dim intcurrentcolumn As Integer
     Dim NewString As String

     If k.KeyCode = Keys.Return Then

        k.SuppressKeyPress = True

        intindex = MyTextBox.SelectionStart
        intcurrentColumn = intindex - MyTextBox.GetFirstCharIndexFromLine(intcurrentLine)

        If intindex = MyTextBox.Text.Length Then

         NewString = MyTextBox.Text & "<End>"

        Else: 

        ???

        End If

        MyTextBox.Text = NewString

        'Sets cursor to end of textbox
        MyTextBox.Select(MyTextBox.Text.Length, 0)

      End If

Thanks In Advance !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T00:30:18+00:00Added an answer on June 6, 2026 at 12:30 am

    The String.Insert method works but resets the cursor position which is generally not what you want (although your code resets it afterwards anyway). A better alternative is to just set the SelectedText:

    MyTextBox.SelectedText = "<End>"
    

    In your case, the selected text simply has length 0 before you insert the string "<End>".

    This also makes the If…Else distinction unnecessary.

    Private Sub Enter_Click(ByVal Sender As Object, ByVal k As System.Windows.Forms.KeyEventArgs)
        If k.KeyCode = Keys.Return Then
            Dim MyTextBox As TextBox = DirectCast(sender, TextBox)
            MyTextBox.SelectedText = "<End>"
            MyTextBox.SelectionStart = MyTextBox.Text.Length
            k.SuppressKeyPress = True
        End If
    End Sub
    

    Note that I’ve also fixed a bug in your code: the assignment of sender to MyTextBox needs an explicit cast! If your original code compiled, you should (!) set Option Strict On. This is essential for improved type checking by the compiler, and should be seen as an unconditional requirement1.

    Furthermore, don’t declare variables before you use them. Declare them at the latest possible point, when you first assign a value to them. This makes the program state easier traceable and often results in shorter code.


    1 (unless you work a lot with COM late binding, in which case you can disable it on a per-file base).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can't figure out how to insert a new string in a specific emplacement in
I am trying to work out why I can not access the nested function
I have a generic Collection and am trying to work out how I can
I am trying work out with MERGE statment to Insert / Update Dimension Table
The data reader named eventreader can be empty and i'm trying to work out
I'm declaring a struct inside my code and then trying to insert it into
I have been trying to work out how to insert text before and after
Im trying to work out how to get the value from one setting in
I trying to work out what regex will match filenames in quotes. Eg. blah
i am trying to work out the following problem in Java (although it could

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.