Results 1 to 18 of 18

Thread: [RESOLVED] copy word when click it

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2012
    Posts
    58

    Resolved [RESOLVED] copy word when click it

    I need code
    When i click a word in the textbox, copy it to another text box
    text2.text= the word
    thanks

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: copy word when click it

    Have a look at the .SelText property of the Textbox

  3. #3
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    500

    Re: copy word when click it

    Code:
    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.

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2012
    Posts
    58

    Re: copy word when click it

    another code
    Code:
    Private Sub RichTextBox1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    
    If Button = 1 Then
    Text2= RichTextBox1.SelText 
    
    End Sub
    thanks

  5. #5
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: [RESOLVED] copy word when click it

    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.

  6. #6
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    500

    Re: [RESOLVED] copy word when click it

    I don't get it, Elroy. Your code copies the entire contents of Text1

  7. #7
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: [RESOLVED] copy word when click it

    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.

  8. #8
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    500

    Re: [RESOLVED] copy word when click it

    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.

  9. #9
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: [RESOLVED] copy word when click it

    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"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  10. #10
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    500

    Re: [RESOLVED] copy word when click it

    Quote Originally Posted by LaVolpe View Post
    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

  11. #11
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: [RESOLVED] copy word when click it

    Just to follow up and a couple of my statements, if clarity is needed.
    1) post #3 is not OP's post; it's mine
    oops, meant #4
    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:
    The intent can be inferred by post #4 which the OP then resolved after posting it
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  12. #12
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: [RESOLVED] copy word when click it

    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

    Name:  sshot.png
Views: 138
Size:  7.6 KB

    You can dump plain text into a RichTextBox as well as RTF text.
    Attached Files Attached Files

  13. #13
    Frenzied Member
    Join Date
    Dec 2008
    Location
    Melbourne Australia
    Posts
    1,487

    Re: [RESOLVED] copy word when click it

    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.

  14. #14
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: [RESOLVED] copy word when click it

    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.

  15. #15
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: [RESOLVED] copy word when click it

    Here's a version using InkEdit controls:

    Name:  sshot.png
Views: 124
Size:  7.2 KB

    Red oval marks the "click"


    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).

    Tablet PC Platform History

    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.
    Attached Files Attached Files

  16. #16

    Thread Starter
    Member
    Join Date
    Aug 2012
    Posts
    58

    Re: [RESOLVED] copy word when click it

    Thank you dilettante

  17. #17
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: [RESOLVED] copy word when click it

    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.

  18. #18

    Thread Starter
    Member
    Join Date
    Aug 2012
    Posts
    58

    Re: [RESOLVED] copy word when click it

    Thank you Elroy , your code work well

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