+ Reply to Thread
Results 1 to 8 of 8

Right-click in userform textbox.

Hybrid View

  1. #1
    Registered User
    Join Date
    07-30-2009
    Location
    Vancouver, British Columbia
    MS-Off Ver
    Excel 2010
    Posts
    46

    Question Right-click in userform textbox.

    Hi,

    How can I have the ability to right-click in a textbox? Currently when I right-click, nothing happens. What I would like is to have the user be able to right-click and then copy the text from the textbox.

    Any ideas

    Cheers,

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,485

    Re: Right-click in userform textbox.

    Previous thread
    http://www.excelforum.com/excel-prog...-userform.html
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    07-30-2009
    Location
    Vancouver, British Columbia
    MS-Off Ver
    Excel 2010
    Posts
    46

    Unhappy Re: Right-click in userform textbox.

    Quote Originally Posted by Andy Pope View Post
    That code doesn't work at all in my WB. I can't seem to find anything

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,485

    Re: Right-click in userform textbox.

    why not post your workbook then

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Right-click in userform textbox.

    The code works fine. here's the code in a workbook
    Attached Files Attached Files
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  6. #6
    Registered User
    Join Date
    07-30-2009
    Location
    Vancouver, British Columbia
    MS-Off Ver
    Excel 2010
    Posts
    46

    Question Re: Right-click in userform textbox.

    Ok I've gotten a code after googling for a while, and I got a menu to come up when I right-click in a textbox (menu consists of copy and paste selections).

    Now the only problem is, even after I highlight the text, right-click, and hit copy, nothing is copied to the clipboard!

    Here's the code I have ... one is for the userform code, and the other is for the module:

    UserForm

    ' Right-Click Menu
    
    Private Sub TextBox1_GotFocus()
        Set ctl = TextBox1
    End Sub
    
    Private Sub tbFranchiseNumber_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
        If Button = vbKeyRButton Then Call ShowTextMenu
    End Sub
    
    Sub ShowTextMenu()
        Call MakeMenu
        Application.CommandBars("TextBox Bar").ShowPopup
    End Sub
    
    Sub MakeMenu()
    
        On Error Resume Next
        CommandBars("TextBox Bar").Delete
        On Error GoTo 0
    
        Dim cbTextBox As CommandBar
        Dim cmdTest As CommandBarButton
    
        Set cbTextBox = Application.CommandBars.Add(Name:="TextBox Bar", Position:=msoBarPopup, Temporary:=True)
    
        With cbTextBox
            For b = 1 To 2
                Set cmdTest = .Controls.Add(Type:=msoControlButton, Temporary:=True)
                With cmdTest
                    .Style = msoButtonIconAndCaption
                    .Parameter = b
                    Select Case b
                        Case 1
                            .FaceId = 19
                            .Caption = "Copy"
                        Case 2
                            .FaceId = 22
                            .Caption = "Paste"
                    End Select
                    .OnAction = "TextMenu"
                End With
            Next b
        End With
    
    End Sub
    Module

    Public txtData As DataObject
    Public ctl As Object
    
    Public Sub TextMenu()
    
        Dim ctlCBarControl As CommandBarControl
        Dim i As Integer
        Dim s As Long
    
        Set ctlCBarControl = CommandBars.ActionControl
        If ctlCBarControl Is Nothing Then Exit Sub
        i = CInt(ctlCBarControl.Parameter)
    
        With UserForm1
            Select Case i
                Case 1
                    Set txtData = New DataObject
                    txtData.SetText .TextBox1.SelText
                    txtData.PutInClipboard
                    If i = 1 Then
                        s = .TextBox1.SelStart
                        .TextBox1 = Left(.TextBox1, .TextBox1.SelStart) & _
                                    Mid(.TextBox1, .TextBox1.SelStart + _
                                    .TextBox1.SelLength + 1)
                        .TextBox1.SelStart = s
                    End If
                Case 2
                    .TextBox1.Paste
            End Select
        End With
    End Sub
    Any ideas

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Right-click in userform textbox.

    Do you have macros called "Copy" & "Paste"? What do you want to copy & paste?

    Attach the workbook

  8. #8
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,485

    Re: Right-click in userform textbox.

    Your are using inconsistent references.

    is it Textbox1 or tbFranchiseNumber ?

    Private Sub TextBox1_GotFocus()
        Set ctl = TextBox1
    End Sub
    
    Private Sub tbFranchiseNumber_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
        If Button = vbKeyRButton Then Call ShowTextMenu
    End Sub
    Also your copy section of TextMenu actually Cuts the selected text after copying.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1