Page 1 of 2
DestroyControl (Form)
Posted: Sun May 04, 2008 10:54 pm
by MoDementia
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)
Code: Select all
Set Form1 = SDB.Objects("SyncTheSyncForm")
Form1.Common.Visible = False
Form1.Common.DestroyControl
Set SDB.Objects("SyncTheSyncForm") = Nothing
Posted: Mon May 05, 2008 2:36 am
by trixmoto
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...
Code: Select all
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!

Posted: Mon May 05, 2008 2:46 am
by MoDementia
Thanks
Hmm yes it does work but... it looks like a really bad practice
Probably the main reason DestroyControl was introduced

Posted: Mon May 05, 2008 3:11 am
by trixmoto
Yeah, probably!!

Posted: Mon May 05, 2008 8:04 am
by onenonymous
just a thought - you might have to DestroyControl on any controls already on the form before you can DestroyControl the form?
Posted: Mon May 05, 2008 9:12 am
by MoDementia
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
Posted: Mon May 05, 2008 10:41 am
by onenonymous
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:
Code: Select all
Item.Common.Parent.Common.Parent.Common.ChildControl("Browser").Common.DestroyControl
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'.
Hope this helps

Posted: Mon May 05, 2008 8:06 pm
by MoDementia
Hmm it works on the lowest children but I Fails on their parents
Code: Select all
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)
'---> OK
Execute("Form1.Common.ChildControl("&Chr(34)&PnlName&Chr(34)&").Common.ChildControl("&Chr(34)&ButtonName&Chr(34)&").Common.DestroyControl")
Next
SDB.MessageBox "2-"&PnlName&" - "&ButtonName, mtInformation, Array(mbOK)
'----> Fails
Execute("Form1.Common.ChildControl("&Chr(34)&PnlName&Chr(34)&").Common.DestroyControl")
Next
SDB.MessageBox "3-Form Destroy", mtInformation, Array(mbOK)
Form1.Common.DestroyControl
Set Form1 = Nothing
Set SDB.Objects("SyncTheSyncForm") = Nothing
Set SDB.Objects("SyncTheSyncPnlButtonCheckedIDList") = Nothing
End Sub
Posted: Mon May 05, 2008 8:34 pm
by MoDementia
This works but produces and access violation when I exit MM
(Removed the Panel DestroyControl)
Code: Select all
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
Posted: Tue May 06, 2008 7:54 am
by onenonymous
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)?
Posted: Thu May 08, 2008 7:46 pm
by MoDementia
I have the answer, well Jiri helped
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)
Code: Select all
Item.Common.Parent.Common.Parent.Common.ChildControl("Browser").Common.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.
Code: Select all
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
Re: DestroyControl (Form)
Posted: Fri Aug 22, 2008 1:07 am
by MoDementia
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
Back to Jiri

Re: DestroyControl (Form)
Posted: Sun Aug 24, 2008 4:51 am
by Bex
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:
Code: Select all
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
Re: DestroyControl (Form)
Posted: Sun Aug 24, 2008 7:57 am
by MoDementia
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.
Re: DestroyControl (Form)
Posted: Sun Aug 24, 2008 10:16 am
by Bex
If I need to recreate the form, while still in the form, I've found that this piece of code will do the trick.
Code: Select all
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