Mono C Winforms Tutorial
Mono C Winforms Tutorial
Tis is Mono C! "in#orms tutorial$ Mono "in#orms tutorial is #or beginner programmers$
%ou may ave a look at te T'! tutorial
or te sister tutorial IronPyton Mono "in#orms tutorial
Mono
Te Mono Pro-ect is an open development initiative sponsored by Novell to develop an open
source+ (*I. version o# te Microso#t $*/T development plat#orm$ It is a $*/T compatible set o#
tools+ ,ic include C! compiler+ Common Language 0untime+ AD1$*/T+ ASP$*/T and
"in#orms libraries$
Mono can be divided into tree groups2
• Core components
• nome development stack
• Microso#t compatibility stack
Te core components are te C! language and te Common language runtime$ Te nome
development stack includes te T'! library and various database connectivity libraries$ Finally
te Microso#t compatibility stack includes te AD1$*/T+ ASP$*/T and te "in#orms libraries$
Mono is multiplat#orm programming plat#orm$ It can be run on Linu)+ 3SD+ Mac 1S .+ Solaris and
"indo,s operating systems$ It is a multilanguage e##ort$ For no,+ only C! language is #ully
supported$ Languages like 4isual 3asic or IronPyton are not yet #inised$
Winforms
"indo,s Forms is a grapical user inter#ace application programming inter#ace 5API6 included as a
part o# Microso#t7s $*/T Frame,ork$ As o# 89 May :;;<+ Mono7s System$"indo,s$Forms :$; is
API complete$ Simply put+ "in#orms is a library #or creating (I applications$
Compiling examples
1ur tutorial uses te C! language$ To compile all te e)amples in tis tutorial+ ,e use te gmcs
compiler$ Te gmcs compiler implements te complete C! :$; speci#ication including generics$ C!
source #iles must end ,it a $cs e)tension$
$ gmcs --about
The Mono C# compiler is (C) 2001-2008, Noell, !nc"
The compiler source coe is release uner the terms o the %N& %'
or more inormation on Mono, isit the pro*ect +eb site
http..."mono-pro*ect"com
The compiler .as .ritten b/ Miguel e !caa, ai 'ratap, Martin aulig,
Mare3 4aar, a*a 5arinath, 6tushi 7nomoto
"e ave a simple C! source #ile$ "e ,ill discuss te source later$ For no,+ ,e concentrate on te
compilation process$
simple"cs
using 4/stem"+ino.s"orms
using 4/stem"9ra.ing
C! applications use modules called assemblies$ Te Mono C! compiler by de#ault only re#erences
tree assemblies2 mscorlib$dll+ System$dll and System$.ml$dll$ Any additional assembly must be
speci#ied ,it a command line option o# te compiler$ In our simple e)ample+ ,e need
System$"indo,s$Forms$dll and System$Dra,ing$dll assemblies$
gmcs -r4/stem"+ino.s"orms"ll -r4/stem"9ra.ing"ll simple"cs
=ere ,e compile te simple$cs source #ile$ I# te output #ile name is not speci#ied+ ,e get te e)e
#ile ,it name e>ual to te source #ile name$ In our e)ample+ simple$e)e #ile$
gmcs -r4/stem"+ino.s"orms"ll -r4/stem"9ra.ing"ll simple"cs
-outsimplee;ample"e;e
=ere ,e speci#y te output #ile name$ "e compile source #ile and get simplee)ample$e)e #ile$
Simple
Tis is a simple "in#orms application$
simple"cs
using 4/stem"+ino.s"orms
using 4/stem"9ra.ing
=ere ,e use te using directive+ ,ic allo,s us to use a type #rom an appropriate namespace
,itout a #ully >uali#ied name$ For e)ample+ ,e can no, ,rite Form instead o#
System$"indo,s$Forms$Form$
public class 4imple orm
:
"""
?
In "in#orms+ any ,indo, or a dialog is a Form$ Tis control is a basic container+ ,ose purpose is
to display oter cild controls$ 1ur class+ Simple+ inerits #rom a #orm$ Tis ,ay it becomes a #orm
itsel#$
Te;t < =4imple=
4ie < ne. 4ie(2>0, 200)
Text and Sie are properties o# a #orm$ Canging tese properties+ ,e modi#y our #orm control$ Te
#irst line displays te)t ?Simple? in te titlebar o# te #orm control$ Te second line sets te si@e o#
te #orm to :;):;; p)$
CenterTo4creen()
?
"en compiled and run+ te Main metod is e)ecuted #irst$ Te code instantiates te Simple class
and runs it$
$ gmcs -r4/stem"+ino.s"orms"ll -r4/stem"9ra.ing"ll simple"cs
=ere is o, ,e compile te source code$ I# ,e didn7t make any mistakes+ ,e sould ave
simple!exe #ile in our current ,orking directory$
Figure2 Simple
Icon
Mono means monkey in Spanis$ I# ,e do not provide an icon #or our application+ ,e ave a ead
o# a monkey by de#ault$ Te ne)t e)ample so,s+ o, to cange tis$
icon"cs
using 4/stem"+ino.s"orms
using 4/stem"9ra.ing
using 4/stem
tr/ :
!con < ne. !con(=.eb"ico=)
? catch (7;ception e) :
Console"+riteine(e"Message)
7nironment"7;it(1)
?
CenterTo4creen()
?
Te code e)ample so,s an icon in te upper le#t corner o# te #orm$ A #orm7s icon is te picture
tat represents te #orm in te taskbar as ,ell as te icon tat is displayed #or te control bo) o# te
#orm$
tr/ :
!con < ne. !con(=.eb"ico=)
? catch (7;ception e) :
Console"+riteine(e"Message)
7nironment"7;it(1)
?
It is a good practice to put all input output ,ork bet,een te tryBcatc key,ords$ Te ,eb$ico #ile
must be available in te current ,orking directory$ Tis is te directory #rom ,ere ,e e)ecute
5$Bicon$e)e6 our application$
Figure2 Icon
Tooltips
A tooltip is a small rectangular popup ,indo, tat displays a brie# description o# a control7s
purpose ,en te user rests te pointer on te control$
tooltips"cs
using 4/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms
public Morm() :
Te;t < =Tooltips=
4ie < ne. 4ie(2>0, 200)
CenterTo4creen()
?
?
class M6pplication :
static oi Main() :
6pplication"un(ne. Morm())
?
?
1ur code e)ample creates a tooltip #or t,o controls$ Te "utton control and te Form control$
ToolTip btnTlp < ne. ToolTip()
=ere ,e create te ToolTip control$ Tis instance is used to provide tooltips #or bot controls$
btnTlp"4etToolTip(this, =This is a orm=)
*otice te creation o# te "utton control$ Te Text property is a label #or te button$ Te ocation
property places te button on te #orm at )9;+ y E; p) coordinates$ Finally+ te $arent property
determines te container+ ,ere te button ,ill reside$
Figure2 Tooltips
"utton
1ur last code e)ample so,s a button control in action$
button"cs
using 4/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms
Controls"6(button)
CenterTo4creen()
?
class M6pplication :
public static oi Main() :
6pplication"un(ne. Morm())
?
?
All (I programming is event driven programming$ In our e)ample+ ,e so, a button control on a
#orm container$ Te button ,ill listen to t,o events$ Te Clic% and te Mouse&nter events$
button"Clic3 < ne. 7ent5anler(DnClic3)
Tis code line plugs an event andler to te Clic% event$ "en ,e click on te button+ te
'nClic%() metod is called$
button"Mouse7nter < ne. 7ent5anler(Dn7nter)
"en ,e enter te button area ,it te mouse pointer+ te Mouse&nter event is triggerd$ In tis
case+ our code calls te 'n&nter() metod$
oi DnClic3(ob*ect sener, 7ent6rgs e) :
Close()
?
"en ,e enter te button control area ,it te mouse pointer+ ?3utton /ntered? te)t is displayed in
te terminal$
Tis part o# te Mono "in#orms tutorial so,ed some introductory code e)amples to get you
started ,it te "in#orms programming library$
Anchor
Te Anchor property o# a control determines o, it is resi@ed ,it its parent$ Ancor is a term
#rom te marine ,orld$ "en an ancor is dropped into te ,ater+ te sip is #i)ed in certain place$
Same applies #or te "in#orms controls$
/ac control in "in#orms can ave one o# tese AnchorSt*les values2
• Top
• Le#t
• 0igt
• 3ottom
*otice+ tat controls are not restricted to one value$ Tey can take any combinatino o# tese values
using te operator$
using 4/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms
public Morm() :
Te;t < =6nchor=
4ie < ne. 4ie(210, 210)
CenterTo4creen()
?
?
class M6pplication :
public static oi Main() :
Morm m < ne. Morm()
6pplication"un(m)
?
?
Tis is a very basic code e)ample+ tat clearly so,s ,at te Anchor property is all about$ "e
ave t,o buttons on te #orm$ Te #irst button as te de#ault AnchorSt*les values+ ,ic are
AncorStyles$Top AncorStyles$Le#t$ Te second button as e)plicitely set te
AncorStyles$0igt$
btn2"6nchor < 6nchor4t/les"ight
"e e)plicitely set te Anchor property o# te second button to AncorStyles$0igt value$
*o, ave a look at te #ollo,ing t,o images$ Te le#t one so,s te application at te beginning$
Te rigt one so,s te same application a#ter resi@ement$ Te #irst button keeps its distance #rom
te le#t and top borders o# te #orm$ Te second button keeps its distance #rom te rigt border o#
te #orm$ 3ut it does not keep any distance in te vertical direction$
+oc%
Te +oc% property allo,s us to stick a control to a certain edge o# te parent #orm or control$
Te #ollo,ing are possible +oc%St*le values$
• Top
• Le#t
• 0igt
• 3ottom
• Fill
• *one
&ditor s%eleton
Te #ollo,ing code e)ample so,s te +oc% property in action$
eitor"cs
using 4/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms
public Morm() :
Te;t < =7itor=
4ie < ne. 4ie(210, 180)
CenterTo4creen()
?
class M6pplication :
public static oi Main() :
6pplication"un(ne. Morm())
?
?
"e so, a menubar and a statusbar$ And te remaining area is taken by te Text"ox control$
Te;to; tb < ne. Te;to;()
tb"'arent < this
=ere ,e create te Text"ox control$ Form container is set to be te parent #or te te)t bo)$
tb"9oc3 < 9oc34t/le"ill
Tis code line makes te Text"ox control take up te remaining space inside te #orm container$
Figure2 /ditor skeleton
Anchored buttons
Te ne)t e)ample so,s t,o buttons placed in te bottom rigt corner o# te #orm$
anchorebuttons"cs
using 4/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms
public Morm() :
Te;t < =6nchor=
4ie < ne. 4ie(+!9T5, 57!%5T)
; < close"+ith
CenterTo4creen()
?
?
class M6pplication :
public static oi Main() :
6pplication"un(ne. Morm())
?
?
Te e)ample displayes 1'+ Close buttons in te bottom rigt corner o# te ,indo,+ as it is common
in dialog ,indo,s$
priate int +!9T5 < 2>0
priate int 57!%5T < 1>0
Te WI+T, and ,&I-,T variables determine te ,idt and eigt o# te application ,indo,$
priate int &TTDN4G4'6C7 < 1>
priate int '6N7G4'6C7 < 8
priate int CD47G4'6C7 < 10
Te ".TT'NS/S$AC& is te space bet,een te 1' and te Close button$ Te $AN&/S$AC&
is te space bet,een te panel and te bottom o# te #orm$ Finally+ te C'S&/S$AC& variable
sets te space bet,een te Close button and te rigt border o# te #orm$
int '6N7G57!%5T < o3"5eight '6N7G4'6C7
=ere ,e compute te eigt o# te panel$ Te eigt o# te panel is based on te eigt o# te 1'
button$ And ,e add some additional space+ so tat te buttons are not too close to te border$
'anel panel < ne. 'anel()
panel"5eight < '6N7G57!%5T
panel"9oc3 < 9oc34t/le"ottom
panel"'arent < this
=ere ,e create and manage te $anel control$ In tis e)ample+ it is used as a container #or our
buttons$ It is glued to te bottom border o# te #orm$ And te buttons are placed ,itin te panel$
o3"Te;t < =D3=
o3"'arent < panel
o3"ocation < ne. 'oint(+!9T5-;, /)
o3"6nchor < 6nchor4t/les"ight
Te 1' button7s parent is set to te panel ,idget$ Te location is computed$ And te Anchor
property is set to te rigt$ Te oter button is created similarly$
Figure2 Ancored buttons
$la*er s%eleton
Te last e)ample o# tis part o# te Mono "in#orms tutorial so,s a more comple) e)ample$ It is a
skeleton o# a music player$
pla/er"cs
using 4/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms
public Morm() :
Te;t < ='la/er=
4ie < ne. 4ie(@>0, 280)
CenterTo4creen()
?
class M6pplication :
public static oi Main() :
6pplication"un(ne. Morm())
?
?
Tis is te black panel+ ,ic takes all te remaining space+ le#t by te menubar+ statusbar and te
control panel$
'anel button'anel < ne. 'anel()
button'anel"'arent < this
button'anel"5eight < J0
button'anel"9oc3 < 9oc34t/le"ottom
Tis is te control panel$ Its parent is te #orm container$ It is glued to te bottom o# te #orm$ Its
eigt is G;p)$ Inside tis control panel+ ,e place all te buttons and te trackar$
utton pause < ne. utton()
pause"lat4t/le < lat4t/le"'opup
pause"'arent < button'anel
pause"ocation < ne. 'oint(>, 10)
pause"4ie < ne. 4ie(2>, 2>)
pause"!mage < ne. itmap(=pause"png=)
Te pause button is one o# te #our buttons+ tat as te de#ault Anchor property value$ Te style o#
te button is set to #lat+ because it looks better$ "e put a bitmap on te button$
tb"6nchor < 6nchor4t/les"ight
"""
auio"6nchor < 6nchor4t/les"ight
Simple menu
In te #irst e)ample+ ,e create a simple menu$
simplemenu"cs
using 4/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms
ms"!tems"6(ile)
MainMenu4trip < ms
4ie < ne. 4ie(2>0, 200)
CenterTo4creen()
?
class M6pplication :
public static oi Main() :
6pplication"un(ne. Morm())
?
?
In our e)ample+ ,e ave a menubar and one menu$ Inside a menu tere is one menu item$ I# ,e
select te menu item+ application is closed$
*otice te ,ays o, ,e can close te application$ "e can close it by using te CtrlH. sorcut or
by pressing Alt+ F+ / keys$
Menu4trip ms < ne. Menu4trip()
MenuStrip creates a menu system #or our #orm$ "e add ToolStripMenuItem ob-ects to te
MenuStrip tat represent te individual menu commands in te menu structure$ /ac
ToolStripMenuItem can be a command #or your application or a parent menu #or oter submenu
items$
Tool4tripMenu!tem ile < ne. Tool4tripMenu!tem(=Eile=)
Te e)it menu item is added to te drop do,n items o# te menu ob-ect$
ms"!tems"6(ile)
Submenu
/ac menu item can also ave a submenu$ Tis ,ay ,e can group similar commnads into groups$
For e)ample ,e can place commands tat ideBso, various toolbars like personal bar+ address bar+
status bar or navigation bar into a submenu called toolbars$
submenu"cs
using 4/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms
public Morm() :
Te;t < =4ubmenu=
ile"9rop9o.n!tems"6(import)
ile"9rop9o.n!tems"6(e;it)
ms"!tems"6(ile)
MainMenu4trip < ms
4ie < ne. 4ie(@80, 200)
CenterTo4creen()
?
class M6pplication :
public static oi Main() :
6pplication"un(ne. Morm())
?
?
In tis e)ample+ ,e create one submenu$ Te submenu Import as tree menu items$
Tool4tripMenu!tem import < ne. Tool4tripMenu!tem()
import"Te;t < =!mport=
Figure2 Submenu
using 4/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms
public Morm() :
Te;t < =Chec3 menu item=
CenterTo4creen()
?
class M6pplication :
public static oi Main() :
6pplication"un(ne. Morm())
?
?
"e ave t,o menus$ File and 4ie,$ Te 4ie, menu as a menu item tat toggles te visibility o#
te statusbar$
MainMenu mainMenu < ne. MainMenu()
In tis e)ample+ ,e use te MainMenu control$ To create a menubar+ ,e can use eiter MainMenu
or MenuStrip controls$ Te latter as some additional #unctionality$
ie.4tatusar"Chec3e < true
Tis menu item is cecked by de#ault+ because te statusbar is visible #rom te start o# te
application$
bool chec3 < ie.4tatusar"Chec3e
i (chec3) :
sb"Iisible < alse
ie.4tatusar"Chec3e < alse
? else :
sb"Iisible < true
ie.4tatusar"Chec3e < true
?
"e determine+ i# te menu item is cecked$ "e so, and ide te statusbar and te ceck tick
depending on te chec% value$
Images0 separator
"e ,ill #urter enance our kno,ledge o# te MenuStrip control$ "e ,ill create a menu item ,it
an image and so, o, to separate tem ,it a separator$
menustrip"cs
using 4/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms
public Morm() :
Te;t < =Menu4trip=
4ie < ne. 4ie(2>0, 200)
Menu4trip menu4trip < ne. Menu4trip()
titem1"9rop9o.n!tems"6(ne. Tool4trip4eparator())
CenterTo4creen()
?
"e ave t,o menus in our code e)ample$ File and Tools$ In te File ,e ave tree menu items ,it
images$ "e ave also one separator$ In tis e)ample+ te png images must be located in te current
,orking directory$
Tool4tripMenu!tem subm1 < ne. Tool4tripMenu!tem(=Ne.=)
subm1"!mage < !mage"romile(=ne."png=)
titem1"9rop9o.n!tems"6(subm1)
=ere ,e create te #irst menu item$ To add an image to te item+ ,e set te Image property to our
image$ "e create an Image #rom te speci#ied #ile using te static FromFile() metod$
titem1"9rop9o.n!tems"6(ne. Tool4trip4eparator())
Tool"ar
Menus group all commands tat ,e can use in an application$ Toolbars provide a >uick access to
te most #re>uently used commands$ Tool"ar control is used to display Tool"ar"utton controls$
"e can assign images to te buttons by creating an Imageist$ "e tan assign te image list to te
Imageist property o# te toolbar and assign te image inde) value to te ImageIndex property #or
eac Tool"ar"utton$
toolbar"cs
using 4/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms
public Morm()
:
4ie < ne. 4ie(2>0, 200)
Te;t < =4imple toolbar=
sae"!mage!ne; < 0
sae"Tag < =4ae=
e;it"!mage!ne; < 1
CenterTo4creen()
?
"e determine ,ic icon #rom te image list ,ill be used #or te save toolbar button$
toolar"uttons"6ange(ne. Toolarutton :sae, e;it?)
Figure2 Tool3ar
Tis part o# te "in#orms tutorial ,as about menus and toolbars$
abel Control
abel is a simple control #or displaying te)t or images$ It does not receive #ocus$
label"cs
using 4/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms
public Morm() :
Te;t < =Tainte oe=
CenterTo4creen()
?
?
class M6pplication :
public static oi Main() :
6pplication"un(ne. Morm())
?
?
abel control is created$
string te;t < P=4ometimes ! eel !Qe got """
Figure2 Label
Chec%"ox
Chec%"ox is a control tat as t,o states$ 1n and 1##$ It is a bo) ,it a label or an image$ I# te
Ceck3o) is cecked+ it is represented by a tick in a bo)$ A Ceck3o) can be used to so,Bide
splasscreen at startup+ toggle visibility o# a toolbar etc$
chec3bo;"cs
using 4/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms
public Morm() :
Te;t < =Chec3o;=
4ie < ne. 4ie(220, 1A0)
CenterTo4creen()
?
class M6pplication :
public static oi Main() :
6pplication"un(ne. Morm())
?
?
1ur code e)ample so,s or ides te title o# te ,indo, depending on it7s state$
cb < ne. Chec3o;()
Chec%"ox control is created$
cb"Te;t < =4ho. Title=
cb"Chec3e < true
"en te application starts+ ,e so, te title$ And ,e set te Chec%"ox control to cecked state$
cb"Chec3eChange < ne. 7ent5anler(DnChange)
? else :
Te;t < ==
?
Figure2 Ceck3o)
Trac%"ar
Trac%"ar is a component tat lets te user grapically select a value by sliding a knob ,itin a
bounded interval$ 1ur e)ample ,ill so, a volume control$
trac3bar"cs
using 4/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms
public Morm() :
Te;t < =Trac3ar=
4ie < ne. 4ie(2S0, 1R0)
?
oi oa!mages() :
mute < ne. itmap(=mute"png=)
min < ne. itmap(=min"png=)
me < ne. itmap(=me"png=)
ma; < ne. itmap(=ma;"png=)
?
i (al << 0) :
pb"!mage < mute
? else i (al 0 EE al U< @) :
pb"!mage < min
? else i (al @ EE al U 8) :
pb"!mage < me
? else :
pb"!mage < ma;
?
?
?
class M6pplication :
public static oi Main() :
6pplication"un(ne. Morm())
?
?
In te code e)ample+ ,e so, a Trac%"ar and a $icture"ox $ 3y dragging te track bar+ ,e cange
te image on te $icture"ox control$
tb < ne. Trac3ar()
Picture3o) control is created$ It is used to display an image$ At te start+ it so,s te mute image$
oi oa!mages() :
mute < ne. itmap(=mute"png=)
min < ne. itmap(=min"png=)
me < ne. itmap(=me"png=)
ma; < ne. itmap(=ma;"png=)
?
? else :
pb"!mage < ma;
?
"e determine te value o# te Trac%"ar$ Depending on its value+ ,e update te $icture"ox
control$
Figure2 Track3ar
Combo"ox
Combo"ox is a control tat combines a button or editable #ield and a dropdo,n list$ Te user can
select a value #rom te dropdo,n list+ ,ic appears at te user7s re>uest$ I# you make te combo
bo) editable+ ten te combo bo) includes an editable #ield into ,ic te user can type a value$
combobo;"cs
using 4/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms
public Morm() :
Te;t < =Comboo;=
4ie < ne. 4ie(2J0, 2J0)
CenterTo4creen()
?
class M6pplication :
public static oi Main() :
6pplication"un(ne. Morm())
?
?
1ur code programming e)ample so,s a combobo) ,it #ive items$ Te selected item is so,n in
a label control$
cb < ne. Comboo;()
Combo"ox control is created$
cb"!tems"6ange(ne. ob*ect :=&buntu=,
=Manria=,
=e 5at=,
=eora=,
=%entoo=?)
=ere te selected te)t #rom te combobo) is copied to te label$
Figure2 Combo3o)
MonthCalendar
In te ne)t e)ample+ ,e ,ill so, a MonthCalendar control$ Te MontCalendar control allo,s
te user to select a date using a visual display$
monthcalenar"cs
using 4/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms
public Morm() :
Te;t < =Month Calenar=
4ie < ne. 4ie(2J0, 2J0)
CenterTo4creen()
?
class M6pplication :
public static oi Main() :
6pplication"un(ne. Morm())
?
?
"e ave t,o controls$ A MonthCalendar and a abel$ Te latter so,s te currently selected date$
oi Dn4electe(ob*ect sener, 7ent6rgs e) :
9ateTime t < calenar"4election4tart
ate"Te;t < t"Month == t"9a/ == t"Vear
?
"en ,e select a date #rom te MonthCalendar + te 'nSelected() metod is called$ Te
SelectionStart property gets te start date o# te selected range o# dates$
Figure2 MontCalendar
Text"ox
Te Text"ox control is used to display or accept some te)t$ Te te)t can be single or multiline$ Tis
control is also capable o# pass,ord masking$
te;tbo;"cs
using 4/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms
public Morm() :
Te;t < =Te;to;=
4ie < ne. 4ie(2>0, 200)
CenterTo4creen()
?
class M6pplication :
public static oi Main() :
6pplication"un(ne. Morm())
?
?
Tis e)ample so,s a te)t bo) and a label$ Te te)t tat ,e key in te te)t bo) is displayed
immediately in te label control$
te;t < ne. abel()
"""
te;t"6uto4ie < true
Te abel control is created$ Te AutoSie property ensures+ tat te Label gro,s to so, te te)t$
Te;to; tbo; < ne. Te;to;()
"""
tbo;"Ke/&p < ne. Ke/7ent5anler(DnKe/&p)
"e plug in te 1e*.p event$ "en ,e release te key+ 'n1e*.p() metod is called$
oi DnKe/&p(ob*ect sener, Ke/7ent6rgs e) :
Te;to; tb < (Te;to;) sener
this"te;t"Te;t < tb"Te;t
?
In te 'n1e*.p() metod ,e update te label control ,it te te)t #rom te te)t bo) control$
Figure2 Te)t3o)
"e ave #inised capter o# te Mono "in#orms tutorial+ dedicated to basic controls$
ist"ox Control
Te ist"ox control is used to display a list o# items$ (sers can select one or more items by
clicking on tem$
listbo;"cs
using 4/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms
public Morm() :
Te;t < =isto;=
4ie < ne. 4ie(210, 210)
CenterTo4creen()
?
class M6pplication :
public static oi Main() :
6pplication"un(ne. Morm())
?
?
1ur e)ample so,s a listbo) ,it si) names$ Te selected item is so,n in te statusbar$
isto; lb < ne. isto;()
lb"'arent < this
ist"ox control is created$
lb"!tems"6(=Wessica=)
Tis is o, ,e add a ne, item to te ist"ox control$ Te control as te Items property$ Te
property is a re#erence to te list o# items in a listbo)$ (sing tis re#erence+ ,e can add+ remove or
get count o# items o# te listbo)$
lb"4electe!ne;Change < ne. 7ent5anler(DnChange)
Inside te 'nChange() metod+ ,e get te re#erence to te listbo) and set te selected te)t to te
statusbar$
Figure2 List3o)
ist2ie3
ist2ie3 control is used to display collections o# items$ It is a more sopisticated control tan te
ist"ox control$ It can display data in various vie,s is mostly used to display data in multicolumn
vie,s$
listie."cs
using 4/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms
using 4/stem"Collections"%eneric
public Morm() :
Te;t < =istIie.=
4ie < ne. 4ie(@>0, @00)
4uspena/out()
esumea/out()
CenterTo4creen()
?
class M6pplication :
public static oi Main() :
6pplication"un(ne. Morm())
?
?
In our e)ample+ ,e ave a listvie, ,it t,o columns$ In te #irst column+ ,e display te name o#
te actress$ In te second one teir date o# birt$ Te data is store in a ist collection$ 3y selecting a
ro,+ te data in a ro, is displayed in te statusbar$ Also+ by clicking on te column eader+ te data
is sorted$
public class 6ctress
:
"""
?
For eac column in a listvie,+ ,e create a Column,eader$ 3y setting te Width to 8+ te ,idt o#
te column is e>ual to te longest item in te column$
istIie. l < ne. istIie.()
l"'arent < this
ist2ie3 control is created$
l"ullo.4elect < true
l"%riines < true
l"6llo.Columneorer < true
l"4orting < 4ortDrer"6scening
=ere ,e set #our properties o# te control$ Tis code lines enable #ull ro, selection+ so, grid lines+
allo, column reordering by dragging te columns and sort te data in ascending order$
l"Columns"6ange(ne. Column5eaer :name, /ear?)
Tis cycle populates te listvie, control$ /ac ro, is added to te listvie, as a ist2ie3Item
class$
l"Iie. < Iie."9etails
Te ist2ie3 control can ave di##erent vie,s$ Di##erent vie,s display data di##erently$
istIie. l < (istIie.) sener
string name < l"4electe!tems0"4ub!tems0"Te;t
string born < l"4electe!tems0"4ub!tems1"Te;t
sb"Te;t < name =, = born
Inside te 'nChanged() metod+ ,e get te data #rom te selected ro, and so, it on te
statusbar$
i (l"4orting << 4ortDrer"6scening) :
l"4orting < 4ortDrer"9escening
? else :
l"4orting < 4ortDrer"6scening
?
Figure2 List4ie,
Tree2ie3
Tree2ie3 control displays ierarcical collection o# items$ /ac item in tis control is represented
by a TreeNode ob-ect$
treeie."cs
using 4/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms
4tatusar sb
public Morm() :
Te;t < =TreeIie.=
4ie < ne. 4ie(2>0, 2>0)
CenterTo4creen()
?
class M6pplication :
public static oi Main() :
6pplication"un(ne. Morm())
?
?
Tis is a very simple demonstration o# te Tree2ie3 control$ "e ave one root item and tree
cildren$
TreeIie. t < ne. TreeIie.()
Cild nodes are plugged into te Nodes property o# te root node$
Figure2 Tree4ie,
+irectories
Te #ollo,ing code e)ample ,ill e)amine te Tree2ie3 control more indept$
irectories"cs
using 4/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms
using 4/stem"!D
public Morm()
:
4ie < ne. 4ie(J00, J00)
Te;t < =9irectories=
4uspena/out()
4ho.9irectories(t"Noes, 5DM7G9!)
esumea/out()
CenterTo4creen()
?
i (sub9irs"ength 0)
:
oreach (9irector/!no r in sub9irs)
:
i (Xr"Name"4tarts+ith(="=))
4ho.9irectories(tr"Noes, r"ullName)
?
?
trNoe"6(tr)
?
?
1ur code e)ample so,s te directories o# te speci#ied ome directory in a Tree2ie3 control$ Te
application starts ,it some delay+ because it reads te directory structure o# te ome directory
#irst$ "e ave also #our buttons on te #orm$ Te buttons e)pand and collapse nodes
programatically$
t"4crollable < true
"e make te treevie, control scrollable+ because te control so,s lots o# directories$
4ho.9irectories(t"Noes, 5DM7G9!)
Te Sho3+irectories() metod #ills te nodes o# te trevie, control ,it directories available in
te speci#ied ome directory$
i (sub9irs"ength 0)
:
"""
?
"e loop troug all directories$ For tis+ ,e use te recursion algoritm$ "e also skip te idden
directories$ Tey begin ,it a dot on (ni) systems$
trNoe"6(tr)
Tis code line actually adds te directory to te treevie, control$
oi Dn7;pan(ob*ect sener, 7ent6rgs e)
:
t"4electeNoe"7;pan()
?
All #our buttons ave events plugged to a metod$ =ere is a metod #or te /)pand button$ It calls
te &xpand() metod o# te currently selected node$
Figure2 Directories
In tis part o# te Mono "in#orms tutorial+ ,e covered several advanced controls available in
"in#orms library$
+ialogs
In tis part o# te Mono "in#orms tutorial+ ,e ,ill talk about dialogs$
Dialog ,indo,s or dialogs are an indispensable part o# most modern (I applications$ A dialog is
de#ined as a conversation bet,een t,o or more persons$ In a computer application a dialog is a
,indo, ,ic is used to ?talk? to te application$ A dialog is used to input data+ modi#y data+
cange te application settings etc$ Dialogs are important means o# communication bet,een a user
and a computer program$
Tere are essentially t,o types o# dialogs$ Prede#ined dialogs and custom dialogs$
Folder"ro3ser+ialog
Tis dialog prompts te user to select a #older$
olerbro.serialog"cs
using 4/stem
using 4/stem"!D
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms
public Morm() :
Te;t < =olerro.ser9ialog=
toolbar < ne. Toolar()
open < ne. Toolarutton()
toolbar"uttons"6(open)
toolbar"uttonClic3 < ne. ToolaruttonClic37ent5anler(DnClic3e)
Controls"6(toolbar)
CenterTo4creen()
?
class M6pplication :
public static oi Main() :
6pplication"un(ne. Morm())
?
?
"e ave a toolbar and one toolbar button$ 3y clicking on te button+ te Folder"ro3ser+ialog
appears on te screen$ Te name o# te selected #older is so,n in te statusbar$
olerro.ser9ialog ialog < ne. olerro.ser9ialog()
Te Sho3+ialog() metod so,s te dialog on te screen$ I# ,e click on te 1' button o# te
dialog+ te selected directory pat is so,n on te statusbar$
Figure2 Folder3ro,serDialog
Color+ialog
Tis dialog displays available colors along ,it controls tat enable te user to de#ine custom
colors$
colorialog"cs
using 4/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms
public Morm() :
Te;t < =Color9ialog=
toolbar"uttons"6(open)
toolbar"uttonClic3 < ne. ToolaruttonClic37ent5anler(DnClic3e)
ocateect()
CenterTo4creen()
?
g"illectangle(brush, r)
?
oi ocateect() :
int ; < (Client4ie"+ith - rect+ith) 2
int / < (Client4ie"5eight - rect5eight) 2
r < ne. ectangle(;, /, rect+ith, rect5eight)
?
?
class M6pplication :
public static oi Main() :
6pplication"un(ne. Morm())
?
?
In tis code e)ample+ ,e use te Color+ialog to coose a color #or a rectangle+ tat is located in te
middle o# te #orm control$
color < Color"lue
At te beginning+ te color o# te rectangle is blue$ "e use te color variable to determine te color
o# te rectangle$
Color9ialog ialog < ne. Color9ialog()
Te code so,s te color dialog$ I# ,e click on te 1' button+ ,e get te selected color and call
te Invalidate() metod$ Te metod invalidates te entire sur#ace o# te control and causes te
control to be redra,n$ Te result is tat te rectangle is dra,n ,it a ne, color value$
Figure2 ColorDialog
Font+ialog
Te Font+ialog is used to select #onts$
ontialog"cs
using 4/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms
public Morm() :
Te;t < =ont9ialog=
ocateTe;t()
toolbar < ne. Toolar()
toolbar"'arent < this
open < ne. Toolarutton()
toolbar"uttons"6(open)
toolbar"uttonClic3 < ne. ToolaruttonClic37ent5anler(DnClic3e)
CenterTo4creen()
?
oi ocateTe;t() :
te;t"Top < (this"Client4ie"5eight - te;t"5eight) 2
te;t"et < (this"Client4ie"+ith - te;t"+ith) 2
?
?
class M6pplication :
public static oi Main() :
6pplication"un(ne. Morm())
?
?
"e dra, some te)t in te middle o# te #orm control$ "e use te #ont dialog to cange #ont #or tis
te)t$
ont9ialog ialog < ne. ont9ialog()
Font+ialog is created$
i (ialog"4ho.9ialog(this) << 9ialogesult"DK) :
te;t"ont < ialog"ont
ocateTe;t()
?
"en ,e click on te 1' button+ ,e set a ne,ly selected #ont #or te abel control$ 3ecause te
si@e o# te te)t canges ,it di##erent #onts+ ,e must call te ocateText() metod+ ,ic locates
te te)t in te middle o# te #orm control$
Figure2 FontDialog
'pen+ialog
Tis dialog is used to open #iles$
openialog"cs
using 4/stem
using 4/stem"!D
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms
public Morm() :
Te;t < =Dpenile9ialog=
toolbar"uttons"6(open)
toolbar"uttonClic3 < ne. ToolaruttonClic37ent5anler(DnClic3e)
Controls"6(toolbar)
Controls"6(te;tbo;)
CenterTo4creen()
?
class M6pplication :
public static oi Main() :
6pplication"un(ne. Morm())
?
?
"e use te 'pen+ialog control to open C! source #iles$ "e ave a Text"ox control+ ,ere ,e
display te #ile$
Dpenile9ialog ialog < ne. Dpenile9ialog()
"e set te Filter property to C! source #iles$ 1nly C! #iles can be cosen ,it tis dialog instance$
i (ialog"4ho.9ialog(this) << 9ialogesult"DK) :
4treameaer reaer < ne. 4treameaer(ialog"ileName)
string ata < reaer"eaTo7n()
reaer"Close()
te;tbo;"Te;t < ata
?
A#ter clicking 1'+ ,e read te contents o# te cosen #ile and put it into te Text"ox control$
Figure2 1penDialog
In tis part o# te Mono "in#orms tutorial+ ,e so,ed various dialogs$
googlecolorbg ?FFFFFF?O
googlecolorlink ?9D<8//?O
googlecolorte)t ?;;;;;;?O
googlecolorurl ?;;<;;;?O
googleui#eatures ?rc28;?O
BB,indo,$googlerenderad56OJK
googleadclient ?pubE;NE;E8889:?O
googlead,idt GN<O
googleadeigt N;O
googlead#ormat ?GN<)N;as?O
googleadtype ?te)t?O
BB:;;E;E8:2 ori@ont
googleadcannel ?EN;E9:9?O
googlecolorborder ?/N/N/N?O
googlecolorbg ?FFFFFF?O
googlecolorlink ?;;;;FF?O
googlecolorte)t ?;;;;;;?O
googlecolorurl ?;;<;;;?O
googleui#eatures ?rc28;?O
BB
,indo,$googlerenderad56OTis part o# te Mono "in#orms tutorial ,ill be dedicated to te drag
& drop operations$
In computer grapical user inter#aces+ draganddrop is te action o# 5or support #or te action o#6
clicking on a virtual ob-ect and dragging it to a di##erent location or onto anoter virtual ob-ect$ In
general+ it can be used to invoke many kinds o# actions+ or create various types o# associations
bet,een t,o abstract ob-ects$ 5"ikipedia6
Drag and drop #unctionality is one o# te most visible aspects o# te grapical user inter#ace$ Drag
and drop operation enables you to do comple) tings intuitively$
+ragging a button
In te #irst e)ample+ ,e ,ill do te drag & drop operation on te button control$ Te e)ample does
te -ob outside te drag & drop protocol$
ragbutton"cs
using 4/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms
public Morm()
:
Te;t < =9rag E rop button=
4ie < ne. 4ie(2A0, 180)
CenterTo4creen()
?
Te code e)ample puts a regular button control on te #orm container$ 3y clicking on te button
sur#ace and simultaneously dragging it ,it a mouse ,e can relocate te button$
priate bool is9ragging < alse
priate int olF, olV
Tese are te supporting variables #or our e)ample$ Te is+ragging variable tells us+ ,eter ,e
are in te process o# dragging an ob-ect$ Te old5 and old6 variables store te )+ y coordinates -ust
be#ore te dragging process begins$
button"Mouse9o.n < ne. Mouse7ent5anler(DnMouse9o.n)
button"Mouse&p < ne. Mouse7ent5anler(DnMouse&p)
button"MouseMoe < ne. Mouse7ent5anler(DnMouseMoe)
"e plug in tree di##erent mouse andlers #or our button$ Tey implement tree di##erent stages o#
te drag & drop process$ Te process begins+ ,en ,e click on te button$ Tis is andled by te
'nMouse+o3n() metod$ Te second part is te movement$ Tis is ,en ,e move te ob-ect to a
ne, position$ It is andled in te 'nMouseMove() metod$ Te #inal part is ,en te process
stops$ It appens ,en ,e release te mouse button$ Te appropriate task is delegated to te
'nMouse.p() metod$
priate oi DnMouse9o.n(ob*ect sener, Mouse7ent6rgs e)
:
is9ragging < true
olF < e"F
olV < e"V
?
Te 'nMouse+o3n() metod implements te #irst part o# te process$ It sets tree necessary
variables$
priate oi DnMouseMoe(ob*ect sener, Mouse7ent6rgs e)
:
i (is9ragging)
:
button"Top < button"Top (e"V - olV)
button"et < button"et (e"F - olF)
?
?
In te 'nMouseMove() metod+ ,e relocate te button$ "e calculate te di##erence bet,een te
stored )+ y coordinates and te ne, coordinates o# te mouse pointer$ Te di##erence is added to te
Top and eft properties o# te button+ tus moving it to a ne, position$
+ragging Text
In te previous e)ample+ ,e did drag & drop on te control$ *e)t ,e ,ill do a drag & drop
operation on te te)tual data$ =ere ,e ,ill use te drag & drop protocol provided by te "in#orms
library$
Drag & drop operation is a standardi@ed communication protocol in "in#orms$ "e ave t,o basic
ob-ects$ Te drag source and te drop target $
ragte;t"cs
using 4/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms
public Morm()
:
!nitorm()
CenterTo4creen()
?
:
Te;to; t;t < (Te;to;) sener
t;t"9o9rag9rop(t;t"Te;t, 9rag9rop7ects"Cop/)
?
"e ave t,o controls on te #orm$ A button and a te)t bo)$ "e ,ill drag te)t #rom te te)t bo) and
drop it on te button$
!nitorm()
Setting up o# te #orm is delegated to te InitForm() metod$ Tis is usually done in larger
applications$
4uspena/out()
"""
esumea/out()
"e lay out our controls bet,een tese t,o metod calls$ Tis is an optimali@ation$ It sould
eliminate #licker$
button"6llo.9rop < true
Again+ te drag & drop process is divided into tree steps$ "e ave tree metods #or eac
particular step$
priate oi DnMouse9o.n(ob*ect sener, Mouse7ent6rgs e)
:
Te;to; t;t < (Te;to;) sener
t;t"9o9rag9rop(t;t"Te;t, 9rag9rop7ects"Cop/)
?
In te 'nMouse+o3n() metod ,e initiali@e te drap & drop process$ "e initiate te process ,it
te +o+rag+rop() metod$ Te +rag+rop&ffects!Cop* parameter speci#ies te type o# te
operation$ /sentially+ ,e can eiter copy te te)t or move it during te drag & drop operation$
priate oi Dn9rag7nter(ob*ect sener, 9rag7ent6rgs e)
:
e"7ect < 9rag9rop7ects"Cop/
?
Te +rag&nter event is launced ,en te mouse pointer enters te area o# te drop target control$
Te &ffect property must be set$ Te DragDrop/##ects o# te drag source and drop target must be
e>ual$ 1ter,ise te operation ,ill not ,ork$
priate oi Dn9rag9rop(ob*ect sener, 9rag7ent6rgs e)
:
utton button < (utton) sener
button"Te;t < (string) e"9ata"%et9ata(9ataormats"Te;t)
?
Finally ,e ave te 'n+rag+rop() metod$ =ere ,e get te data #rom te event ob-ect and set it to
te button Text property$
+ragging Image
In our last e)ample+ ,e ,ill drag & drop image on te #orm$
ragimage"cs
using 4/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms
public !mage9rag9rop()
:
Client4ie < ne. 4ie(@>0, 2>0)
Te;t < =9ragging !mage=
'aint < ne. 'aint7ent5anler(Dn'aint)
is9ragging < alse
ropect < ne. ectangle(10, 10, 200, 1S0)
brush < rushes"%ra/
pico; < ne. 'ictureo;()
loa!mage()
CenterTo4creen()
?
oi loa!mage() :
tr/ :
image < ne. itmap(=image"*pg=)
? catch :
Console"+riteine(=7rror reaing image=)
7nironment"7;it(1)
?
?
i(ropect"Contains(pico;"ouns)) :
brush < rushes"%ol
? else :
brush < rushes"%ra/
?
eresh()
?
In our e)ample ,e ave a $icture"ox and ,e dra, a gray rectangle$ I# ,e drop te picture inside
te rectangle+ te color o# te rectangle canges to gold$
brush < rushes"%ra/
Te brush variable olds te brus o# te rectangle$ It is a gray color by de#ault$
oi loa!mage() :
tr/ :
image < ne. itmap(=image"*pg=)
? catch :
Console"+riteine(=7rror reaing image=)
7nironment"7;it(1)
?
?
In te 'nMouse.p() metod+ ,e determine te brus o# te rectangle$ I# te bounds o# te picture
bo) are inside te rectangle+ te brus is o# gold colorO gray oter,ise$
eresh()
"e must call te 7efresh() metod to activate te ne, brus color$
Figure2 Drag & drop image
Tis capter ,as dedicated to drag & drop operations using te Mono "in#orms library$
custom control #rom scratc$ To do te painting+ ,e use te painting API provided by te "in#orms
library$ Te painting is done ,itin a metod+ tat ,e plug into te $aint event$
Te S*stem!+ra3ing namespace provides access to -+I8 basic grapics #unctionality$ More
advanced #unctionality is provided in te System$Dra,ing$Dra,ing:D+ System$Dra,ing$Imaging+
and System$Dra,ing$Te)t namespaces$ Te -raphics class provides metods #or dra,ing on te
#orm$
ines
1ur #irst e)ample ,ill dra, lines on te Form control$
lines"cs
using 4/stem
using 4/stem"9ra.ing
using 4/stem"9ra.ing"9ra.ing29
using 4/stem"+ino.s"orms
public Morm() :
Te;t < =ines=
4ie < ne. 4ie(280, 2A0)
esieera. < true
pen"9ash'attern < ne. loat :S, 8, 1, 1, 1, 1, 1, 1 ?
g"9ra.ine(pen, 20, 200, 2>0, 200)
g"9ispose()
?
?
class M6pplication :
public static oi Main() :
6pplication"un(ne. Morm())
?
?
"e dra, #ive lines on te #orm$ /ac line as di##erent +ashSt*le$
"en ,e resi@e te #orm+ it is automatically redra,n$ Tis is not te de#ault beavior$
'aint < ne. 'aint7ent5anler(Dn'aint)
In order to paint on te #orm+ ,e must get te -raphics ob-ect$ Painting on a #orm is actually
calling various metods o# te -raphics ob-ect$
'en pen < ne. 'en(Color"lac3, 1)
"e create a $en ob-ect$ Tis ob-ect is used to dra, outlines o# sapes$ Tan ,e set a dotted
+ashSt*le$ Finally ,e dra, te line ,it te +ra3ine() metod$ Te #irst parameter is te pen
ob-ect$ Te ne)t #our values are ) and y values o# starting and ending points o# te line$
pen"9ash'attern < ne. loat :S, 8, 1, 1, 1, 1, 1, 1 ?
Tere are several builtin +ashSt*le values$ "e can create our o,n style by using te +ash$attern
property$ It may look di##icult at te #irst sigt$ 3ut te pattern is simply an array o# #ill and empty
values$
g"9ispose()
"e kno, tat C! language uses garbage collection$ So ,y are ,e e)plicitely releasing resourcesQ
Tis is to increase e##iciency$ "e are elping te garbage collector$
Figure2 Lines
Colors
A color in "in#orms library represents an A03 5alpa+ red+ green+ blue6 color$ It is a combination
o# Alpa+ 0ed+ reen+ and 3lue 5036 intensity values$ Tere are also prede#ined color names+ tat
,e can use in painting$ ttp2BB@etcode$comBtutorialsBmono,in#ormstutorialBimages
colors"cs
using 4/stem
using 4/stem"9ra.ing
using 4/stem"9ra.ing"9ra.ing29
using 4/stem"+ino.s"orms
public Morm() :
Te;t < =Colors=
4ie < ne. 4ie(@S0, @00)
httpetcoe"comtutorialsmono.inormstutorialimages
'aint < ne. 'aint7ent5anler(Dn'aint)
CenterTo4creen()
?
g"9ispose()httpetcoe"comtutorialsmono.inormstutorialimages
?
?
class M6pplication :
public static oi Main() :
6pplication"un(ne. Morm())
?
?
Te Fill7ectagle() metod #ills a speci#ied rectangle ,it a brus$ A brus can be a color or a
pattern$ Tere are some prede#ined colors available$ "e can get tem #rom te "rushes
enumeration$ Te last #our values are te )+ y values o# te tople#t point and te ,idt and eigt o#
te rectangle$
Figure2 Colors
,atches
Te ,atch"rush ob-ect is used to #ill te interiors o# te sapes$ Tere are several builtin patterns+
tat ,e can use$
hatches"cs
using 4/stem
using 4/stem"9ra.ing
using 4/stem"9ra.ing"9ra.ing29
using 4/stem"+ino.s"orms
public Morm() :
Te;t < =5atches=
4ie < ne. 4ie(@S0, @00)
CenterTo4creen()
?
hb"9ispose()
g"9ispose()
?
?
class M6pplication :
public static oi Main() :
6pplication"un(ne. Morm())
?
?
Tis time ,e #ill nine rectangles ,it nine di##erent patterns+ called atces$
5atchrush hb < ne. 5atchrush(5atch4t/le"Cross, Color"lac3, this"ac3Color)
=ere ,e create a ,atch"rush ob-ect$ Te parameters are te atc style and te #oreground and te
background colors$ Te background color is set to te color o# te #orm+ so tat it looks like ,e
ave dra,n onto te #orm$
g"illectangle(hb, 10, 1>, R0, S0)
Figure2 =atces
-radients
In computer grapics+ gradient is a smoot blending o# sades #rom ligt to dark or #rom one color
to anoter$ In :D dra,ing programs and paint programs+ gradients are used to create color#ul
backgrounds and special e##ects as ,ell as to simulate ligts and sado,s$ 5ans,ers$com6
graients"cs
using 4/stem
using 4/stem"9ra.ing
using 4/stem"9ra.ing"9ra.ing29
using 4/stem"+ino.s"orms
public Morm() :
Te;t < =%raients=
4ie < ne. 4ie(@>0, @>0)
CenterTo4creen()
?
lg"9ispose()
g"9ispose()
?
?
class M6pplication :
public static oi Main() :
6pplication"un(ne. Morm())
?
?
"e dra, #ive rectangles ,ic are #illed ,it di##erent linear gradients$
ttp2BB@etcode$comBtutorialsBmono,in#ormstutorialBimages
'oint pt1 < ne. 'oint(>, >)
'oint pt2 < ne. 'oint(2>, 2>)
Tese t,o are te controlling points o# te linear gradient brus$
rush lg < ne. inear%raientrush(pt1, pt2, Color"e, Color"lac3)
"e create te inear-radient"rush ob-ect$ "e use t,o controlling points and t,o blending
colors$
Figure2 radients
+ra3ing string
To dra, string on te "in#orms Form+ ,e use te +ra3String() metod$
l/rics"cs
using 4/stem
using 4/stem"9ra.ing
using 4/stem"9ra.ing"9ra.ing29
using 4/stem"+ino.s"orms
public Morm() :
g"9ispose()
?
?
class M6pplication :
public static oi Main() :
6pplication"un(ne. Morm())
?
?
g"9ra.4tring(=Meet /ou o.nstairs in the bar an hear=, t, br, pt)
Te +ra3String() metod takes te #ollo,ing parameters2 te)t to dra,+ #ont+ brus and te $ointF
ob-ect$
Figure2 Lyrics
+ra3ing image
In our last e)ample ,e ,ill dra, an image on te Form control$
reroc3"cs
using 4/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms
public Morm() :
Te;t < =e oc3=
loa!mage()
Client4ie < ne. 4ie(castle"+ith, castle"5eight)
CenterTo4creen()
?
oi loa!mage() :
tr/ :
castle < ne. itmap(=reroc3"png=)
? catch (7;ception e) :
Console"+riteine(e"Message)
7nironment"7;it(1)
?
?
class M6pplication :
public static oi Main() :
6pplication"un(ne. Morm())
?
?
Figure2 Image
Sna%e game
Sna%e is an older classic video game$ It ,as #irst created in late E;s$ Later it ,as brougt to PCs$ In
tis game te player controls a snake$ Te ob-ective is to eat as many apples as possible$ /ac time
te snake eats an apple+ its body gro,s$ Te snake must avoid te ,alls and its o,n body$ Tis
game is sometimes called Nibbles$
+evelopment
Te si@e o# eac o# te -oints o# a snake is 8;p)$ Te snake is controlled ,it te cursor keys$
Initially te snake as tree -oints$ Te game is started by pressing one o# te cursor keys$ I# te
game is #inised+ ,e display ame 1ver message in te middle o# te 3oard$
oar"cs
using 4/stem
using 4/stem"Collections
using 4/stem"ComponentMoel
using 4/stem"9ra.ing
using 4/stem"9ata
using 4/stem"+ino.s"orms
public oar() :
tr/ :
ot < ne. itmap(=ot"png=)
apple < ne. itmap(=apple"png=)
hea < ne. itmap(=hea"png=)
? catch (7;ception e) :
Console"+riteine(e"Message)
7nironment"7;it(1)
?
init%ame()
?
i (in%ame) :
chec36pple()
chec3Collision()
moe()
?
this"eresh()
?
ots < @
locate6pple()
Ke/&p < ne. Ke/7ent5anler(DnKe/&p)
?
i (in%ame) :
? else :
gameDer(g)
?
?
i (let) :
;0 -< 9DTG4!Y7
?
i (right) :
;0 < 9DTG4!Y7
?
i (up) :
/0 -< 9DTG4!Y7
?
i (o.n) :
/0 < 9DTG4!Y7
?
?
i (/0 U 0) :
in%ame < alse
?
i (;0 U 0) :
in%ame < alse
?
?
?
In te chec%Collision() metod+ ,e determine i# te snake as it itsel# or one o# te ,alls$
or (int < ots 0 --) :
Finis te game+ i# te snake its one o# its -oints ,it te ead$
i (/0 57!%5T - 9DTG4!Y7 - T!T76G57!%5T - D97G+!9T5) :
in%ame < alse
?
Finis te game+ i# te snake its te bottom o# te 3oard$
4na3e"cs
using 4/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms
public 4na3e() :
Controls"6(boar)
CenterTo4creen()
?
?
class M6pplication :
public static oi Main() :
6pplication"un(ne. 4na3e())
?
?
Figure2 Snake
Tis ,as te Snake game programmed using te Mono "in#orms library$