Views
- the details
At present, the class that implements the Views concept is called Views.Form.
Each instance of the Views.Form class creates a single
Windows form on the screen. Each form contains some simple controls,
such as labels, buttons and text boxes that the calling program can use
for GUI input and output. The controls displayed on the form and their
layout are specified by an XML string passed to the constructor. The
user can subsequently set and get information that appears within the
controls, e.g. the user can call the GetText method to obtain
the text available inside a textbox control. Below we have a Views Windows form for a
simple holiday query program, with the Views.Form class
instantiation to create it.
Views.Form view = new Views.Form(@"<Form Text=Holidays>
<Vertical>
<Horizontal>
<Label Text='Read the data
from:' Width=150/>
<Textbox Name=Filename
Text=holidays.dat Width=80/>
<Button Name='Read in'/>
</Horizontal>
<Horizontal>
<Vertical>
<Label
Text='List of holidays' Width=150/>
<Listbox Name=List/>
</Vertical>
<Vertical>
<Label
Text='Type and Find or Choose and Select Holiday date' Width=100
Height=50/>
<Textbox
Name=Date Text='16 June' Width=100/>
<Horizontal
Height=20> </Horizontal>
<Label Text =
'Holiday name' Width=100/>
<Textbox
Name=Holiday Width=150/>
<Button
Name=Find/>
<Button
Name=Select/>
</Vertical>
</Horizontal>
<Button Name=Exit/>
</Vertical>
</Form>");
Considerable effort has been put into making the XML specification easy to write, so that for the user’s convenience, capitalization of the tagnames and attribute names is ignored, and double quotes are not required around attribute values. The names of all tags in Views.Form correspond on a one-to-one basis to controls and attributes in the System.Windows.Forms namespace, for ease of upgrading to VS .NET later. Everything has a sensible default. Layout and sizing are automatic unless overridden by explicit attributes. Controls implemented in the View class include: Label, Button, TextBox, and ListBox, OpenFileDialog and SaveFileDialog. There are several others which control the colour of the individual controls. The Views.Form object created by the program maintains the controls specified in the XML. To interact with them, we use a series of methods, such as:
An event loop to process the above GUI would be:
while(view != null) {
string b = view.GetControl();
switch(b) {
case "Read in":
ReadData(view.GetText("Filename"));
break;
case "Find":
HandleSelection (view.GetText("Date"));
break;
case "Select":
HandleSelection (view.GetText("List"));
break;
case "Exit":
view.CloseGUI();
view = null;
break;
default:
Console.WriteLine("Incorrect button string: "+b);
view.CloseGUI();
view = null;
break;
}