I'm writing a script dialog and I would like to check whether a control is enabled or disabled. The help file states that dlg.control("control name").enabled should return a bool if it's enabled/disabled.
I've tried the following:
If (dlg.control("control name").enabled) Then
msgbox "control is enabled"
Else
msgbox "control is disabled"
End If
I've also tried other variations like If dlg.control("control name").enabled = True Then... which also causes the script to fail with "A method was called unexpectedly".
I'm not sure if I'm missing something obvious, but I can't seem to figure it out. Any help would be appreciated.
option explicit
' TestDlg
' (c) 2020 Leo Davidson
' This is a script for Directory Opus.
' See https://www.gpsoft.com.au/DScripts/redirect.asp?page=scripts for development information.
' Called by Directory Opus to initialize the script
Function OnInit(initData)
initData.name = "TestDlg"
initData.version = "1.0"
initData.copyright = "(c) 2020 Leo Davidson"
' initData.url = "https://resource.dopus.com/c/buttons-scripts/16"
initData.desc = ""
initData.default_enable = True
initData.min_version = "12.0"
End Function
' Called to add commands to Opus
Function OnAddCommands(addCmdData)
Dim cmd
Set cmd = addCmdData.AddCommand
cmd.name = "TestDlg"
cmd.method = "OnTestDlg"
cmd.desc = ""
cmd.label = "TestDlg"
cmd.template = ""
cmd.hide = False
cmd.icon = "script"
End Function
' Implement the TestDlg command
Function OnTestDlg(scriptCmdData)
Dim dlg
Dim msg
Set Dlg = scriptCmdData.func.Dlg()
dlg.title = "Test" ' Window title.
dlg.template = "dialog1" ' Dialog name from associated Resources.
dlg.detach = True
DOpus.Output "Test Dialog started"
dlg.Create
' Do any runtime control setup here before showing the window.
dlg.Show
Do While True
Set msg = dlg.GetMsg()
If (NOT msg.result) Then
Exit Do
End If
If (msg.event = "click" and msg.control = "button1") Then
DOpus.Output "Button 1 Clicked"
End If
If (msg.event = "click" and msg.control = "button2") Then
DOpus.Output "Button 2 Clicked"
End If
If (msg.event = "click") Then
If (dlg.Control("button1").enabled) Then
DOpus.Output "Button 1 was enabled, now disabled"
dlg.Control("button1").enabled = False
Else
DOpus.Output "Button 1 was disabled, now enabled"
dlg.Control("button1").enabled = True
End If
If (dlg.Control("button2").enabled) Then
DOpus.Output "Button 2 was enabled, now disabled"
dlg.Control("button2").enabled = False
Else
DOpus.Output "Button 2 was disabled, now enabled"
dlg.Control("button2").enabled = True
End If
End If
Loop
DOpus.Output "Test Dialog finished"
End Function
==SCRIPT RESOURCES
<resources>
<resource name="dialog1" type="dialog">
<dialog fontsize="8" height="114" lang="" resize="yes" standard_buttons="ok,cancel" width="186">
<control enable="no" height="14" name="button1" title="Button 1" type="button" width="50" x="6" y="6" />
<control height="14" name="button2" title="Button 2" type="button" width="50" x="6" y="24" />
</dialog>
</resource>
</resources>
Thanks for the code - it's helpful. The strange thing is that I'm running similar lines of code to check for the enabled state but they're failing in my script. My script is too long to post, but I'm guessing it could be because I'm trying to check for the enabled state after the msg loop ends (when the user clicks the "OK" button and the script processes the user input). Is it still possible to check for the enabled state of a control if it's outside of the msg loop?
Once the dialog closes, the actual controls (as in the Win32 buttons on the screen) no longer exist.
The dialog scripting object still exists to let you query things like the result field, and I think you may be able to query the scripting dialog.control(...) objects (e.g. to get what was typed into an edit field). But anything which would depend on querying the actual on-screen controls can't work, because they no longer exist.
You'd normally handle doing checks like that when your OK button is pushed, or through some other event (e.g. "close") before the dialog loop ends. (You can hide the dialog at that time, but need to query the enabled/disabled state of things before it has actually closed.)