### Create Modal Dialog by Code

Example creating a modal form by code.
This is a dialog to replce text in a textarea.
The form created has two textfields and the buttons OK and Cancel.
[TextField Target]
[TextField Replace]
[Button OK] [Button Cancel]
Define the global nodes which are created in sub Button_ReplaceAll_Action
`Private Sub Class_Globals
Private Form_Replace As Form
Private TextField_ReplaceAll_Target As TextArea
Private TextField_ReplaceAll_Replace As TextArea
Define the sub to create & show the dialog
`Private Sub Button_ReplaceAll_Action
Dim FormWidth As Double = 200
Dim FormHeight As Double = 170
If TextArea_Editor_IsEmpty Then Return
Form_Replace.Initialize("Form_Replace", FormWidth, FormHeight)
Form_Replace.SetFormStyle("UTILITY")
Form_Replace.Resizable = False
Form_Replace.Title = "Replace All"
Dim TextField_ReplaceAll_Target As TextArea
TextField_ReplaceAll_Target.Initialize("TextField_ReplaceAll_Target")
TextField_ReplaceAll_Target.PromptText = "Target"
Form_Replace.RootPane.AddNode(TextField_ReplaceAll_Target, 10, 10, FormWidth - 20, 30)
Dim TextField_ReplaceAll_Replace As TextArea
TextField_ReplaceAll_Replace.Initialize("TextField_ReplaceAll_Replace")
TextField_ReplaceAll_Replace.PromptText = "Replace"
Form_Replace.RootPane.AddNode(TextField_ReplaceAll_Replace, 10, 60, FormWidth - 20, 30)
Dim Button_Form_Replace_OK As Button
Button_Form_Replace_OK.Initialize("Button_Form_Replace_OK")
Button_Form_Replace_OK.Text = "Replace"
Form_Replace.RootPane.AddNode(Button_Form_Replace_OK, 10, 120, 80, 30)
Dim Button_Form_Replace_Cancel As Button
Button_Form_Replace_Cancel.Initialize("Button_Form_Replace_Cancel")
Button_Form_Replace_Cancel.Text = "Cancel"
Form_Replace.RootPane.AddNode(Button_Form_Replace_Cancel, FormWidth - 10 - 80, 120, 80, 30)
Form_Replace.ShowAndWait
End Sub`
' The event for the OK button
`Private Sub Button_Form_Replace_OK_Action
Log($"Replace all ${TextField_ReplaceAll_Target.Text} by ${TextField_ReplaceAll_Replace.Text}."$)
Dim Target As String = TextField_ReplaceAll_Target.Text
If Target.Length = 0 Then
Label_StatusInfo.Text = $"Replace all aborted as no target entered."$
Form_Replace.Close
Return
End If
Dim Replace As String = TextField_ReplaceAll_Replace.Text
If Replace = Null Then Replace = ""
TextArea_Editor.Text = TextArea_Editor.Text.Replace(Target, Replace)
Label_StatusInfo.Text = $"Text replaced "${Target}" by "${Replace}"."$
Form_Replace.Close
End Sub`
' The event for the cancel button
`Private Sub Button_Form_Replace_Cancel_Action
Form_Replace.Close
End Sub`
`

Changed: , Category: Forms, File: T6C195M1412.htm, ID: 1412