Private Sub Text1_DblClick()
Text2.Text = Text1.SelText
End Sub
This will copy only one word (as you requested) but if you want to select and copy multiple words then use the Text1_Click() event and you have to hold the mouse down while dragging it accross the multiple words
Last edited by Ordinary Guy; Mar 21st, 2017 at 11:55 AM.
Given ANSI TextBoxes, I'd tend to do it something like the following (a form with two TextBoxes, Text1 and Text2). This seems to be more true to the way the original post poses the question. Also, it works with a multi-line TextBox.
Code:
Option Explicit
'
Dim sBreaks As String
'
Private Sub Form_Load()
Dim i As Long
'
For i = 0 To 47
sBreaks = sBreaks & Chr$(i)
Next i
For i = 58 To 64
sBreaks = sBreaks & Chr$(i)
Next i
For i = 91 To 96
sBreaks = sBreaks & Chr$(i)
Next i
For i = 123 To 255
sBreaks = sBreaks & Chr$(i)
Next i
End Sub
Private Sub Text1_Click()
Dim iPtr As Long
Dim iStart As Long
Dim iEnd As Long
'
iPtr = Text1.SelStart + 1
If InStr(sBreaks, Mid$(Text1.Text, iPtr, 1)) Then
Text2.Text = ""
Exit Sub ' If we're not on a word, forget it.
End If
'
iStart = TheWordStart(Text1.Text, iPtr)
iEnd = TheWordEnd(Text1.Text, iPtr)
'
Text2.Text = Mid$(Text1.Text, iStart, iEnd - iStart + 1)
End Sub
Private Function TheWordStart(s As String, iPtr As Long) As Long
Dim i As Long
'
For i = iPtr - 1 To 1 Step -1
If InStr(sBreaks, Mid$(s, i, 1)) Then
TheWordStart = i + 1
Exit Function
End If
Next i
TheWordStart = 1
End Function
Private Function TheWordEnd(s As String, iPtr As Long) As Long
Dim i As Long
'
For i = iPtr + 1 To Len(s)
If InStr(sBreaks, Mid$(s, i, 1)) Then
TheWordEnd = i - 1
Exit Function
End If
Next i
TheWordEnd = Len(s)
End Function
Enjoy,
Elroy
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
Hmmm, not for me. I'm just putting random words, separated by spaces into Text1, and then I click around on the different words, and it puts the word clicked into Text2.
Did you try it? Or are you just staring at the code?
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
Well, I started out just staring at the code wondering why one would go through all that coding just to wind up getting the exact same thing as Text2.Text = Text1.SelText. Since I found your code very interesting I just had to try it out since I thought perhaps it had to do more than just that so I copied your code as is, dumped it in an empty project, added Text1 and Text2 and ran the project. Clicked on just the middle word and Text2 then showed the entire text from Text1. I tested many times clicking on random words and even inter-word spaces over and over and every time I got the entire text from Text1. I thought perhaps it was because I am using XP so I copied the project over to another PC which runs 8.1 and it does the same thing.
Even if it worked I can't figure out why you would write it like that. What do you think it does better than what has already been described earlier
Last edited by Ordinary Guy; Mar 21st, 2017 at 04:58 PM.
Well, based on the OP's post #3, the real request was how to copy the selected text not the 'word' clicked on (which could be implied, nothing is selected at that point)
Regarding whether Elroy's works or not, it does, as written. Ordinary Guy, did you forget to include the Form_Load code? If sBreaks value is null string, entire contents is copied as you describe.
Last edited by LaVolpe; Mar 21st, 2017 at 05:03 PM.
Reason: edited perceived intent
Insomnia is just a byproduct of, "It can't be done"
Well, based on the OP's post #3, the real request was how to copy the selected text not the 'word' clicked on (which could be implied, nothing is selected at that point)
Regarding whether Elroy's works or not, it does, as written. Ordinary Guy, did you forget to include the Form_Load code? If sBreaks value is null string, entire contents is copied as you describe.
OK, I see that the Dim sBreak got stuck elsewhere in the code so it didn't get placed in the declaration area. That now solves the problem I had.
Now, a few comments on your post
1) post #3 is not OP's post; it's mine
2) OP's post #4 does the same thing as my post #3 and copies only one word, the word that is clicked on, to Text2
3) OP's request was how to copy one word by clicking on it, not the selected text as you say. See his words below:
I need code
When i click a word in the textbox, copy it to another text box
text2.text= the word
thanks
Question still remains for Elroy. What does Elroy's code do or do better than the simple Text2.Text = Text1.SelText single line code
If this is really about clicking a word you could probably use TOM with a RichTextBox to get at some of the additional capabilities of the underlying RichEdit control.
Code:
Option Explicit
Private Const WM_USER As Long = &H400&
Private Const EM_GETOLEINTERFACE As Long = WM_USER + 60
Private Declare Function SendMessage Lib "user32" Alias "SendMessageW" ( _
ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Private tdRichTextBox1 As tom.ITextDocument
Private Sub Form_Load()
Dim IUnknown As IUnknown
With RichTextBox1
SendMessage .hWnd, EM_GETOLEINTERFACE, 0, VarPtr(IUnknown)
Set tdRichTextBox1 = IUnknown
.LoadFile "Some Lorem.rtf", rtfRTF
End With
End Sub
Private Sub RichTextBox1_Click()
With tdRichTextBox1.Selection.Duplicate
.Collapse tomStart 'Collapse possible multiword range.
.Expand tomWord 'Expand to current word.
Text1.Text = .Text
End With
End Sub
You can dump plain text into a RichTextBox as well as RTF text.
OP (light3),
Was your (brief) OP (post 1), referring to ordinary textboxes or RichTextBoxes ?
If ordinary were they multi-line ?
Try to give fuller explanations of your needs.
Also for the sanity of contributors, and for the assistance of future browsers, it is polite to post any clarification PLUS the final solution.
You can also make use of the InkEdit control that has shipped as part of Windows since Vista came out in 2006. This is a RichEdit control wrapped along with the "ink" (handwriting) input recognizer within an ActiveX control VB6 can easily use. You can turn off ink recognition and you have a Unicode-aware RichEdit without the need to deploy any OCX.
If you weren't asleep at the switch you also previously downloaded the inkless redist version of this control that you can deploy to XP SP1 or later. Microsoft has pulled the download though because XP is dead.
Here both a single-line and a multi-line InkEdit are used, shifted into plain-text mode (no RTF) with ink input disabled. Another single-line InkEdit is also used here to display the clicked word.
This has nothing to deploy except the EXE on Vista or later. For XP SP1+ you'd need the redist OCX from the old Tablet PC SDK Version 1.1, which is no longer available. XP Tablet Edition of course has the fully ink-enabled version of the Ink controls (InkEdit, InkPicture).
Note Applications being distributed to non–Tablet PC operating systems can use a subset of Tablet PC platform features. These applications must include the redistributed merge module that shipped in the version 1.1 Development Kit with their setup.
Well, I've been away, and I see there's been a lively discussion.
LaVolpe, thanks for clearing up the mis-understanding about my code.
And Light3, I just took your OP as literally as I possibly could and had some fun with it. As others have said, SelText is certainly a useful property. However, sometimes when I have a concept in my head, I don't like to compromise just because some other way makes it easier (such as a double-click/select rather than a single-click and no select). I must admit that there have been times when I've contorted substantially to make something work "exactly" like the idea in my head.
All The Best,
Elroy
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.