CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2006
    Posts
    4

    Popup menu problem

    Hi all,
    I found some small problem poping up menu. I created a Form2 with textbox, added to form2 menu, and replaced default menu textbox menu with mine. Then, created same other form2 with everything same plus added command button. Starting form will be second. In to Form2 set for menu load Form1, also and for the command button.
    Now if run project with Form2 then press command button and request menu for textbox. Well' you'll that's fine!!! Now, If I load a form usinf Textbox's menu on Form2 to request Form1. In the textbox on Form1 you won't see a user menu enymore, just default.
    Conclusion: using button command to load form with user menu - OK, but loading same form using textbox's popuping up menu - Nope!
    How do I correct this problem using poping menu?
    I have tried to using both methods of loading popup menu with same result:

    FORM2

    visual basic code:--------------------------------------------------------------------------------
    Private Sub Command1_Click()
    Form1.Show vbModal
    End Sub

    Private Sub First_Click()
    Form1.Show vbModal
    End Sub

    'using subclassing
    Private Sub Form_Load()
    'hereefine a new window procedure for the text box
    prevProc = SetWindowLong(Text2.hWnd, GWL_WNDPROC, AddressOf WindowProc)
    End Sub


    'By disabling the textbox
    Private Sub Text3_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    If Button = vbRightButton Then
    Text3.Enabled = False
    Call Text3_MouseUp(Button, Shift, x, y)
    End If
    End Sub

    Private Sub Text3_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)

    If Button = vbRightButton Then
    Text3.Enabled = True
    PopupMenu mnu
    End If
    End Sub

    --------------------------------------------------------------------------------
    Module

    visual basic code:--------------------------------------------------------------------------------
    Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    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 prevProc As Long

    Public Const GWL_WNDPROC = (-4)
    Const WM_CONTEXTMENU = &H7B

    'this function intercepts all messages (events) sent to the textbox
    'it forwards all messages to the default window procedure
    'except the WM_CONTEXTMENU message
    Public Function WindowProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    If uMsg = WM_CONTEXTMENU Then
    frm.PopupMenu frm.mnu
    Else
    WindowProc = CallWindowProc(prevProc, hWnd, uMsg, wParam, lParam)
    End If
    End Function
    --------------------------------------------------------------------------------
    FORM1:

    visual basic code:--------------------------------------------------------------------------------
    Private Sub Text3_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    If Button = vbRightButton Then
    Text3.Enabled = False
    Call Text3_MouseUp(Button, Shift, x, y)
    End If
    End Sub

    Private Sub Text3_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)

    If Button = vbRightButton Then
    Text3.Enabled = True
    PopupMenu mnuPopup
    End If
    End Sub
    --------------------------------------------------------------------------------
    Last edited by Tomazas; August 17th, 2006 at 03:29 PM.

  2. #2
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: Popup menu problem

    The code in your zip file is nothing like what you are showing here, and I am having a hard time trying to figure out what you are trying to do. Could you re-upload your zip and also maybe a step by step list of what you want to happen and what actually happens.

    ETA: Check the zip. Is this what you want?
    Attached Files Attached Files
    Last edited by sotoasty; August 17th, 2006 at 02:28 PM. Reason: Might have figured out what the problem is.

  3. #3
    Join Date
    Aug 2006
    Posts
    4

    Re: Popup menu problem

    Hi,
    I cut out a bit... Made more simple.
    Open project, run it. Click on button. You'll see Form2. In this form2 right click in textbox, to get user popup menu. You should see menu 'I want to see this menu'. This is how should work. But also need to get same using different way. Close Form2.
    In Form1 right click in tetBox, you'll see menu 'Click here to open Form2'. This will load same form2. Now go to same textbox and do it right click. You'll see Windows standart menu! I want to see same menu as it was in first place: menu text 'I want to see this menu'.
    For me seems to be that first popup menu is still in memory and it is not alowing to popup another menu in Form2.
    How can I correct this?
    Written code contains also SetWindowLong API call which gives me same result as this one. So I removed that part. I tried to use LockWindowUpdate before menu and after. This also did not helped.
    Any idies?
    Attached Files Attached Files

  4. #4
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: Popup menu problem

    That's very interesting. This is what appears to be happening to me. When you click on the textbox on form1 and show the menu, you then go straight into the context menu processing. Here you show form2 as modal and you are still in the menu processing until form2 closes. This seems to be preventing the context menu in form2 from showing. You can use the classes I posted here in the updated project to do your menu processing. This seems to work fine on my system.

    Hope this is what you want.
    Attached Files Attached Files

  5. #5
    Join Date
    Aug 2006
    Posts
    4

    Thumbs up Re: Popup menu problem

    Thanks for the replay, It seems to be working fine.
    It just correction I added to form1:

    Private Sub Text3_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = vbRightButton Then
    Dim MPF2 As New PopupMenu
    MPF2.MenuItems.Add "1", "Show Form 2"
    MPF2.MenuItems.Add "2", "Cancel"
    Dim A As String
    A = MPF2.ShowMenu
    If A = "1" Then
    Form2.Show vbModal
    ElseIf A <> 0 Then
    MsgBox MPF2.MenuItems(A).Caption & " was selected"
    End If
    Set MPF2 = Nothing
    Text3.Enabled = True
    End If
    End Sub

    Otherways you get an error when popup menu requested but nothing selected. I'll try to implement this to my application.
    Thanks

  6. #6
    Join Date
    Aug 2006
    Posts
    4

    Re: Popup menu problem

    Hi,
    I have implemented succesfully your code into my application. My application contains Images in the Popup menus on form. Your way to get around form menu removed imaging form textbox's poping up menu, because it is created after click. Could you help me with that?
    I want just to see image in my poping up menu in the textbox also as in the form menu. I did not wrote this code for imaging poping up menus, so its hard for me to find where should I change your source code to get imaging in textbox menu.
    Thanks
    Attached Files Attached Files

  7. #7
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: Popup menu problem

    You will have to give me a bit of time. I might be able to check it out this weekend, but it is not possible today.

    If you have to have your graphics on the popup menu, the other option you could try is with timers.


    See Attached Zip
    Attached Files Attached Files

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured