DestroyControl (Form)

Post a reply

Smilies
:D :) :( :o :-? 8) :lol: :x :P :oops: :cry: :evil: :roll: :wink:

BBCode is ON
[img] is ON
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: DestroyControl (Form)

Re: DestroyControl (Form)

by MoDementia » Tue Jan 13, 2009 1:48 am

raybeau528 wrote:Modementia,

Would it help to just set those controls to visible/invisible as you need them rather than recreating new forms/controls ? Or you could have 1 form and multiple panels where each panel had the pre-configured controls, the panels could overlay each other, and just set the appropriate panel to visible and all others invisible. Just a thought.

Ray
Hi,

I think the original script this relates to is a form that has the selected tracks listed with radio buttons next to them so the form was never the same so this sort of work around was never possible :(
I have used the vis/invis method in Custom Properties Panel it does work quite well
And I'm sure I have another script that calls either 1 or the other form based on criteria :)

Re: DestroyControl (Form)

by Bex » Mon Jan 12, 2009 7:02 pm

Teknojnky wrote:
I think I figured it out after studying bex's example more. But apparently you can not close a modal dialog by setting the object to nothing like you can with a normal form.
That's because X:ing down the form has the modalresult=2. For that reason only I recommend all to always use modalresults in all their forms and give the Cancel button the modalresult=2, then Cancel and X:ing always do the same thing.

Re: DestroyControl (Form)

by raybeau528 » Mon Jan 12, 2009 6:36 pm

Modementia,

Would it help to just set those controls to visible/invisible as you need them rather than recreating new forms/controls ? Or you could have 1 form and multiple panels where each panel had the pre-configured controls, the panels could overlay each other, and just set the appropriate panel to visible and all others invisible. Just a thought.

Ray

Re: DestroyControl (Form)

by Teknojnky » Mon Jan 12, 2009 4:04 pm

MoDementia wrote: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:
To get button onclick events to work, you must use

Code: Select all

    Script.RegisterEvent btnDoIt.Common, "OnClick", "DoIt"
Now I have another problem which I suspect has something to do with showmodal:


I am creating a form with some controls on it, with an OK and (hidden) cancel button.

When I click escape, the cancel event fires but does not close the form. When I use the X on the dialog to close, it fires the cancel event and closes the form.

pseudocode

Code: Select all

Sub Start()
  Set MainForm = SDB.UI.NewForm
    Script.RegisterEvent MainForm, "OnClose", "CloseMe"

  Dim btnClose
  Set btnClose = SDB.UI.NewButton(MainForm)
    btnClose.Cancel = True
    btnClose.Common.SetRect 0,0,0,0
    Script.RegisterEvent btnClose.Common, "OnClick", "CloseMe"

  MainForm.ShowModal
  SDB.Objects("SimpleAppend") = MainForm

End Sub

Sub CloseMe(obj)
  msgbox("Closing")
  Script.UnregisterAllEvents
  Set SDB.Objects("SimpleAppend") = Nothing
End Sub
Escape/cancel does fire the event, but the form remains visible.

If I change to

Code: Select all

  SDB.Objects("SimpleAppend") = MainForm
  SDB.Objects("SimpleAppend").Common.Visible = True
Then the form disappears with escape/cancel.

Is there a problem with form.showmodal or am I not doing something I should be?

edit:

I think I figured it out after studying bex's example more. But apparently you can not close a modal dialog by setting the object to nothing like you can with a normal form.

Re: DestroyControl (Form)

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

Re: DestroyControl (Form)

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.

Re: DestroyControl (Form)

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

Re: DestroyControl (Form)

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:

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

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

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

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

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

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

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?

Top