Click to See Complete Forum and Search --> : Insert text into string in textbox


rockies1
June 15th, 2001, 12:02 PM
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!

John G Duffy
June 15th, 2001, 12:47 PM
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

phunkydude
June 15th, 2001, 01:20 PM
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

Catrina
June 15th, 2001, 04:02 PM
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

Ed_CompSci
September 16th, 2001, 12:18 AM
Not really knowing, I could logically think of using concatenation with the caption property. I have not tried it.