Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Right clicking in a Text Box

Posted on 1999-09-23
14
Medium Priority
?
194 Views
Last Modified: 2010-05-02
Is there any way to stop the standard text box menu (cut, paste etc) appearing when right clicking in a text box? I need to place my own menu on the right click even instead. At the moment, it show the windows menu first and then on the second right click it shows my custom menu!

Thanks.
0
Comment
Question by:lbowers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
  • +3
14 Comments
 
LVL 6

Expert Comment

by:setiawan
ID: 2068607
Option Explicit

Public Declare Function CallWindowProc Lib "user32" _
    Alias "CallWindowProcA" _
   (ByVal lpPrevWndFunc As Long, _
    ByVal hwnd As Long, _
    ByVal Msg As Long, _
    ByVal wParam As Long, _
    ByVal lParam As Long) As Long

Public Declare Function SetWindowLong Lib "user32" _
    Alias "SetWindowLongA" _
   (ByVal hwnd As Long, _
    ByVal nIndex As Long, _
    ByVal dwNewLong As Long) As Long

Public Const GWL_WNDPROC = -4
Public Const WM_RBUTTONUP = &H205
Public defWndProc As Long


Public Sub Hook(hwnd As Long)

   If defWndProc = 0 Then
   
      defWndProc = SetWindowLong(hwnd, _
                                 GWL_WNDPROC, _
                                 AddressOf WindowProc)
   End If
                                 
End Sub


Public Sub UnHook(hwnd As Long)

    If defWndProc > 0 Then
   
      Call SetWindowLong(hwnd, GWL_WNDPROC, defWndProc)
      defWndProc = 0
     
   End If
   
   
End Sub


Public Function WindowProc(ByVal hwnd As Long, _
                           ByVal uMsg As Long, _
                           ByVal wParam As Long, _
                           ByVal lParam As Long) As Long

    Select Case uMsg
        Case WM_RBUTTONUP
       
           'this executes when the window is hooked
            Form1.PopupMenu Form1.mnuPopup
           
        Case Else
       
            WindowProc = CallWindowProc(defWndProc, _
                                        hwnd, _
                                        uMsg, _
                                        wParam, _
                                        lParam)
    End Select
   
End Function
'--end block--'
   

 Form Code
 
To a form, add a top level menu (mnuPopup), and any number of submenu items under it.   Add three command buttons in a control array to the form (cmd(0) - cmd(2)). Add the following code:

--------------------------------------------------------------------------------
 

Option Explicit

Private Sub cmd_Click(Index As Integer)

   Select Case Index
      Case 0: Call Hook(txtHook.hwnd)
      Case 1: Call UnHook(txtHook.hwnd)
      Case 2: Unload Me
   End Select

End Sub


Private Sub Form_Unload(Cancel As Integer)

  'just in case, call the unhook method
   Call UnHook(txtHook.hwnd)
   
End Sub


Private Sub txtHook_MouseUp(Button As Integer, _
                           Shift As Integer, _
                           X As Single, Y As Single)

  'this executes when the window is unhooked
   If Button = vbRightButton Then
       PopupMenu mnuPopup
   End If
   
End Sub

taken from : http://www.mvps.org/vbnet/code/subclass/contextmenu.htm

  regards

   danny
0
 

Author Comment

by:lbowers
ID: 2068861
Is it possible to only hook one text box at a time?
For some reason, this method periodically crashes my VB6 in Win98SE! Is there another way of achieving this?
0
 
LVL 2

Expert Comment

by:alokanant
ID: 2068896
Hi lbowers,

create your menu named mnuPopup on the form.
then write the following code in the mousedown event of the text box -

Text1.enabled = false
text1.enabled = true

popup mnupopup

' assuming that the text1 is the control on the form
' the customized menu mnupopup shall appear instead of the standard menu.

hth
alok.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:kbalaraju
ID: 2069484
This comment is added to receive notification when it is answered.
0
 

Author Comment

by:lbowers
ID: 2077424
Unfortunately, we couldn't get this to work either. It still brought up the windows edit menu. Have you successfully used this method in the past?

We even tried:-

txtDay(Index).Enabled=False
PopUpMenu mnuUserArea
txtDay(Index).Enabled=True

And it still didn't work.

Thanks anyway.
0
 
LVL 2

Expert Comment

by:alokanant
ID: 2077520
Dear lbowers,

i am using the code successfully.
where have u written the code?
you have to write the code in the mousedown event.
check if index holds the correct value.

hth
alok.
0
 

Author Comment

by:lbowers
ID: 2077908
Sorry - You're right it does work. I'd accidently placed the code in the mouse up event! Excellent.

How do I now award you the points ?
0
 
LVL 3

Expert Comment

by:troywillmot
ID: 2079961
The first code (that uses subclassing) is fine, but :

1. it will crash your vb periodically if you stop you project using the stop button in VB etc. as the hook is not removed.

2. You coud do it neater using the source code/activex control from

http://www.softcircuits.com

3. It should work for only one textbox at a time if you pass the textboxes hwnd to the hook/unhook procedures, not the forms. Which were you using ?
0
 
LVL 2

Expert Comment

by:alokanant
ID: 2079986
Hi troywillmot,

is there anything wrong in the answer i suggested?
will it crash vb?

Hi lbowers,

you cannot give points to me until you reject troywillmots answer (if you find it not ok).

alok.
0
 
LVL 3

Expert Comment

by:troywillmot
ID: 2080035
alokanant

No, your code won't crash VB (it doesn't use hooks), and it will work for one text box at a time - sorry, I didn't see it originally.

The API hook will make sure that VB doesn't do anything else first with it though, but I don't think thats a problem here.

Its a good, simple answer. Well done.
0
 

Author Comment

by:lbowers
ID: 2080817
The answer is fine bu I feel alok should have the points.
Hope this is okay with you?

Thanks for tkaing the time to answer.

Leigh.
0
 
LVL 3

Expert Comment

by:troywillmot
ID: 2082554
Sure, doesn't bother me. I was just trying to help.

See ya.
0
 
LVL 2

Accepted Solution

by:
alokanant earned 200 total points
ID: 2090635
thanks to all of you.
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6863194
This question was awarded, but never cleared due to the JSP-500 errors of that time.  It was "stuck" against userID -1 versus the intended expert whom you awarded.  This corrects the problem and the expert will now receive these points; points verified.

Please click on your Member Profile and select "View Question History" to navigate through any open or locked questions you may have to update and finalize them.  If you are an EE Pro user, you can also choose Power Search to find all your open questions.

This is the Community Support link, if help is needed, along with the link to All Topics which reflects many TAs recently added.

http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
http://www.experts-exchange.com/jsp/zonesAll.jsp
 
Thank you,
Moondancer
Moderator @ Experts Exchange
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question