Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert text at cursor...

Status
Not open for further replies.

AKMonkeyboy

IS-IT--Management
Feb 4, 2002
141
US
I've worked out some code (with the help of Tek-Tips experts, of course!) that will insert predefined statements into a users active field. Basically the user presses "CTRL" and a corresponding number to import text to a text box. This has worked well enough, but I would like to make it more user friendly by having the imported text placed at the current cursor position - the code that I have only puts the imported text at the end of the line.

Any thoughts on how to put imported text at the cursor instead?

Here's what I have so far:

Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
On Error GoTo Err_Form_KeyDown

Dim skc: skc = Chr(KeyCode)
  
  Select Case Shift
    'Case 0 'no modifier keys
     
    'Case 1 'Shift is down
      
    Case 2  'CTRL is down
        Select Case skc
              Case "i"
                Me.ActiveControl.FontSize = Me.ActiveControl.FontSize + 1
              Case "d"
                Me.ActiveControl.FontSize = Me.ActiveControl.FontSize - 1
              Case "0"
                Me.Refresh
                If IsNull(Me.ActiveControl) = True Then
                    Me.ActiveControl = DLookup("[Comment]", "[TblDefaultComments]", "[ID]=0")
                Else
                    Me.ActiveControl = Me.ActiveControl & " " & DLookup("[Comment]", "[TblDefaultComments]", "[ID]=0")
                End If
              Case "1"
                Me.Refresh
                If IsNull(Me.ActiveControl) = True Then
                    Me.ActiveControl = DLookup("[Comment]", "[TblDefaultComments]", "[ID]=1")
                Else
                    Me.ActiveControl = Me.ActiveControl & " " & DLookup("[Comment]", "[TblDefaultComments]", "[ID]=1")
                End If
              Case "2"
                Me.Refresh
                If IsNull(Me.ActiveControl) = True Then
                    Me.ActiveControl = DLookup("[Comment]", "[TblDefaultComments]", "[ID]=2")
                Else
                    Me.ActiveControl = Me.ActiveControl & " " & DLookup("[Comment]", "[TblDefaultComments]", "[ID]=2")
                End If
              Case "3"
                Me.Refresh
                If IsNull(Me.ActiveControl) = True Then
                    Me.ActiveControl = DLookup("[Comment]", "[TblDefaultComments]", "[ID]=3")
                Else
                    Me.ActiveControl = Me.ActiveControl & " " & DLookup("[Comment]", "[TblDefaultComments]", "[ID]=3")
                End If
              Case "4"
                Me.Refresh
                If IsNull(Me.ActiveControl) = True Then
                    Me.ActiveControl = DLookup("[Comment]", "[TblDefaultComments]", "[ID]=4")
                Else
                    Me.ActiveControl = Me.ActiveControl & " " & DLookup("[Comment]", "[TblDefaultComments]", "[ID]=4")
                End If
              Case "5"
                Me.Refresh
                If IsNull(Me.ActiveControl) = True Then
                    Me.ActiveControl = DLookup("[Comment]", "[TblDefaultComments]", "[ID]=5")
                Else
                    Me.ActiveControl = Me.ActiveControl & " " & DLookup("[Comment]", "[TblDefaultComments]", "[ID]=5")
                End If
              Case "6"
                Me.Refresh
                If IsNull(Me.ActiveControl) = True Then
                    Me.ActiveControl = DLookup("[Comment]", "[TblDefaultComments]", "[ID]=6")
                Else
                    Me.ActiveControl = Me.ActiveControl & " " & DLookup("[Comment]", "[TblDefaultComments]", "[ID]=6")
                End If
              Case "7"
                Me.Refresh
                If IsNull(Me.ActiveControl) = True Then
                    Me.ActiveControl = DLookup("[Comment]", "[TblDefaultComments]", "[ID]=7")
                Else
                    Me.ActiveControl = Me.ActiveControl & " " & DLookup("[Comment]", "[TblDefaultComments]", "[ID]=7")
                End If
              Case "8"
                Me.Refresh
                If IsNull(Me.ActiveControl) = True Then
                    Me.ActiveControl = DLookup("[Comment]", "[TblDefaultComments]", "[ID]=8")
                Else
                    Me.ActiveControl = Me.ActiveControl & " " & DLookup("[Comment]", "[TblDefaultComments]", "[ID]=8")
                End If
              Case "9"
                Me.Refresh
                If IsNull(Me.ActiveControl) = True Then
                    Me.ActiveControl = DLookup("[Comment]", "[TblDefaultComments]", "[ID]=9")
                Else
                    Me.ActiveControl = Me.ActiveControl & " " & DLookup("[Comment]", "[TblDefaultComments]", "[ID]=9")
                End If
              
              End Select
    'Case 3  'CTRL-SHIFT is down
      'txtKey.Text = "You pressed CTRL-SHIFT-" & skc
    'Case 4  'ALT is down
        
    'Case 5  'SHIFT-ALT is down
      'txtKey.Text = "You pressed SHIFT-ALT-" & skc
    'Case 6  'CTRL-ALT is down
      'txtKey.Text = "You pressed CTRL-ALT-" & skc
    'Case 7  'CTRL-ALT-SHIFT is down
      'txtKey.Text = "You pressed CTRL-ALT-SHIFT-" & skc
    'Case Else
      'txtKey.Text = "huh? SHIFT=" & Shift & " KEYCODE=" & KeyCode
  End Select
  
Exit_Form_KeyDown:
    Exit Sub

Err_Form_KeyDown:
    MsgBox Err.Description
    Resume Exit_Form_KeyDown
End Sub

Give a man a fish, and you feed him for a day.
Teach a man to fish, and you feed
him for life.
Send a man to Tek-Tips and the poor sap can find out how to fish on his own, and learn more by doing it.
 
You can get the cursor position in a textbox with the .selstart property.

From there it is a simple matter of using left, right and length to break the existing text into left of cursor and right of cursor chunks and putting it back together with your comment in the middle.
 
I have tried a variation of this, but seem to get hung up on getting new values. That is, if I use Me.Refresh anywhere in the KeyDown statement it causes strange behavior - without the Me.Refresh, I don't get the right information... and the loop goes on.

Have you ever implemented something like this?

Thanks for your input,

Monkey

Give a man a fish, and you feed him for a day.
Teach a man to fish, and you feed
him for life.
Send a man to Tek-Tips and the poor sap can find out how to fish on his own, and learn more by doing it.
 
Code:
Me.ActiveControl.SelText = "INSERTED TEXT"

' or

TextBox1.SelText = "INSERTED TEXT"
 
I tried briefly your solution, WinblowsME (nice handle BTW) - but haven't had time to extensively work it.

Will post back if I find a solution - in the mean time, any suggestions will be appreciated.

Maybe I can fund some sort of X-Prize for the best solution - what do you think?

Monkey

Give a man a fish, and you feed him for a day.
Teach a man to fish, and you feed
him for life.
Send a man to Tek-Tips and the poor sap can find out how to fish on his own, and learn more by doing it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top