More than one window
A program often has a main window and a tool window that stays open beside it. This lesson opens a second window with Show and an owner, keeps only one copy of it, lets the two windows talk, counts the open windows and closes the tool with its owner.
What you will learn#
- the difference between a modal window (
ShowDialog) and a window that stays open (Show), Show(owner): the tool window belongs to the main window,- opening one copy and bringing it to the front (
Activate), - two windows that talk: a function that changes the controls of the other,
Application.OpenForms,- why an event must not destroy its own window object.
The tutorial Your own dialog used a modal dialog: the main window waits until it is closed. A tool window is modeless: both windows work.
The program#
# more than one window - a tool window next to the main window: Show with an owner, only one copy, talking between windows, closing together
load "dotnet.ring"
using {
"System.Drawing"
"System.Windows.Forms"
}
# the windows and the state are global variables
oForm = "" # the main window (uiTest closes the window in the variable oForm)
oShown = "" # the label of the main window that the tool window changes
oTool = "" # the tool window (the last one that was made)
lToolOpen = False # is the tool window open now?
oToolBox = ""
oToolStatus = ""
nToolOpened = 0 # how many times a tool window was made
cApplied = ""
func main
oForm = uiForm("Main window",420,200)
oShown = uiLabel(oForm,"Nothing applied yet",20,20,370,28)
uiButton(oForm,"Open the tool window",20,60,170,32,"openTool()")
oForm.setFormClosedEvent("cApplied += '[main closed]'")
uiRun(oForm)
# Open the tool window. A program should not open a second copy: if it is open, the function brings it to the front.
func openTool
if lToolOpen
oTool.Activate()
return
ok
nToolOpened++
lToolOpen = True
oTool = uiForm("Tool window",300,150)
oTool.StartPosition = "Manual"
oTool.Location = new Point(oForm.Left + oForm.Width + 10,oForm.Top)
oToolBox = uiTextBox(oTool,20,20,250,24)
uiButton(oTool,"Apply",20,56,90,30,"applyTool()")
oToolStatus = uiLabel(oTool,"",20,98,250,22)
oTool.setFormClosedEvent("toolClosed()")
# Show(owner): the window is NOT modal (the main window can still be used), but it belongs to the owner: it stays in front of it,
# is minimized with it, and is closed with it.
oTool.Show(oForm)
# the tool window changes the main window: one window calls a function that works with the controls of the other
func applyTool
oShown.Text = "Applied: " + oToolBox.Text
oToolStatus.Text = "Sent to the main window"
# The event code runs inside a method of the tool window, so it must not destroy the window object (assigning another value to oTool
# would): it only sets a flag, and the next openTool makes a new window and replaces the old object.
func toolClosed
cApplied += "[tool closed]"
lToolOpen = False
# the number of the open windows of the program
func openForms
return new Application().OpenForms.CountHow it works#
Show with an owner#
oTool.Show(oForm) opens the tool window and returns at once, so the main window can still be used. The argument is the owner: an owned window always stays in front of its owner, is minimized and restored with it, and is closed with it. Without an owner (Show(NULL)) the two windows are separate. The test reads oTool.Owner.Text and gets the title of the main window.
One copy#
Clicking Open the tool window twice must not make two tool windows. openTool first looks at lToolOpen: when the window is open it only calls Activate(), which brings it to the front, and returns. nToolOpened counts how many windows were really made; the test shows 1 after two clicks.
The checks of this program are in a separate file, selftest/ch10_windows.ring:
# the checks of ch10_windows.ring: run with ring ch10_windows.ring --selftest (the program loads this file and runs selfTest() once its window is shown)
func selfTest
dnAssert("one window at the start",openForms(),1)
# open the tool window: two windows, and the tool has the main window as owner
openTool()
dnAssert("two windows",openForms(),2)
dnAssert("the owner",oTool.Owner.Text,"Main window")
# a second click on the button does not make a second window
openTool()
dnAssert("still two windows",openForms(),2)
dnAssert("one tool window was made",nToolOpened,1)
# the tool talks to the main window
oToolBox.Text = "blue"
applyTool()
dnAssert("the main window shows it",oShown.Text,"Applied: blue")
dnAssert("the tool says so",oToolStatus.Text,"Sent to the main window")
# closing the tool: the main window stays; the tool can be opened again (a new window)
oTool.Close()
dnAssert("the tool closed",cApplied,"[tool closed]")
dnAssert("one window again",openForms(),1)
openTool()
dnAssert("a new tool window",nToolOpened,2)
dnAssert("two windows again",openForms(),2)
# the form of this test is closed by uiTest after this code: the owned tool window closes with its ownerTalking#
A window is made of controls, and the controls are variables. The tool window calls applyTool(), a function that reads the text box of the tool and writes the label of the main window: oShown.Text = "Applied: " + oToolBox.Text. There is nothing special about it: both windows are in one program. (A bigger program keeps the data in one place and lets both windows read it.)
Counting windows#
Application.OpenForms is the collection of all the open forms of the program: 1 at the start, 2 with the tool, 1 again after the tool was closed. It is a good check in a test, and it is how a program finds out whether a window of a kind is already open.
Closing#
setFormClosedEvent runs after a window closed. The tool window uses it to clear the flag, so the next click opens a new window. The code only sets a flag: assigning another value to oTool inside the event would destroy the object while one of its own methods is running, and Ring refuses (Trying to destroy the object using the self reference). The next openTool makes a new window and puts it in oTool, and the old object is released then.
When the main window closes, its owned windows close with it, and the program ends when the last window is closed (the message loop runs on the main window).
The main window of a test program has to be in the variable oForm, because the test closes oForm when the test code ends.
What you learned#
Show(owner)for a window that stays open,- opening one copy,
- windows that call functions that change each other's controls,
OpenForms,- closing and the clean up of a window object.
Exercises#
- Make the tool window remember its place: write
LeftandTopin the closed event (the tutorial Settings) and use them when it opens again. - Add a button Close the tool to the main window.
- Open a modal dialog from the tool window with
ShowDialogand see which windows can be used. - Make a second kind of tool window (a Colors window) and keep one copy of each.