Title: objects, classes, and hierarchies 1: windows and menus
1objects, classes, and hierarchies 1windows and
menus
2The story up until now
- Everything in your computer is data
- Including programs
- Data is divided into objects
- Objects can be inside of other objects
- Boxes inside groups
- Colors inside bitmaps
- Objects have types (or classes)
- Procedures
- Numbers (1, -3.5)
- Strings (this is a string, blue)
- Bitmaps
- Picture objects (lines, groups, boxes, etc.)
3Looking inside data objects
Ellipse Width 15 Height 10
- Data objects are like forms
- They have fields (aka members)
- Filled in by values
- The fields
- Have names (Width, Height)
- The fields are filled in by other data objects
- The objects type (Box, Color) determines what
fields it has
Box Width 10 Height 10
Number Value 10
Procedure Name iterated-group Arguments proc
count Body apply group
up-to count proc
Color R 240 G 220 B 0
4Member notation
Ellipse Width 15 Height 10
- You can ask for a field of a data object using
the . notation object.memberName - myColor.R
- pixel myBitmap 0 0.R
- iterated-group.Name
- iterated-group.Arguments
- mybox.Width
- Note to simplify the presentation, Ive lied
here about what the actual fields of boxes,
procedures, etc. are. - You can find out what fields are really in an
object using the inspector.
Box Width 10 Height 10
Number Value 10
Procedure Name iterated-group Arguments proc
count Body apply group
up-to count proc
Color R 240 G 220 B 0
5Types and subtypes
Object
- Types often have subtypes
- Integers are a subtype of numbers
- Boxes are a subtype of Pictures
- All types are subtypes of the magic type Object
- Subtyping means objects can have many types
- All Integers are also Numbers
- All Boxes are also Pictures
- And all objects are Objects
List
Number
Array
Integer
Float
Picture
Box
Line
Group
6Example 1The Windows Forms class hierarchy
Control
- User-interface components (windows, buttons,
etc.) are all objects - Individual windows are Form objects
- Individual elements of windows are objects of
type Button, TextBox, Label, etc. - Form, Button, etc. are all subtypes of the type
Control - Controls can have other Controls inside of them
- Which is how buttons can be inside forms
- All these classes are in the System.Windows.Forms
namespace
Form
TextBox
Button
TrackBar
Label
PictureBox
Greatly simplified version of the winforms class
hierarchy
7Wait a minute, whats a class?
- For our purposes, we can think of it as a synonym
for type - In C, Java, and .NET, classes are a kind of
type - Have fields
- Have methods (essentially fields that hold
procedures) - Fields and methods of an object are determined by
its class - Objects of the same class have the same fields
and methods (procedures that seem like fields) - But not necessarily the same values for the fields
8Fields of the Control class
- These can be used to change the appearance of any
control - There are many more, but these should be enough
for you for now.
- BackColor, ForeColor
- Background/foreground color for the control
- Font
- Font that text appears in (see the slide at the
end for how to specify a font) - Text
- Text that appears in the control (if any)
- Height, Width
- The size of the control
- Location
- The X,Y coordinate of the top-left corner (as a
point) - Left, Top
- The individual X- and Y- coordinates of the
control within its window
9Member procedures (aka methods)
- Some of an objects members are procedures
- They act like theyre stored inside the object
- You call them by saying object.name args
- They are usually called methods
- N.B. method is also used to refer to a
slightly different kind of procedure well talk
about in the next unit - Examples
- x.ToStringReturns a string that represents x
- g.DrawLine startx starty endx endyDraws a line
in a window given its
10Class-based inheritance
- When a class is a subtype of another class it
automatically has - All the fields of the parent class
- All the methods of the parent class
- We say that the subclass inherits the fields and
methods of the parent class
11The Form class
- A form is a window that can hold controls
- Its also a subclass of Control, so it has all
the standard fields of controls - E.g. Width, Height, BackColor, etc.
- using System.Windows.Forms
- Tells system to use classes in the Windows Forms
namespace - new Form
- Makes a form
- form.Controls.Add control
- Adds a control to a form
- Application.Run form
- Displays form and lets the user work with it
until they close it. - form.Show
- Used to show a new form after Application.Run has
already been invoked
12Creating new data objects
- new type arguments
- Creates a new object of the specified type
- The arguments depend on the type
- For windows forms classes, they generally dont
take any arguments - Great, but how do we get at the data in the
object?
- ? define my-form new Form
13The TextBox control
- new TextBox
- Makes a space where the user can type
- Whatever they type appears in the Text field of
the object (remember the Text field?) - Again, has all the standard fields of a Control
14Example entering text
- define get-text starting-textwith f new
Form tbox new TextBox
f.Controls.Add tbox - tbox.Text ? starting-text
- tbox.Width ? 150
- tbox.Location ? point 10 10
- Application.Run f
- tbox.Text
15Example entering text
- define get-text starting-textwith f new
Form make the form tbox
new TextBox f.Controls.Add tbox - tbox.Text ? starting-text
- tbox.Width ? 150
- tbox.Location ? point 10 10
- Application.Run f
- tbox.Text
16Example entering text
- define get-text starting-textwith f new
Form tbox new TextBox make the
text box f.Controls.Add tbox - tbox.Text ? starting-text
- tbox.Width ? 150
- tbox.Location ? point 10 10
- Application.Run f
- tbox.Text
17Example entering text
- define get-text starting-textwith f new
Form tbox new TextBox
f.Controls.Add tbox add the text box - tbox.Text ? starting-text
- tbox.Width ? 150
- tbox.Location ? point 10 10
- Application.Run f
- tbox.Text
18Example entering text
- define get-text starting-textwith f new
Form tbox new TextBox
f.Controls.Add tbox - tbox.Text ? starting-text set its
text - tbox.Width ? 150
- tbox.Location ? point 10 10
- Application.Run f
- tbox.Text
19Example entering text
- define get-text starting-textwith f new
Form tbox new TextBox
f.Controls.Add tbox - tbox.Text ? starting-text
- tbox.Width ? 150 and width
- tbox.Location ? point 10 10
- Application.Run f
- tbox.Text
20Example entering text
- define get-text starting-textwith f new
Form tbox new TextBox
f.Controls.Add tbox - tbox.Text ? starting-text
- tbox.Width ? 150
- tbox.Location ? point 10 10 and
location - Application.Run f
- tbox.Text
21Example entering text
- define get-text starting-textwith f new
Form tbox new TextBox
f.Controls.Add tbox - tbox.Text ? starting-text
- tbox.Width ? 150
- tbox.Location ? point 10 10
- Application.Run f
give it to the user - tbox.Text
22Example entering text
- define get-text starting-textwith f new
Form tbox new TextBox
f.Controls.Add tbox - tbox.Text ? starting-text
- tbox.Width ? 150
- tbox.Location ? point 10 10
- Application.Run f
- tbox.Text
return the text
23The make command
- You will find that you spend a lot of time
calling new and then setting fields of the
resulting object - This can be tedious
- And hard to read
- The make command simplifies this by letting you
simultaneously - Make the object, and
- Set its fields
- make Type args field value
- Runs new Type args
- Then sets each field of the new object to its
corresponding value - N.B. the colons are mandatory
24Rewriting using make
- define get-text starting-textwith f new
Form tbox make TextBox
Text starting-text
Width 150
Location point 10 10 - f.Controls.Add tbox
- Application.Run f
- tbox.Text
25Aside macros and syntactic sugar
- Make is an example of a macro
- Macros are convenient shorthands for more
complicated expressions - Meta translates make into a call to new and a
bunch of assignment statements (well, almost) - Why are we telling you this?
- Because it means that if you use make incorrectly
- You probably wont see make anywhere in the
debugger - Youll see a call to new instead
- So we just dont want you to be confused
- If you want to learn to write your own macros,
talk to Ian
- Some macros you may have been using
- when test expressions unless test
expressions - Gets translated toif test begin expressions
, or if not test begin expressions ,
respctvly - cond test1 expressions test2
expressions etc - Gets translated toif test1 begin
expressions if test2 begin
expressions etc - define name args expressions
- Gets translated todefine name args ?
expressions
26Three problems
- This is okay, but it has some definite problems
- You click the close box to finish (rather than an
OK box) - It doesnt actually work because Application.Run
turns out to clear the Text of tbox - Well ignore this for the moment and pretend it
works - The window is outrageously tall
- Well leave fixing this as an exercise for the
reader - The window has no name
- You can fix this by setting its Text or Name field
27Adding a button
- define get-text starting-textwith f new
Form tbox make TextBox
Text starting-text
Width 150
Location point 10 10 b
make Button Text
OK Location point
170 10 - f.Controls.Add tbox
- f.Controls.Add b
- Application.Run f
- tbox.Text
28Trying it out
- Hey! We have a button!
- Hey! It doesnt do anything!
- Hey! The window is still too tall and has no
name! - Oh, shut up
29Event handling
- You need some way of specifying what to do when
the button is pressed - This is done by specifying a procedure called an
event handler or a callback - When the even happens, the system calls the
procedure
30Event handling in Meta
- Events look like lists of procedures to call when
the event happens - To add a handler for an event, add it to the
list - object.event ? handler-procedure
- Or specify it as part of the make arguments
- Note Windows Forms always passes two parameters
to the handler procedure - The control that generated the event
- An extra data object that well ignore for the
moment
31Adding a Click handler
- define get-text starting-textwith f new
Form tbox make TextBox
Text starting-text
Width 150
Location point 10 10 b
make Button Text
OK Location point
170 10 Click ignore
ignore ? f.Close - f.Controls.Add tbox
- f.Controls.Add b
- Application.Run f
- tbox.Text
32Shortening the form
- define get-text starting-textwith f make
Form Height 75
tbox make TextBox
Text starting-text
Width 150
Location point 10 10 b make
Button Text OK
Location point 170 10
Click ignore ignore ?
f.Close - f.Controls.Add tbox
- f.Controls.Add b
- Application.Run f
33Naming the form
- define get-text starting-textwith f make
Form Height 75 - Text Please enter
some text tbox make TextBox
Text starting-text
Width 150
Location point 10 10
b make Button Text
OK Location point
170 10 Click ignore
ignore ? f.Close - f.Controls.Add tbox
- f.Controls.Add b
- Application.Run f
34It works!
- In particular, clicking the OK box
- Closes the form as if the user had clicked the
close box - Which causes Application.Run to finish
- Which causes get-text to finish and return the
text
35The TrackBar control
- new TrackBar
- Makes a slider control
- Useful fields (plus Controls fields)
- Value
- The current numeric value the user has chosen
- Minimum, Maximum
- The numeric values for the ends of the range of
the slider - TickFrequency
- How often to draw tick marks
- Orientation
- Whether it should be a horizontal or vertical
control.Set it to either Orientation.Horizontal
or Orientation.Vertical. - Useful events
- ValueChanged (called when the user moves the
slider)
36What does this do?
- with f new Form r make TrackBar Top
10 g make TrackBar Top 60 b make
TrackBar Top 110 - for-each slider ?
- f.Controls.Add slider
- slider.Width ? 255
- slider.Minimum ? 0
- slider.Maximum ? 255
- slider.ValueChanged ?
ignore ignore ?
f.BackColor ? color r.Value
g.Value b.Value - list r g b
- Application.Run f
37What does this do?
- with f new Form
make a form r make TrackBar Top 10 g
make TrackBar Top 60 b make TrackBar
Top 110 - for-each slider ?
- f.Controls.Add slider
- slider.Width ? 255
- slider.Minimum ? 0
- slider.Maximum ? 255
- slider.ValueChanged ?
ignore ignore ?
f.BackColor ? color r.Value
g.Value b.Value - list r g b
- Application.Run f
38What does this do?
- with f new Form r make TrackBar
Top10 make three sliders to go
with it g make TrackBar Top 60 b
make TrackBar Top 110 - for-each slider ?
- f.Controls.Add slider
- slider.Width ? 255
- slider.Minimum ? 0
- slider.Maximum ? 255
- slider.ValueChanged ?
ignore ignore ?
f.BackColor ? color r.Value
g.Value b.Value - list r g b
- Application.Run f
39What does this do?
- with f new Form r make TrackBar Top
10 g make TrackBar Top 60 b make
TrackBar Top 110 - for-each slider ?
run this procedure - f.Controls.Add slider
- slider.Width ? 255
- slider.Minimum ? 0
- slider.Maximum ? 255
- slider.ValueChanged ?
ignore ignore ?
f.BackColor ? color r.Value
g.Value b.Value - list r g b
- Application.Run f
40What does this do?
- with f new Form r make TrackBar Top
10 g make TrackBar Top 60 b make
TrackBar Top 110 - for-each slider ?
- f.Controls.Add slider
- slider.Width ? 255
- slider.Minimum ? 0
- slider.Maximum ? 255
- slider.ValueChanged ?
ignore ignore ?
f.BackColor ? color r.Value
g.Value b.Value - list r g b
on r, g, and b - Application.Run f
41What does this do?
- with f new Form r make TrackBar Top
10 g make TrackBar Top 60 b make
TrackBar Top 110 - for-each slider ?
- f.Controls.Add slider
add the slider to the
form - slider.Width ? 255
- slider.Minimum ? 0
- slider.Maximum ? 255
- slider.ValueChanged ?
ignore ignore ?
f.BackColor ? color r.Value
g.Value b.Value - list r g b
- Application.Run f
42What does this do?
- with f new Form r make TrackBar Top
10 g make TrackBar Top 60 b make
TrackBar Top 110 - for-each slider ?
- f.Controls.Add slider
- slider.Width ? 255
make it 255 pixels wide - slider.Minimum ? 0
- slider.Maximum ? 255
- slider.ValueChanged ?
ignore ignore ?
f.BackColor ? color r.Value
g.Value b.Value - list r g b
- Application.Run f
43What does this do?
- with f new Form r make TrackBar Top
10 g make TrackBar Top 60 b make
TrackBar Top 110 - for-each slider ?
- f.Controls.Add slider
- slider.Width ? 255
- slider.Minimum ? 0
set its limits to 0 and 255
- slider.Maximum ? 255
- slider.ValueChanged ?
ignore ignore ?
f.BackColor ? color r.Value
g.Value b.Value - list r g b
- Application.Run f
44What does this do?
- with f new Form r make TrackBar Top
10 g make TrackBar Top 60 b make
TrackBar Top 110 - for-each slider ?
- f.Controls.Add slider
- slider.Width ? 255
- slider.Minimum ? 0
- slider.Maximum ? 255
- slider.ValueChanged ?
add the following handler for -
when the slider changes
ignore ignore ?
f.BackColor ? color r.Value
g.Value b.Value - list r g b
- Application.Run f
45What does this do?
- with f new Form r make TrackBar Top
10 g make TrackBar Top 60 b make
TrackBar Top 110 - for-each slider ?
- f.Controls.Add slider
- slider.Width ? 255
- slider.Minimum ? 0
- slider.Maximum ? 255
- slider.ValueChanged ?
ignore arguments
and change the background color
of the form ignore ignore
? f.BackColor ? color
r.Value g.Value b.Value - list r g b
- Application.Run f
46What does this do?
- with f new Form r make TrackBar Top
10 g make TrackBar Top 60 b make
TrackBar Top 110 - for-each slider ?
- f.Controls.Add slider
- slider.Width ? 255
- slider.Minimum ? 0
- slider.Maximum ? 255
- slider.ValueChanged ?
ignore ignore ?
f.BackColor ? color r.Value
g.Value b.Value - list r g b
- Application.Run f
start the form up
47Running it
48Fixing a bug
- with f new Form r make TrackBar Top
10 g make TrackBar Top 60 b make
TrackBar Top 110 - for-each slider ?
- f.Controls.Add slider
- slider.Width ? 255
- slider.Minimum ? 0
- slider.Maximum ? 255
- slider.ValueChanged ?
ignore ignore ?
f.BackColor ? color r.Value
g.Value b.Value - list r g b
- Application.Run f
This what it looks like when you start it up.
Whats wrong with this?
49Fixing a bug
- with f new Form r make TrackBar Top
10 g make TrackBar Top 60 b make
TrackBar Top 110 - f.BackColor ? color 0 0 0
- for-each slider ?
- f.Controls.Add slider
- slider.Width ? 255
- slider.Minimum ? 0
- slider.Maximum ? 255
- slider.ValueChanged ?
ignore ignore ?
f.BackColor ? color r.Value
g.Value b.Value - list r g b
- Application.Run f
50What does this do?
- define slider-text
- with f new Form
- s make TrackBar
Top 10
ValueChanged i i ? t.Text ? to-string
s.Value - t make TextBox
Top60 TextChanged
i i ? s.Value ? System.Int32.Parse t.Text - f.Controls.Add s
- f.Controls.Add t
- Application.Run f
- Hints
- System.Int32.Parse string returns the number
represented by string.Example
System.Int32.Parse 10 returns 10 (the
integer, not a string)
51Fixing a bug
- define slider-text
- with f new Form
- s make TrackBar
Top 10
ValueChanged i i ? t.Text ? to-string
s.Value - t make TextBox
Top60 TextChanged
i i ? ignore-errors
? s.Value ?
System.Int32.Parse t.Text - f.Controls.Add s
- f.Controls.Add t
- Application.Run f
- Ignore-errors runs its argument procedure and
ignores any errors that occur - Since ? expressions looks funny, you can also
say expressions
52It works!
53Some other useful Controls
- new Label
- Like a textbox but the user cant change it
- new CheckBox
- Makes a box
- Useful fields
- Checked (whether the box has been checked (true
or false))
54Some other useful Controls
- new PictureBox
- Lets you show pictures
- Useful fields
- Width, Height, LocationSame as any other form
- ImageThe image to show. You make an image by
saying new Bitmap file where file is a
pathname for a .jpg or other image file. - SizeModeControls how to scale the size of the
picture. Set to - PictureBoxSizeMode.AutoSizeTo make it scale
images to fit the window (this doesnt actually
seem to work for me) - PictureBoxSizeMode.StretchImageTo make it
stretch/squish image to exactly fit the window
(this may change the aspect ratio of the image).
55Another useful component
- make Timer Interval
time-in-milliseconds Enabled true
Tick ignore ignore ? code - Creates a timer object that runs code every
time-in-milliseconds - Not actually a type of Control, so dont try to
add it to a form
56appendix
- picky little details I ignored
57Object-oriented programming terminology
- Object
- Any piece of data
- Has type, members (e.g. fields)
- Type
- Determines what kinds of members an object has
- Class
- Essentially means type
- Subclass
- A class that inherits all the members of its
parent class
- Member
- Data (including procedures) associated with
objects - Fields, Methods, Events, Properties
- Field
- Little variable inside an object
- Holds a data value
- Method
- Essentially a field that holds a procedure
- Event
- Essentially a field that holds a list of
procedures
58How do I specify a font?
- Just type
- new Font fontfamily
System.Convert.ToSingle size style - where
- Fontfamily is a string, like Times New Roman
- Size is a number (the size of the font in
points), e.g. 10 or 12 - Style is one of
- FontStyle.Regular, FontStyle.Bold,
FontStyle.Italic, FontStyle.Strikeout,
FontStyle.Underline
59How do we fix get-text to return the stupid text?
- Change
- with f new Form
- b new Button
- tbox new TextBox
- bla bla bla
- Application.Run f
- tbox.Text
- To
- with f new Form
- b new Button
- tbox new TextBox
- remember starting-text
- bla bla bla
- tbox.TextChanged ? ignore ignore ?
remember ? tbox.Text - Application.Run f
- remember
We add a new event handler that runs whenever the
user changes the text