Results 1 to 9 of 9

Thread: Menu Items in the Main Menu [RESOLVED]

  1. #1

    Thread Starter
    Member Jared's Avatar
    Join Date
    Nov 2002
    Posts
    58

    Menu Items in the Main Menu [RESOLVED]

    How can I write code to uncheck ALL of the checked menu items that are in a Main Menu?

    Here is the code that I tried: (which doesn't work)

    Private Sub MenuItem8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem8.Click
    cpnum = 3
    Dim i As Integer
    Dim mnu As MainMenu = Me.MainMenu1
    For i = 1 To 20
    MainMenu1.MenuItems.Item(i).Checked = False
    Next
    MenuItem8.Checked = True
    End Sub

    Thanks,
    Jared.
    Last edited by Jared; Dec 22nd, 2002 at 09:35 AM.

  2. #2
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    try with a foreach instead...also what did u get with that code that u wrote?
    \m/\m/

  3. #3

    Thread Starter
    Member Jared's Avatar
    Join Date
    Nov 2002
    Posts
    58
    An error stating that "This is only valid for MenuItems that have no children and are not top-level."

    Also the above code should have been:

    Private Sub MenuItem7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem7.Click

    Dim i As Integer
    Dim mnu As MainMenu = Me.MainMenu1
    For i = 1 To 20
    mnu.MenuItems.Item(i).Checked = False
    Next
    MenuItem7.Checked = True
    End Sub

    But it did not change anything.

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    try this

    VB Code:
    1. Dim mnu As New MainMenu()

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    A mainmenu kind of works like a treeview where children of a parent can have children and on and on. So you have to have a recursive function to check all the generations.

    VB Code:
    1. 'syntax of use
    2.     UncheckAll(Me.MainMenu1.MenuItems)
    3.  
    4.     Public Sub UncheckAll(ByVal mnu As MainMenu.MenuItemCollection)
    5.         Dim mi As MenuItem
    6.         'checks each item in this collection
    7.         For Each mi In mnu
    8.             If mi.Checked Then mi.Checked = False
    9.             'checks any children of this item
    10.             'this will always be called to make sure it checks all generations
    11.             UncheckAll(mi.MenuItems)
    12.         Next
    13.     End Sub

  6. #6

    Thread Starter
    Member Jared's Avatar
    Join Date
    Nov 2002
    Posts
    58
    Thanks Edneeis, that worked like a charm. I knew there was an easier way to uncheck all the menu items, just couldn't get it to work correctly. I had an extremely long list of redundant code to accomplish this task (but now I can delete it! ).

    - Jared.

  7. #7
    Hyperactive Member stingrae's Avatar
    Join Date
    Apr 2002
    Location
    Sydney
    Posts
    401
    (before reading this post, i must admit that i've had a huge weekend on the piss, and am not feeling the best for a monday morning)


    edneeis, is it possible to have
    Code:
    UncheckAll(mi.MenuItems)
    called from inside itself? looking at it, to me it seems circular.
    "The passion lives to keep your faith, though all are different, all are great" ... Michael Hutchence 1960-1997.

    Windows & Web Developer
    Specialising in Visual Basic .Net & Client Server Programming & Client/Customer Relations Databases
    Sutherland Shire, Sydney Australia
    www.stingrae.com.au
    Developer of Arnold - Gym & Martial Arts Database Management System
    www.gymdatabase.com.au

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Pardon? I don't think I understand the question.

    Are you asking if UncheckAll can be called from within itself? If so then yes you can do that.

    As far as circular, not exactly but it is recursive. It doesn't call itself with the same parameters repeatibly. It calls itself on the collection of the current item in the current collection it is checking. This is intentional so that all of the nodes and their children get checked.

  9. #9
    Hyperactive Member stingrae's Avatar
    Join Date
    Apr 2002
    Location
    Sydney
    Posts
    401
    Edneeis,

    yes, I was unsure if a sub-routine could call itself. when i first looked at it, i thought that it would get stuck in a loop and only stop when the call stack memory got too large. now i'm awake and focuessed, it makes sense.

    thanks
    "The passion lives to keep your faith, though all are different, all are great" ... Michael Hutchence 1960-1997.

    Windows & Web Developer
    Specialising in Visual Basic .Net & Client Server Programming & Client/Customer Relations Databases
    Sutherland Shire, Sydney Australia
    www.stingrae.com.au
    Developer of Arnold - Gym & Martial Arts Database Management System
    www.gymdatabase.com.au

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