CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 1999
    Location
    Blue Springs, MO
    Posts
    43

    Insert text into string in textbox

    I have a textbox that will have text in it. (That's it's purpose, right?)

    I want my users to press a command button, and insert a number into the text at the point where the cursor was.

    For example, if I have "My name is Fred" in the textbox, and the cursor is between the N and A in name, when the user presses the button 2, I want the textbox to read "My n2ame is Fred"

    Anyone have any idea how to do this?

    Thanks!


  2. #2
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Insert text into string in textbox


    option Explicit

    private Sub Command1_Click()
    Text1.SelStart = 4 ' position cursor
    Text1.SetFocus ' set the focus to text box
    SendKeys "2" ' send it a character
    End Sub

    private Sub Form_Load()
    Text1 = "My name is Fred"
    End Sub




    John G

  3. #3
    Join Date
    Aug 2000
    Location
    Namibia
    Posts
    139

    Re: Insert text into string in textbox

    This snippet has lots of limitations, just meant to point you in some direction.
    Limitations
    1) Text box must be big enough to accomodate all of text as visible
    2) you must use a fixed width font, I used Courier in this example.

    private Type POINTAPI
    x as Long
    y as Long
    End Type

    private Declare Function GetCaretPos Lib "user32" (lpPoint as POINTAPI) as Long

    private Sub Command1_Click()
    'This function assumes that the text box font is a fixed width font ...
    ' e.g. Courier with a size of 8
    Dim pos as POINTAPI
    Dim lResult as Long
    Dim charpos as Integer

    lResult = GetCaretPos(pos)
    If lResult <> 0 then
    charpos = (pos.x - 1) / 8
    Text1.Text = Left$(Text1.Text, charpos) & "2" & Right$(Text1.Text, len(Text1.Text) - charpos)
    else
    'there was some sort of error
    Exit Sub
    End If
    End Sub



    This was just a little favour

    HTH


  4. #4
    Join Date
    Feb 2000
    Location
    Indiana USA
    Posts
    193

    Re: Insert text into string in textbox


    Dim CPoint as Integer
    private Sub Command1_Click()
    CPoint = Text1.SelStart
    End Sub

    private Sub Form_KeyPress(KeyAscii as Integer)
    Dim KP as string

    KP = Chr(KeyAscii)

    Text1.Text = mid(Text1.Text, 1, CPoint) & KP & mid(Text1.Text, CPoint + 1)
    End Sub

    private Sub Form_Load()
    Form1.KeyPreview = true
    End Sub






  5. #5
    Join Date
    Aug 1999
    Location
    California
    Posts
    143

    Re: Insert text into string in textbox

    Not really knowing, I could logically think of using concatenation with the caption property. I have not tried it.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured