CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Dynamical creation of Buttonmenu

  1. #1
    Join Date
    Apr 2002
    Posts
    388

    Dynamical creation of Buttonmenu

    I have a curious problem:

    I will first post the code and then I will explain you the problem:
    Code:
    Public Sub BuildButtonMenu(Button As MSComctlLib.Button)
    'creates a buttonmenu with all entries of the collection
        Dim anElement As clsElement
        Dim ButtonMenu As MSComctlLib.ButtonMenu
        
        'add the Elements
        For Each anElement In Me
            Set ButtonMenu = Button.ButtonMenus.Add()
            With ButtonMenu
                .text = anElement.Caption
                .Key = anElement.Name '<-- Error
            End With
        Next anElement
    End Sub
    When I want to assign the key element of the Buttonmenu the error "Invalid Key" occurs.
    The first thought was, that there must already be an element with this key in the Buttonmenus collection, but there is not!!

    Could you think, what the problem is?
    mfg Ungi

    Music, music and VB. VB is like music: You never know how it is interpreted.

  2. #2
    Join Date
    Apr 2002
    Posts
    388

    Talking I got it!

    It's described in following MSDN Articel: "Q204054: PRB: Error 35603 "Invalid Key" with TreeView or ListView Control"

    It says that the key must not be numeric!
    mfg Ungi

    Music, music and VB. VB is like music: You never know how it is interpreted.

  3. #3
    Join Date
    May 2003
    Location
    Australia
    Posts
    155
    Yes,

    This is common behaviour for all MS common controls that have collections, treeview nodes, listview items, toolbar buttons and buttonmenus, imagelists , imagecombos ... even the standard VB Collection type works the same way.

    It is because you can refer to an item in the collection by either key or by index position ... so if its a number, it assumes its an index position ... therefore keys cant be numerical by themselves.



    Cheers.
    Zen-Programming:

    If a compiler beeps in the IDE forest, and nobody hears it, was there really a bug?

  4. #4
    Join Date
    Apr 2002
    Posts
    388
    But I think that is not logical.

    "123" is a String, why shouldn't it be the key to an entry of my collection with the index 4711?
    mfg Ungi

    Music, music and VB. VB is like music: You never know how it is interpreted.

  5. #5
    Join Date
    May 2003
    Location
    Australia
    Posts
    155
    I agree Ungi,

    I think that you should be able to pass a number as a string to use as the key ie. "123" not just 123 the number. You would think they would be able to easily tell whether the variable passed to them is a string or a number, and return the value from the collection accordingly.

    Perhaps they (MS) decided against doing it that way to avoid confusion ... maybe originally they didnt think of it, and now have to maintain using this approach for backward compatibility and consistency amongst all their controls with collections.

    You can work around it by adding a prefix to the number - is a pain in the butt, and I have had many times when I wish I could have just used a number for the key, but thats the way it goes!

    I feel your pain and frustration

    Cheers,
    Tinbum747
    Zen-Programming:

    If a compiler beeps in the IDE forest, and nobody hears it, was there really a bug?

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)