Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Show a custom popup menu for a TextBox without subclassing

Total Hit ( 2073)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


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:

Click here to copy the following block
Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) _
  As Long

Private 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 If
End 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:

Click here to copy the following block
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 If
End Sub


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.