DestroyControl (Form)

This forum is for questions / discussions regarding development of addons / tweaks for MediaMonkey.

Moderator: Gurus

MoDementia
Posts: 1321
Joined: Thu Jun 15, 2006 3:26 pm
Location: Geelong, Victoria, Australia

DestroyControl (Form)

Post by MoDementia » Sun May 04, 2008 10:54 pm

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

trixmoto
Posts: 10024
Joined: Fri Aug 26, 2005 3:28 am
Location: Hull, UK
Contact:

Post by trixmoto » Mon May 05, 2008 2:36 am

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! :)
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
Posts: 1321
Joined: Thu Jun 15, 2006 3:26 pm
Location: Geelong, Victoria, Australia

Post by MoDementia » Mon May 05, 2008 2:46 am

Thanks

Hmm yes it does work but... it looks like a really bad practice :(

Probably the main reason DestroyControl was introduced :P

trixmoto
Posts: 10024
Joined: Fri Aug 26, 2005 3:28 am
Location: Hull, UK
Contact:

Post by trixmoto » Mon May 05, 2008 3:11 am

Yeah, probably!! :D
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.

onenonymous
Posts: 374
Joined: Sat Feb 02, 2008 1:09 pm
Location: Texas

Post by onenonymous » Mon May 05, 2008 8:04 am

just a thought - you might have to DestroyControl on any controls already on the form before you can DestroyControl the form?

MoDementia
Posts: 1321
Joined: Thu Jun 15, 2006 3:26 pm
Location: Geelong, Victoria, Australia

Post by MoDementia » Mon May 05, 2008 9:12 am

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

onenonymous
Posts: 374
Joined: Sat Feb 02, 2008 1:09 pm
Location: Texas

Post by onenonymous » Mon May 05, 2008 10:41 am

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 :-?

MoDementia
Posts: 1321
Joined: Thu Jun 15, 2006 3:26 pm
Location: Geelong, Victoria, Australia

Post by MoDementia » Mon May 05, 2008 8:06 pm

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

MoDementia
Posts: 1321
Joined: Thu Jun 15, 2006 3:26 pm
Location: Geelong, Victoria, Australia

Post by MoDementia » Mon May 05, 2008 8:34 pm

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

onenonymous
Posts: 374
Joined: Sat Feb 02, 2008 1:09 pm
Location: Texas

Post by onenonymous » Tue May 06, 2008 7:54 am

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)?

MoDementia
Posts: 1321
Joined: Thu Jun 15, 2006 3:26 pm
Location: Geelong, Victoria, Australia

Post by MoDementia » Thu May 08, 2008 7:46 pm

I have the answer, well Jiri helped :P
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

MoDementia
Posts: 1321
Joined: Thu Jun 15, 2006 3:26 pm
Location: Geelong, Victoria, Australia

Re: DestroyControl (Form)

Post by MoDementia » Fri Aug 22, 2008 1:07 am

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 :( :x :cry:

Bex
Posts: 6316
Joined: Fri May 21, 2004 5:44 am
Location: Sweden

Re: DestroyControl (Form)

Post by Bex » Sun Aug 24, 2008 4:51 am

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
Advanced Duplicate Find & Fix Find More From Same - Custom Search. | Transfer PlayStat & Copy-Paste Tags/AlbumArt between any tracks.
Tagging Inconsistencies Do you think you have your tags in order? Think again...
Play History & Stats Node Like having your Last-FM account stored locally, but more advanced.
Case & Leading Zero Fixer Works on filenames too!

All My Scripts

MoDementia
Posts: 1321
Joined: Thu Jun 15, 2006 3:26 pm
Location: Geelong, Victoria, Australia

Re: DestroyControl (Form)

Post by MoDementia » Sun Aug 24, 2008 7:57 am

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.

Bex
Posts: 6316
Joined: Fri May 21, 2004 5:44 am
Location: Sweden

Re: DestroyControl (Form)

Post by Bex » Sun Aug 24, 2008 10:16 am

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
Advanced Duplicate Find & Fix Find More From Same - Custom Search. | Transfer PlayStat & Copy-Paste Tags/AlbumArt between any tracks.
Tagging Inconsistencies Do you think you have your tags in order? Think again...
Play History & Stats Node Like having your Last-FM account stored locally, but more advanced.
Case & Leading Zero Fixer Works on filenames too!

All My Scripts

Post Reply