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.
Try posting some code. Maybe you're doing something wrong. I haven't used VB6 in years so I just may be wrong :P.
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 from: Networks on December 27, 2005, 11:54 AM
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.
Works perfectly here.
Quote from: Adron on December 27, 2005, 03:50 PM
Quote from: Networks on December 27, 2005, 11:54 AM
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.
Works perfectly here.
You don't get: Control Array element '1' doesn't exist. If not mind showing me how you're doing it?
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
I have item(0) defined on the form from the start.
Quote from: Networks on December 27, 2005, 04:59 PM
Quote from: Adron on December 27, 2005, 03:50 PM
Quote from: Networks on December 27, 2005, 11:54 AM
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.
Works perfectly here.
You don't get: Control Array element '1' doesn't exist. If not mind showing me how you're doing it?
If you want help, then post your code. No one is going to steal it, and it is probably not anything special anyways.
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
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.
Quote from: Networks on December 28, 2005, 10:37 AM
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?
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 from: Networks on December 28, 2005, 10:37 AM
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.
Yes. You need to write your code so it knows which one you want to delete.
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
65025 is a rather unique number to use...why such a large array?
Joe likes his programs to eat memory.
Quote from: Warrior on December 29, 2005, 03:57 AM
Joe likes his programs to eat memory.
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 from: Joe on December 29, 2005, 07:47 AM
Quote from: Warrior on December 29, 2005, 03:57 AM
Joe likes his programs to eat memory.
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.
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:
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
As you can see Joe, much more efficient. :)
Or he could just make it a dynamic array instead of a static one...