Show a custom popup menu for a TextBox without subclassing

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:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as