• Welcome to Valhalla Legends Archive.
 

Creating/Destroying Menus

Started by Networks, December 27, 2005, 11:02 AM

Previous topic - Next topic

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.

dxoigmn

Try posting some code. Maybe you're doing something wrong. I haven't used VB6 in years so I just may be wrong :P.

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.

Adron

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.

Networks

#4
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?

Adron


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.

dxoigmn

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.

Networks

#7

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.

Adron

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.

Joe[x86]

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
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

Trejo

65025 is a rather unique number to use...why such a large array?

Warrior

Joe likes his programs to eat memory.
Quote from: effect on March 09, 2006, 11:52 PM
Islam is a steaming pile of fucking dog shit. Everything about it is flawed, anybody who believes in it is a terrorist, if you disagree with me, then im sorry your wrong.

Quote from: Rule on May 07, 2006, 01:30 PM
Why don't you stop being American and start acting like a decent human?

Joe[x86]

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: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

Networks

#13
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. :)

rabbit

Or he could just make it a dynamic array instead of a static one...
Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.