Menus Not Always Dropping-VBForums
Results 1 to 8 of 8

Thread: Menus Not Always Dropping

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Location
    Texas
    Posts
    18

    Menus Not Always Dropping

    I am experiencing a problem with the menu bar on the application I am developing.

    This application's menu structure is defined completely through the database. The menu bar and sub-menus are created with the Windows API.

    My problem is that sometimes at random, one or more of the menus will not drop down when I run the program in Vista or Windows 7. I don't recall this problem ever happening when I had XP for my development environment.

    I have been looking over the code to build the menu bar, and I have noticed that the API call to CreateMenu does return a negative value for the sub-menus that do not show up on any given run.

    Any thoughts on why CreateMenu will sometimes return a negative?

  2. #2
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Menus Not Always Dropping

    According to the documentation of CreateMenu it should return a non-null value if successful. Negative values should be expected as it returns a Handle to the new empty Menu. So, unless you're also seeing zero being returned that doesn't look like the problem.

    Have you looked at the returns from other API calls ?

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Location
    Texas
    Posts
    18

    Re: Menus Not Always Dropping

    Quote Originally Posted by Doogle View Post
    Have you looked at the returns from other API calls ?
    No, I haven't. I just noticed this pattern after putting a statement to write the handle to the debug window right after the call. The return value for CreateMenu always seems to be negative when a menu is not going to drop, and positive when it will. I've never seen it return 0 since I started looking at this code.

    Anyway, I thought a handle always had to be positive.

  4. #4
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Menus Not Always Dropping

    As far as i understand a handle is just an unsigned Long value but since VB doesn't have that data type you'll see negative values when you debug.print.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Location
    Texas
    Posts
    18

    Re: Menus Not Always Dropping

    DataDrivenMenu.zip

    Maybe it would make more sense if you could see how I'm doing this. I've gone ahead and attached a sample project that I made to build a menu using logic similar to what our actual application uses.

    The program looks at the contents of a MenuItems table in a SQL Server database, so I have attached a SQL script to build a sample of that table.

    This program also lists the collection of parent menu information, along with the handle, onto a ListView. If the handle listed is negative, that menu on the top of the form will most likely not drop down.


    Note that I did not design the original code for building the menu--I inherited it, and it's probably more than a decade old. I realize that there may have been a better way to do this.
    Last edited by Pizzor2000; Dec 4th, 2012 at 08:47 AM. Reason: Fixed attached code.

  6. #6
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Menus Not Always Dropping

    I tried the code attached to your Post.

    There were two things missing; the RECT Structure and the TrackPopupMenu API definition. Having put those into modAPI and changed the ConnectionString to suit my SQL Server it ran perfectly (on multiple executions) with the example Menu data supplied. (I'm running Vista Home Edition SP1, VB6 SP6)

    It reported "6 submenu(s) in database. 0 menu(s) wont drop" at the bottom of the Form and each Menu and Sub-Menu item dropped OK all the time. Interestingly, I didn't get any output to the Immediate Window so there were no negative Handles returned from CreateMenu.

    Not too sure where to go from here. Have you actually tried the demo code you posted ? (obviously not, since it doesn't compile ) Perhaps you should and see if it misbehaves for you or not.

    EDIT: The additions you need in Module modAPI are
    Code:
    Public Type RECT
            Left As Long
            Top As Long
            Right As Long
            Bottom As Long
    End Type
    
    Public Declare Function TrackPopupMenu Lib "user32" _
                    (ByVal hMenu As Long, _
                     ByVal wFlags As Long, _
                     ByVal x As Long, _
                     ByVal y As Long, _
                     ByVal nReserved As Long, _
                     ByVal hwnd As Long, _
                     lprc As RECT) As Long
    Last edited by Doogle; Dec 4th, 2012 at 12:47 AM.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Location
    Texas
    Posts
    18

    Re: Menus Not Always Dropping

    Thank you for checking out my code, Doogle.

    Quote Originally Posted by Doogle View Post
    I tried the code attached to your Post.

    Have you actually tried the demo code you posted ? (obviously not, since it doesn't compile ) Perhaps you should and see if it misbehaves for you or not.
    I actually did. I used TrackPopupMenu to test something out when I first developed this demo application. I removed the API declaration from the code before posting to this board, but I forgot to take out the call to it. Since I was running the program without "full compile," I missed this.

    I fixed the code and re-uploaded it. I edited my previous post to link to the new version.

    It reported "6 submenu(s) in database. 0 menu(s) wont drop" at the bottom of the Form and each Menu and Sub-Menu item dropped OK all the time. Interestingly, I didn't get any output to the Immediate Window so there were no negative Handles returned from CreateMenu.
    I'm actually not surprised you didn't experience any problems. I haven't either running this little demo program by itsself. My actual application uses the same APIs and database table structure, but of course, it is more complicated--having many more components to load, and the table of menu items has over 100 rows. That said, even my full application doesn't usually have problems with the menu on its first few executions from a clean Windows boot.

    However, when I'm coding on the full application, it means I'm starting and stopping it many times, so eventually the menus start malfunctioning--all the menus will appear on the menu bar, but one or more menus (seemingly random) will not drop down when you click it. If it gets to this point, drop-down menus begin failing on the attached demo application, too! This is when you start seeing negative handle values.

  8. #8
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Menus Not Always Dropping

    Quote Originally Posted by Pizzor2000 View Post
    it means I'm starting and stopping it many times.
    I've not had time to read the documentation for the APIs but I wonder whether it's a problem with not closing the Handles when you terminate. I know that I've had problems with other things where I've not close Handles and then re-run. (after 5 or 6 times things 'go wrong' for no apparent reason - Normally the API dcoumentation tells you to close the Handles if it's 'important')

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.