In the subroutines for form close, and button cancel, OK, I have the following code because the form is different each time it is created.
Depending on where I put the code it throws an "invalid pointer operation" or a "memory access violation error" when I close MM
The form works perfectly while in MM (created 5-10 times)
Firstly you should check that the form is there, although I'm sure you code means that it always is, you should still check. I've never used the "DestroyControl" myself, previously when I've had to destroy forms (like in my BatchArtFinder) I've done it like this...
Set Form1 = SDB.Objects("SyncTheSyncForm")
If Not (Form1 Is Nothing) Then
Form1.Common.Visible = False
Form1.Common.ControlName = ""
Set SDB.Objects("SyncTheSyncForm") = Nothing
End If
This seems to work for me anyway!
Download my scripts at my own MediaMonkey fansite.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.
MoDementia wrote:Yeah I tried that , it locks the parent control
I started with the buttons and I think it errored on the panels before I even go t to the form
Hmmm... I use DestroyControl in my Right Click For Web script to destroy a panel that's in a form. This works for me on both a Persistent Panel as well as a Form where I have a panel that has an embedded web browser. My code is:
Where "item" is the button on another panel that calls the procedure to destroy the control and "Browser" is the '.Common.ControlName' of the Panel I want to destroy. (So "Browser" is a child of 'Item' grandparent.) I think mine works cause I'm destroying the 'controlname' not the 'objectname'.
Sub DestroyControls
Dim xCounter, PnlName, ButtonName
Set Form1 = SDB.Objects("SyncTheSyncForm")
Form1.Common.Visible = False
Set PnlButtonCheckedIDList = SDB.Objects("SyncTheSyncPnlButtonCheckedIDList")
For xCounter = 0 to PnlButtonCheckedIDList.Count - 1
tmpArray = Split(PnlButtonCheckedIDList.Item(xCounter),"/",-1,1)
PnlName = tmpArray(0)
For yCounter = 1 to Ubound(tmpArray)
tmp2Array = Split(tmpArray(yCounter),",",-1,1)
ButtonName = tmp2Array(0)
SDB.MessageBox "1-"&PnlName&" - "&ButtonName, mtInformation, Array(mbOK)
Execute("Form1.Common.ChildControl("&Chr(34)&PnlName&Chr(34)&").Common.ChildControl("&Chr(34)&ButtonName&Chr(34)&").Common.DestroyControl")
Next
Next
SDB.MessageBox "3-Form Destroy", mtInformation, Array(mbOK)
Form1.Common.DestroyControl
Set SDB.Objects("SyncTheSyncForm") = Nothing
Set SDB.Objects("SyncTheSyncPnlButtonCheckedIDList") = Nothing
End Sub
MoDementia wrote:This works but produces and access violation when I exit MM
Hmm - I've been getting my share of access violations on exit also. I'd assumed it was cause I'm working on script editing and don't have all my "Set XXX = Nothing" lines in place yet - perhaps it is the use of DestroyControl. I wonder if anyone else is using DestroyControl in their script successfully (ie without MM crash on exit)?
Jiri wrote:
DestroyControl should only be used in special cases, the only one example I know it in http://mediamonkey.com/wiki/index.php/S ... rch_script. I understand that it's a little confusing and will try to simplify the control creating/destruction workflow for future MM versions...
Anyway, I still believe that in your case it should work to just set Nothing to the object.
So onenonymous use on a browser is correct (The only use for DestroyControl)
All other requirements should use trixmoto's method. I'm pretty sure I just set it to nothing without setting the ControlName to "" at one stage and it failed because the name was in use so Jiri's reply may not be 100% accurate and I know Trixmoto's works.
Set Form1 = SDB.Objects("SyncTheSyncForm")
If Not (Form1 Is Nothing) Then
Script.UnregisterEvents Form1
Form1.Common.Visible = False
Form1.Common.ControlName = ""
Set SDB.Objects("SyncTheSyncForm") = Nothing
End If
[EDIT] Added Script.UnregisterEvents Form1 to make it more correct
Form1.Common.ControlName = "" should not need to be used so will be fixed in version 3.1
This only works if you use OnClickFunc for buttons Onclick Events produce errors
as there is no OnClickFunc for OnClose Form using the "X" produces errors
I always assign a ModalResult to my <OK> and <Cancel> buttons in a Form.
Then I use Form.ShowModal to visualize and close the form.
It's very important to set the <Cancel> button to ModalResult=2 since X:ing down the form has the ModalResult of 2.
Something like this:
Sub CreateTestForm
Set Form = UI.NewForm
Form.Common.SetRect 0, 0, 410, 240
Form.FormPosition = 4 ' Screen Center
Form.BorderStyle = 3 ' Dialog
'Add some stuff to the form
Set Btn = UI.NewButton( Form)
Btn.Caption = SDB.Localize("&OK")
Btn.Common.SetRect 110,170,75,25
Btn.ModalResult = 1
Btn.Default = true
Set Btn = UI.NewButton( Form)
Btn.Caption = SDB.Localize("&Cancel")
Btn.Common.SetRect 220,170,75,25
Btn.ModalResult = 2 '<--- Cancel must have the value 2
Btn.Cancel = true
If Form.ShowModal=1 then '<-- The code stops here and waits for user input. It also makes the form visible
'OK pressed. Do your stuff here, like calling subs and saving values etc
End If
'OK, Cancel or X is pressed here
'If you have registred events you must unregistred them here
'Otherwise you'll get AV-errors
End Sub
Thats interesting no onclicks anywhere.
I will give it a go soon.
I hope it works as I seem to be the only one who needs to destroy and recreate forms in scripts.
i.e.
sometimes it has 3 checkboxes, 2 dropdowns and 4 text boxes
and the next time it is opened it needs
5 checkboxes, 3 dropdowns and 1 text box, 1 spin edit and a radio button set.
If Form.ShowModal=1 then '<-- The code stops here and waits for user input. It also makes the form visible
If sometest = False Then
res=SDB.MessageBox("The action is not allowed. Do you want to start over?",mtWarning, Array(mbYes,mbNo))
If res=mbYes Then
Call(CreateTestForm) '<-- Creates the form again. I.e. a new form is created
Exit Sub '<--- destroys the old form
End If
If res=mbNo Then
Exit Sub '<--- destroys the old form
End If
End If
'OK pressed. Do your stuff here, like calling subs and saving values etc
End If