Showing a Form

Showing a form is very straightforward, once you have brought in the necessary assembly, and the Form type itself.

-- form1.wlua
require 'CLRPackage'
import("System.Windows.Forms")
form = Form()
form.Text = "Hello, World!"
form:ShowDialog()

Since this is a GUI script, I have given it a .wlua extension. The convention is that .lua is associated with the console version lua.exe, and .wlua is associated with the GUI version wlua.exe. Then, if you run this script from Windows Explorer you will not get the dreaded black console window appearing as well. (Also, in Lua for Windows .wlua scripts can be properly run and debugged from SciTE.)

Since explicitly bringing in all the required types can be tedious, I will now switch to using CLRPackage/import for the next example:

-- form2.wlua
require 'CLRPackage'
import "System.Windows.Forms"
import "System.Drawing"
 
form = Form()
form.Text = "Hello, World!"
button = Button()
button.Text = "Click Me!"
button.Location = Point(20,20)
button.Click:Add(function()
    MessageBox.Show("We wuz clicked!",arg[0],MessageBoxButtons.OK)
end)
 
form.Controls:Add(button)
form:ShowDialog()

Windows.Forms applications are conceptually very simple; one creates controls of various types, sets their properties, and adds them to the control array of the form, which then can be shown. One attaches functions to the events of the objects to get feedback from the user or the system.

Unlike IUP, controls are usually given an explicit position. However, controls can be docked to the sides of their container:

-- form3.wlua
require 'CLRPackage'
import "System.Windows.Forms"
import "System.Drawing"
 
button = Button()
button.Text = "Click!"
button.Dock = DockStyle.Top
edit = RichTextBox()
edit.Dock = DockStyle.Fill
 
form = Form()
form.Text = "Hello, World!"
form.Controls:Add(edit)
form.Controls:Add(button)
form:ShowDialog()

In this example, a text box is made to fill all of the form, and then a button is added which is to be docked to the top side of the form. (The creation order of the text box and the button is not important, but they do have to be added in this order.)