Valhalla Legends Forums Archive | Visual Basic Programming | Creating/Destroying Menus

AuthorMessageTime
Networks
Unloading and Loading menus simply doesn't work correctly since it loads and unloads objects from the memory. For instance if I have 3 menus and decide to unload the 2nd one and then add a new (3rd) one, it will say 'Object is already loaded'. This poses a problem when you're trying to dynamically add, and remove new menus. If anyone can provide a way around this, it's greatly appreciated.

I checked google for some API calls but since I don't know them I gave up quickly.
December 27, 2005, 5:02 PM
kamakazie
Try posting some code. Maybe you're doing something wrong. I haven't used VB6 in years so I just may be wrong :P.
December 27, 2005, 5:17 PM
Networks
Make a menu as an array. Now add 3 items. Remove the 2nd item. Try adding a new one. It should tell you that the object already exists.
December 27, 2005, 5:54 PM
Adron
[quote author=Networks link=topic=13667.msg139501#msg139501 date=1135706051]
Make a menu as an array. Now add 3 items. Remove the 2nd item. Try adding a new one. It should tell you that the object already exists.
[/quote]

Works perfectly here.
December 27, 2005, 9:50 PM
Networks
[quote author=Adron link=topic=13667.msg139534#msg139534 date=1135720221]
[quote author=Networks link=topic=13667.msg139501#msg139501 date=1135706051]
Make a menu as an array. Now add 3 items. Remove the 2nd item. Try adding a new one. It should tell you that the object already exists.
[/quote]

Works perfectly here.
[/quote]

You don't get: Control Array element '1' doesn't exist. If not mind showing me how you're doing it?
December 27, 2005, 10:59 PM
Adron
[code]
Private Sub Command1_Click()
  Unload item(1)
End Sub

Private Sub Command2_Click()
  Load item(1)
  item(1).Caption = "new"
End Sub

Private Sub Form_Load()
  Load item(1)
  Load item(2)
End Sub
[/code]

I have item(0) defined on the form from the start.
December 28, 2005, 1:58 AM
kamakazie
[quote author=Networks link=topic=13667.msg139553#msg139553 date=1135724353]
[quote author=Adron link=topic=13667.msg139534#msg139534 date=1135720221]
[quote author=Networks link=topic=13667.msg139501#msg139501 date=1135706051]
Make a menu as an array. Now add 3 items. Remove the 2nd item. Try adding a new one. It should tell you that the object already exists.
[/quote]

Works perfectly here.
[/quote]

You don't get: Control Array element '1' doesn't exist. If not mind showing me how you're doing it?
[/quote]

If you want help, then post your code. No one is going to steal it, and it is probably not anything special anyways.
December 28, 2005, 3:43 AM
Networks
[code]
Private Sub Command1_Click()
    'Remove 2nd one.
    Unload mnuarray(2)
End Sub

Private Sub Form_Load()
    'Load menu items.
    mnuarray(0).Caption = "1"
   
    Load mnuarray(1)
    mnuarray(1).Caption = "2"
   
    Load mnuarray(2)
    mnuarray(2).Caption = "3"
End Sub
[/code]

Press the command1 button twice so it attempts to remove the 2nd menu so only the original one exists. You should get the error I am getting. So in other words how do I sucessfully remove that third one if I don't know it's the third one?

Error: Control array element '2' doesn't exist.

Edit:
Also, the number of menus can be anything and with lots of deleting and adding the actual elements of the menus will get mixed up. So deleting the 2nd menu might actually want to delete the 8th menu.
December 28, 2005, 4:37 PM
Adron
[quote author=Networks link=topic=13667.msg139629#msg139629 date=1135787872]
Press the command1 button twice so it attempts to remove the 2nd menu so only the original one exists. You should get the error I am getting. So in other words how do I sucessfully remove that third one if I don't know it's the third one?
[/quote]

You need to know what menu items you have. Indexes do not change just because you remove one item. Of course you cannot remove the same item twice - that will produce an error message.


[quote author=Networks link=topic=13667.msg139629#msg139629 date=1135787872]
Also, the number of menus can be anything and with lots of deleting and adding the actual elements of the menus will get mixed up. So deleting the 2nd menu might actually want to delete the 8th menu.
[/quote]

Yes. You need to write your code so it knows which one you want to delete.
December 28, 2005, 7:11 PM
JoeTheOdd
[code]Public IndexesUsed(1 to &HFF * &HFF) as Boolean

'// Initialize the array
Public Sub Menu_Init()
    Dim I as Integer: For I = 1 to &HFF * &HFF
        IndexesUsed(I) = False
    Next I
End Sub


'// Create a new menu
'// Return: Index
Public Function MakeNewMenu(Caption as String) As Integer
    Dim I as Integer: For I = 1 to &HFF * &HFF
        If IndexesUsed(I) = False Then
            Load Menu(I)
            Menu(I).Caption = Caption
            MakeNewMenu = I
            IndexesUsed(I) = True
            Exit Function
        End If
    Next I
End Function

Public Sub DistroyMenu(Index as Integer)
    Unload Menu(Index)
End Sub[/code]
December 29, 2005, 8:52 AM
Trejo
65025 is a rather unique number to use...why such a large array?
December 29, 2005, 9:08 AM
Quarantine
Joe likes his programs to eat memory.
December 29, 2005, 9:57 AM
JoeTheOdd
[quote author=Warrior link=topic=13667.msg139736#msg139736 date=1135850236]
Joe likes his programs to eat memory.
[/quote]
Pfft, if hes already using VB, theres no loss.

Really, you can make a guestimate at the highest you'd go to and do it like that, but I didn't feel like thinking of all the possible uses this had.
December 29, 2005, 1:47 PM
Networks
[quote author=Joe link=topic=13667.msg139739#msg139739 date=1135864029]
[quote author=Warrior link=topic=13667.msg139736#msg139736 date=1135850236]
Joe likes his programs to eat memory.
[/quote]
Pfft, if hes already using VB, theres no loss.

Really, you can make a guestimate at the highest you'd go to and do it like that, but I didn't feel like thinking of all the possible uses this had.
[/quote]

Uhhh yeah, eww.

I've come up with my own solution:
Set the menu index that is being deleted the caption of the menu after it (if there is one) and do so with all menus until I've reached the last one and delete that one. (which is just menus.ubound)

Joe you solution eats memory and is inefficient. Mine might be the same but I am yet to find anyone that can find something better. *cough* API Calls?! *cough*

edit:

[code]
Public Sub deleteArrayMenu(ByRef Index As Integer, ByRef MenuArray As Object)
    Dim i As Integer
   
    If (Index <= MenuArray.Ubound) Then
        For i = Index To MenuArray.Ubound - 1
            MenuArray(i).Caption = MenuArray(i + 1).Caption
        Next i
       
        If (MenuArray.Ubound = 0) Then
            MenuArray(MenuArray.Ubound).Caption = vbNullString
        Else
            Unload MenuArray(MenuArray.Ubound)
        End If
    End If
End Sub
[/code]

As you can see Joe, much more efficient. :)
December 29, 2005, 3:11 PM
rabbit
Or he could just make it a dynamic array instead of a static one...
December 31, 2005, 3:33 AM

Search