~~Title: Interacting with Dialog Controls ~~
When a dialog is [[.the_dialog_message_loop:detached_dialogs|detached]] (by setting the **[[:reference:scripting_reference:scripting_objects:dialog|Dialog]].detach** property to **True**) it becomes possible for your script to interact with the controls on the dialog to a much greater extent. You can read their values at any time, not just after the dialog has closed, and you can also modify their values (both after the dialog has been created and in response to control input). The various methods and properties of theĀ **[[:reference:scripting_reference:scripting_objects:control|Control]]** object let you read and modify control values while a detached dialog is open.
The following (admittedly artificial!) example demonstrates the script interacting with the controls on a detached dialog.
{{:media:image137.png?nolink|}}
Clicking one of the five radio buttons performs the following actions:
* The matching static text (//Option one//, etc.) will be enabled.
* The static texts corresponding to the other radio buttons will be disabled.
* The number field in the bottom-right corner will update to reflect the selected radio button.
* The static text at the bottom of the dialog will also update to reflect the selection.
Additionally, you can edit the number field in the bottom-right which will cause the appropriate radio button to be selected.
==Code==
Function OnClick(ByRef clickData)
Set Dlg = DOpus.Dlg
Dlg.window = clickData.func.sourcetab
Dlg.template = "testdlg"
Dlg.detach = True
Dlg.Show
' Message loop.
Do
Set Msg = Dlg.GetMsg()
If Msg.event = "click" And Left(Msg.control, 5) = "radio" Then
' Click message from one of the radio buttons.
Call RadioSelected(Dlg, Msg)
ElseIf Msg.event = "editchange" and Msg.control = "val" Then
' EditChange message from the "val" number input.
Call EditChanged(Dlg, Msg)
End If
Loop While Msg
End Function
Sub RadioSelected(ByRef Dlg, ByRef Msg)
If Msg.data Then ' checked?
' Loop through the radio controls.
' They're called "radio1" through "radio5".
' The static controls are named similarly.
For i = 1 to 5
' Was this radio button clicked?
If Msg.control = "radio" & i Then
' Enable the radio's matching static control.
Dlg.Control("static" & i).enabled = True
' Update the text in the static at the bottom of the dialog.
Dlg.Control("seltext").label = "You selected option #" & i
' If (and ONLY if) we had focus, update the number field.
' The focus test is important because it means this event came
' from an actual click rather than from us calling SetCheck below.
If Msg.focus Then Dlg.Control("val").value = i
Else
' This wasn't the clicked radio, so disable its matching static control.
Dlg.Control("static" & i).enabled = False
End If
Next
End If
End Sub
Sub EditChanged(ByRef Dlg, ByRef Msg)
' We only want to process this when the control has focus, as
' otherwise the change is one we made ourselves rather than the user.
If Msg.focus and Msg.value >= 1 and Msg.Value <= 5 Then
' Check the appropriate radio button.
Dlg.Control("radio" & Msg.Value).value = True
End If
End Sub
==Resources==