Overview

This section takes you through the code for a small demo script. Future articles will provide full explanations of the topics introduced in this section. It also provides a bigger, more realistic example that you can use to expand and test your Kixforms knowledge. You can find the whole listing at the bottom of this page.

The demo script brings up a window that looks like this:

 

 

Each time the user clicks the button, the label is updated.

The demo accomplishes the following tasks:

Importing the System namespace

The following line imports the System (root) namespace:

$System = CreateObject("Kixtart.System")

Setting Up the Top-Level Container

Every script that presents a GUI contains at least one top-level container. The top-level container in Kixforms is called the Form object. Each form object implements a single main window. The demo script has only one top-level form. When the user closes the form, the script exits. Here is the code that sets up and shows the form:

$Form = $System.Form() $Form.Text = "Kixforms Demo" $Form.Width = 300 $Form.Height = 130 $Form.FontName = "Verdana" $Form.FontSize = 12 ; create the controls that make up the form... $Form.Show() While $Form.Visible $= Execute($Form.DoEvents) Loop Exit 0

Setting Up Buttons and Labels

Like most GUIs, the demo GUI contains a button and a label. (Unlike most GUIs, that's about all that demo contains). Here's the code that initializes the button:

$Button1 = $Form.Controls.Add("Button") $Button1.Left = 35 $Button1.Top = 50 $Button1.Width = 230 $Button1.Height = 40 $Button1.Text = "I'm a Kixforms button!" $Button1.OnClick = "Button1_Click()"

The first line creates a button and adds it to the form. The next four lines set the size and location of the button. The second last line sets the text displayed on the button to "I'm a Kixforms button!". The last line registers an event handler for the button click event. You'll see the event-handling code for this script later under Handling Events.

Here's the code that initializes and positions the label:

$Label1 = $Form.Controls.Add("Label") $Label1.Left = 35 $Label1.Top = 10 $Label1.Width = 270 $Label1.Height = 30 $Label1.Text = $ClickText + $ClickCount

The preceding code is relatively straightforward, except the last line. The last line sets the label text by combining the values of two values. $ClickPrefix contains the string "Number of button clicks: " and $ClickCount contains the count of button clicks.

Handling Events

The demo example contains one event handler to handle button clicks. Here is the event-handling code:

$Button1.OnClick = "Button1_Click()" $Form.Show() While $Form.Visible $=Execute($Form.DoEvents) Loop Function Button1_Click() $ClickCount = $ClickCount + 1 $Label1.Text = $ClickPrefix + $ClickCount EndFunction

The function call that handles button clicks is assigned to the OnClick event property of the button. The form is then shown and the DoEvents loop is entered. While the form is visible, event code returned by DoEvents is run through the Execute function. The result is that when the button clicked, the Button1_Click function is called. The Button1_Click function simply increments the click counter and updates the label text. When Button1_Click finishes, control is returned to the DoEvents loop.

Complete Listing

Break On $System = CreateObject("Kixtart.System") $ClickText = "Number of button clicks: " $ClickCount = 0 $Form = $System.Form() $Form.Text = "Kixforms Demo" $Form.Width = 300 $Form.Height = 130 $Form.FontName = "Verdana" $Form.FontSize = 12 $Label1 = $Form.Controls.Add("Label") $Label1.Left = 35 $Label1.Top = 10 $Label1.Width = 270 $Label1.Height = 30 $Label1.Text = $ClickText + $ClickCount $Button1 = $Form.Controls.Add("Button") $Button1.Left = 35 $Button1.Top = 50 $Button1.Width = 230 $Button1.Height = 40 $Button1.Text = "I'm a Kixforms button!" $Button1.OnClick = "Button1_Click()" $Form.Center() $Form.Show() While $Form.Visible $= Execute($Form.DoEvents) Loop Exit 0 Function Button1_Click() $ClickCount = $ClickCount + 1 $Label1.Text = $ClickText + $ClickCount EndFunction




The CHM file was converted to HTML by chm2web software.