Show a custom popup menu for a TextBox without subclassing

Elsewhere in the TipBank we show how you can display a custom popup menu on a TextBox control by subclassing the WM_CONTEXTMENU message that Windows sends the control when the user right-clicks on it. If you don’t like to resort to subclassing for such an easy job, you can use the following tip, taken from Microsoft Knowledge Base:

Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) _    As LongPrivate Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, _    Y As Single)    If Button = vbRightButton Then        ' don't let the control display as "grayed"        LockWindowUpdate Text1.hWnd        ' disable the textbox, so that it can't react to mouse click        Text1.Enabled = False        ' show your custom menu               PopupMenu mnuFile        ' re-enable the control        Text1.Enabled = True        ' stop freezing the update        LockWindowUpdate 0&    End IfEnd Sub

Even better, it turns out that you don’t even need the LockWindowUpdate API function to avoid the TextBox control enter the “grayed” state, provided that you re-arrange the above statements and ensure that the TextBox control misses the opportunity to display the default Edit menu:

Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, _    Y As Single)    If Button = vbRightButton Then        ' disable the textbox        Text1.Enabled = False        ' (this DoEvents seems to be optional)        DoEvents        ' re-enable the control, so that it doesn't appear as grayed        Text1.Enabled = True        ' show your custom menu               PopupMenu mnuFile    End IfEnd Sub

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS