0% found this document useful (0 votes)
370 views75 pages

Mono C# Winforms Tutorial

This document provides an introduction and overview of the Mono C# Winforms tutorial. It discusses the Mono platform and Winforms library. It then covers basic Winforms programs like creating a simple window, adding an icon, and tooltips. It demonstrates using buttons and handling button click and mouse enter events. The tutorial is intended for beginner programmers to learn the basics of GUI programming with Mono Winforms.

Uploaded by

Marcelo Pacifico
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
Download as odt, pdf, or txt
0% found this document useful (0 votes)
370 views75 pages

Mono C# Winforms Tutorial

This document provides an introduction and overview of the Mono C# Winforms tutorial. It discusses the Mono platform and Winforms library. It then covers basic Winforms programs like creating a simple window, adding an icon, and tooltips. It demonstrates using buttons and handling button click and mouse enter events. The tutorial is intended for beginner programmers to learn the basics of GUI programming with Mono Winforms.

Uploaded by

Marcelo Pacifico
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1/ 75

Mono C# Winforms tutorial

Introduction First steps Layout management Menus & toolbars Controls Advanced Controls Dialogs Drag & Drop Painting Snake T is is Mono C! "in#orms tutorial$ Mono "in#orms tutorial is #or beginner programmers$ %ou may ave a look at t e &T'! tutorial or t e sister tutorial IronPyt on Mono "in#orms tutorial

Introduction to Mono Winforms


T e #irst part o# t e Mono "in#orms tutorial introduces t e Mono plat#orm and t e "in#orms library$

About this tutorial


T is is Mono C! "in#orms tutorial$ Mono "in#orms tutorial is #or beginner programmers$ T e goal o# t is tutorial is to teac readers basics o# &(I programming in Mono "in#orms$ T e tutorial is created and tested on Linu)$ *evert eless+ it can be used on ot er operating systems as ,ell$ Most e)amples s ould run ,it out modi#ication$ Images used in t is tutorial can be do,nloaded ere$

Mono
T e Mono Pro-ect is an open development initiative sponsored by Novell to develop an open source+ (*I. version o# t e 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 t ree groups2 Core components &nome development stack Microso#t compatibility stack T e core components are t e C! language and t e Common language runtime$ T e &nome development stack includes t e &T'! library and various database connectivity libraries$ Finally t e Microso#t compatibility stack includes t e AD1$*/T+ ASP$*/T and t e "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 IronPyt on are not yet #inis ed$

Winforms
"indo,s Forms is a grap ical 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 t e C! language$ To compile all t e e)amples in t is tutorial+ ,e use t e gmcs compiler$ T e gmcs compiler implements t e 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, No ell, !nc" The compiler source co#e is release# un#er the terms o$ the %N& %'( )or more in$ormation on Mono, isit the pro*ect +eb site http,--..."mono-pro*ect"com The compiler .as .ritten b/ Miguel #e !ca0a, 1a i 'ratap, Martin 2aulig, Mare3 4a$ar, 1a*a 1 5arinath, 6tushi 7nomoto

"e ave a simple C! source #ile$ "e ,ill discuss t e source later$ For no,+ ,e concentrate on t e compilation process$
simple"cs using 4/stem"+in#o.s")orms8 using 4/stem"9ra.ing8 public class 4imple , )orm : public 4imple() : Te;t < =4imple=8 4i0e < ne. 4i0e(2>0, 200)8 CenterTo4creen()8 ? static public oi# Main() : 6pplication"1un(ne. 4imple())8 ?

C! applications use modules called assemblies$ T e Mono C! compiler by de#ault only re#erences t ree assemblies2 mscorlib$dll+ System$dll and System$.ml$dll$ Any additional assembly must be speci#ied ,it a command line option o# t e compiler$ In our simple e)ample+ ,e need System$"indo,s$Forms$dll and System$Dra,ing$dll assemblies$
gmcs -r,4/stem"+in#o.s")orms"#ll -r,4/stem"9ra.ing"#ll simple"cs

=ere ,e compile t e simple$cs source #ile$ I# t e output #ile name is not speci#ied+ ,e get t e e)e #ile ,it name e>ual to t e source #ile name$ In our e)ample+ simple$e)e #ile$
gmcs -r,4/stem"+in#o.s")orms"#ll -r,4/stem"9ra.ing"#ll simple"cs -out,simplee;ample"e;e

=ere ,e speci#y t e output #ile name$ "e compile source #ile and get simplee)ample$e)e #ile$

First steps in Mono Winforms


In t is part o# t e Mono "in#orms tutorial+ ,e introduce some basic programs in "in#orms programing library$

Simple
T is is a simple "in#orms application$
simple"cs using 4/stem"+in#o.s")orms8 using 4/stem"9ra.ing8 public class 4imple , )orm : public 4imple() : Te;t < =4imple=8 4i0e < ne. 4i0e(2>0, 200)8 CenterTo4creen()8 ? static public oi# Main() : 6pplication"1un(ne. 4imple())8 ?

T is code e)ample s o,s a small ,indo, on t e screen$


using 4/stem"+in#o.s")orms8 using 4/stem"9ra.ing8

=ere ,e use t e using directive+ , ic allo,s us to use a type #rom an appropriate namespace ,it out 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$ T is control is a basic container+ , ose purpose is to display ot er c ild controls$ 1ur class+ Simple+ in erits #rom a #orm$ T is ,ay it becomes a #orm itsel#$
Te;t < =4imple=8 4i0e < ne. 4i0e(2>0, 200)8

Text and Si e are properties o# a #orm$ C anging t ese properties+ ,e modi#y our #orm control$ T e #irst line displays te)t ?Simple? in t e titlebar o# t e #orm control$ T e second line sets t e si@e o# t e #orm to :A;):;; p)$
CenterTo4creen()8

T is met od centers our application on t e screen$


static public oi# Main() : 6pplication"1un(ne. 4imple())8

" en compiled and run+ t e Main met od is e)ecuted #irst$ T e code instantiates t e Simple class and runs it$
$ gmcs -r,4/stem"+in#o.s")orms"#ll -r,4/stem"9ra.ing"#ll simple"cs

=ere is o, ,e compile t e source code$ I# ,e didn7t make any mistakes+ ,e s ould 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$ T e ne)t e)ample s o,s+ o, to c ange t is$
icon"cs using 4/stem"+in#o.s")orms8 using 4/stem"9ra.ing8 using 4/stem8 public class M)orm , )orm : public M)orm() : Te;t < =!con=8 4i0e < ne. 4i0e(2>0, 200)8 tr/ : !con < ne. !con(=.eb"ico=)8 ? catch (7;ception e) : Console"+rite(ine(e"Message)8 7n ironment"7;it(1)8 ? ? CenterTo4creen()8

static public oi# Main() : 6pplication"1un(ne. M)orm())8 ?

T e code e)ample s o,s an icon in t e upper le#t corner o# t e #orm$ A #orm7s icon is t e picture

t at represents t e #orm in t e taskbar as ,ell as t e icon t at is displayed #or t e control bo) o# t e #orm$
tr/ : !con < ne. !con(=.eb"ico=)8 ? catch (7;ception e) : Console"+rite(ine(e"Message)8 7n ironment"7;it(1)8 ?

It is a good practice to put all input output ,ork bet,een t e tryBcatc key,ords$ T e ,eb$ico #ile must be available in t e current ,orking directory$ T is is t e directory #rom , ere ,e e)ecute 5$Bicon$e)e6 our application$

Figure2 Icon

Tooltips
A tooltip is a small rectangular popCup ,indo, t at displays a brie# description o# a control7s purpose , en t e user rests t e pointer on t e control$
tooltips"cs using 4/stem8 using 4/stem"9ra.ing8 using 4/stem"+in#o.s")orms8 class M)orm , )orm : public M)orm() : Te;t < =Tooltips=8 4i0e < ne. 4i0e(2>0, 200)8 ToolTip btnTlp < ne. ToolTip()8 btnTlp"4etToolTip(this, =This is a )orm=)8 2utton button < ne. 2utton()8 btnTlp"4etToolTip(button, =This is a 2utton Control=)8 button"Te;t < =2utton=8 button"(ocation < ne. 'oint(@0, A0)8 button"'arent < this8 ? ? CenterTo4creen()8

class M6pplication : static oi# Main() : 6pplication"1un(ne. M)orm())8 ? ?

1ur code e)ample creates a tooltip #or t,o controls$ T e "utton control and t e Form control$
ToolTip btnTlp < ne. ToolTip()8

=ere ,e create t e ToolTip control$ T is instance is used to provide tooltips #or bot controls$
btnTlp"4etToolTip(this, =This is a )orm=)8

=ere ,e set a tooltip #or a #orm$


btnTlp"4etToolTip(button, =This is a 2utton Control=)8

And ere #or our button$


2utton button < ne. 2utton()8 btnTlp"4etToolTip(button, =This is a 2utton Control=)8 button"Te;t < =2utton=8 button"(ocation < ne. 'oint(@0, A0)8 button"'arent < this8

*otice t e creation o# t e "utton control$ T e Text property is a label #or t e button$ T e #ocation property places t e button on t e #orm at )D9;+ y D E; p) coordinates$ Finally+ t e $arent property determines t e container+ , ere t e button ,ill reside$

Figure2 Tooltips

"utton
1ur last code e)ample s o,s a button control in action$
button"cs using 4/stem8 using 4/stem"9ra.ing8 using 4/stem"+in#o.s")orms8

class M)orm , )orm : public M)orm() : Te;t < =2utton=8 4i0e < ne. 4i0e(2>0, 200)8 2utton button < ne. 2utton()8 button"(ocation < ne. 'oint(@0, 20)8 button"Te;t < =Buit=8 button"Clic3 C< ne. 7 ent5an#ler(DnClic3)8 button"Mouse7nter C< ne. 7 ent5an#ler(Dn7nter)8 Controls"6##(button)8 CenterTo4creen()8 oi# DnClic3(ob*ect sen#er, 7 ent6rgs e) : Close()8 oi# Dn7nter(ob*ect sen#er, 7 ent6rgs e) : Console"+rite(ine(=2utton 7ntere#=)8

? ?

class M6pplication : public static oi# Main() : 6pplication"1un(ne. M)orm())8 ? ?

All &(I programming is event driven programming$ In our e)ample+ ,e s o, a button control on a #orm container$ T e button ,ill listen to t,o events$ T e Clic% and t e Mouse&nter events$
button"Clic3 C< ne. 7 ent5an#ler(DnClic3)8

T is code line plugs an event andler to t e Clic% event$ " en ,e click on t e button+ t e 'nClic%() met od is called$
button"Mouse7nter C< ne. 7 ent5an#ler(Dn7nter)8

" en ,e enter t e button area ,it t e mouse pointer+ t e Mouse&nter event is triggerd$ In t is case+ our code calls t e 'n&nter() met od$
? oi# DnClic3(ob*ect sen#er, 7 ent6rgs e) : Close()8

T e met od closes t e application$


? oi# Dn7nter(ob*ect sen#er, 7 ent6rgs e) : Console"+rite(ine(=2utton 7ntere#=)8

" en ,e enter t e button control area ,it t e mouse pointer+ ?3utton /ntered? te)t is displayed in t e terminal$ T is part o# t e Mono "in#orms tutorial s o,ed some introductory code e)amples to get you started ,it t e "in#orms programming library$

#a*out management in Mono Winforms


T e Mono "in#orms tutorial continues ,it t e layout management o# controls$ A#ter ,e ave placed controls on t eir parent containers+ ,e ave to ensure t eir proper layuot$

Anchor
T e Anchor property o# a control determines o, it is resi@ed ,it its parent$ Anc or is a term #rom t e marine ,orld$ " en an anc or is dropped into t e ,ater+ t e s ip is #i)ed in certain place$ Same applies #or t e "in#orms controls$ /ac control in "in#orms can ave one o# t ese AnchorSt*les values2 Top Le#t 0ig t 3ottom

*otice+ t at controls are not restricted to one value$ T ey can take any combinatino o# t ese values using t e F operator$

"asic Anchor example


T e #ollo,ing e)ample s o,s a very basic e)ample+ demonstrating t e Anchor property$
anchor"cs using 4/stem8 using 4/stem"9ra.ing8 using 4/stem"+in#o.s")orms8 class M)orm , )orm : public M)orm() : Te;t < =6nchor=8 4i0e < ne. 4i0e(210, 210)8 2utton btn1 < ne. 2utton()8 btn1"Te;t < =2utton=8 btn1"'arent < this8 btn1"(ocation < ne. 'oint(@0, @0)8 2utton btn2 < ne. 2utton()8 btn2"Te;t < =2utton=8 btn2"'arent < this8 btn2"(ocation < ne. 'oint(@0, 80)8 btn2"6nchor < 6nchor4t/les"1ight8 CenterTo4creen()8 ? ?

class M6pplication : public static oi# Main() : M)orm m$ < ne. M)orm()8 6pplication"1un(m$)8 ? ?

T is is a very basic code e)ample+ t at clearly s o,s , at t e Anchor property is all about$ "e ave t,o buttons on t e #orm$ T e #irst button as t e de#ault AnchorSt*les values+ , ic are Anc orStyles$Top F Anc orStyles$Le#t$ T e second button as e)plicitely set t e Anc orStyles$0ig t$
btn2"6nchor < 6nchor4t/les"1ight8

"e e)plicitely set t e Anchor property o# t e second button to Anc orStyles$0ig t value$ *o, ave a look at t e #ollo,ing t,o images$ T e le#t one s o,s t e application at t e beginning$ T e rig t one s o,s t e same application a#ter resi@ement$ T e #irst button keeps its distance #rom t e le#t and top borders o# t e #orm$ T e second button keeps its distance #rom t e rig t border o# t e #orm$ 3ut it does not keep any distance in t e vertical direction$

Figure2 3e#ore and a#ter resi@ing

+oc%
T e +oc% property allo,s us to stick a control to a certain edge o# t e parent #orm or control$ T e #ollo,ing are possible +oc%St*le values$ Top Le#t 0ig t 3ottom Fill *one

&ditor s%eleton
T e #ollo,ing code e)ample s o,s t e +oc% property in action$
e#itor"cs using 4/stem8 using 4/stem"9ra.ing8

using 4/stem"+in#o.s")orms8 class M)orm , )orm : public M)orm() : Te;t < =7#itor=8 4i0e < ne. 4i0e(210, 180)8 MainMenu mainMenu < ne. MainMenu()8 Menu!tem $ile < mainMenu"Menu!tems"6##(=E)ile=)8 $ile"Menu!tems"6##(ne. Menu!tem(=7E;it=, ne. 7 ent5an#ler(this"Dn7;it), 4hortcut"CtrlF))8 Menu < mainMenu8 Te;t2o; tb < ne. Te;t2o;()8 tb"'arent < this8 tb"9oc3 < 9oc34t/le")ill8 tb"Multiline < true8 4tatus2ar sb < ne. 4tatus2ar()8 sb"'arent < this8 sb"Te;t < =1ea#/=8 ? CenterTo4creen()8 oi# Dn7;it(ob*ect sen#er, 7 ent6rgs e) : Close()8

? ?

class M6pplication : public static oi# Main() : 6pplication"1un(ne. M)orm())8 ? ?

"e s o, a menubar and a statusbar$ And t e remaining area is taken by t e Text"ox control$
Te;t2o; tb < ne. Te;t2o;()8 tb"'arent < this8

=ere ,e create t e Text"ox control$ Form container is set to be t e parent #or t e te)t bo)$
tb"9oc3 < 9oc34t/le")ill8

T is code line makes t e Text"ox control take up t e remaining space inside t e #orm container$

Figure2 /ditor skeleton

Anchored buttons
T e ne)t e)ample s o,s t,o buttons placed in t e bottom rig t corner o# t e #orm$
anchore#buttons"cs using 4/stem8 using 4/stem"9ra.ing8 using 4/stem"+in#o.s")orms8 class M)orm , )orm : pri pri pri pri pri ate ate ate ate ate int int int int int +!9T5 < 2>08 57!%5T < 1>08 2&TTDN4G4'6C7 < 1>8 '6N7(G4'6C7 < 88 C(D47G4'6C7 < 108

public M)orm() : Te;t < =6nchor=8 4i0e < ne. 4i0e(+!9T5, 57!%5T)8 2utton o3 < ne. 2utton()8 int '6N7(G57!%5T < o3"5eight C '6N7(G4'6C78 'anel panel < ne. 'anel()8 panel"5eight < '6N7(G57!%5T8 panel"9oc3 < 9oc34t/le"2ottom8 panel"'arent < this8 int ; < o3"+i#th H 2 C 2&TTDN4G4'6C78 int / < ('6N7(G57!%5T - o3"5eight) - 28 o3"Te;t < =D3=8 o3"'arent < panel8 o3"(ocation < ne. 'oint(+!9T5-;, /)8 o3"6nchor < 6nchor4t/les"1ight8 2utton close < ne. 2utton()8

; < close"+i#th8 close"Te;t < =Close=8 close"'arent < panel8 close"(ocation < ne. 'oint(+!9T5-;-C(D47G4'6C7, /)8 close"6nchor < 6nchor4t/les"1ight8 ? ? class M6pplication : public static oi# Main() : 6pplication"1un(ne. M)orm())8 ? ? CenterTo4creen()8

T e e)ample displayes 1'+ Close buttons in t e bottom rig t corner o# t e ,indo,+ as it is common in dialog ,indo,s$
pri ate int +!9T5 < 2>08 pri ate int 57!%5T < 1>08

T e WI+T, and ,&I-,T variables determine t e ,idt and eig t o# t e application ,indo,$
pri ate int 2&TTDN4G4'6C7 < 1>8 pri ate int '6N7(G4'6C7 < 88 pri ate int C(D47G4'6C7 < 108

T e ".TT'NS/S$AC& is t e space bet,een t e 1' and t e Close button$ T e $AN&#/S$AC& is t e space bet,een t e panel and t e bottom o# t e #orm$ Finally+ t e C#'S&/S$AC& variable sets t e space bet,een t e Close button and t e rig t border o# t e #orm$
int '6N7(G57!%5T < o3"5eight C '6N7(G4'6C78

=ere ,e compute t e eig t o# t e panel$ T e eig t o# t e panel is based on t e eig t o# t e 1' button$ And ,e add some additional space+ so t at t e buttons are not too close to t e border$
'anel panel < ne. 'anel()8 panel"5eight < '6N7(G57!%5T8 panel"9oc3 < 9oc34t/le"2ottom8 panel"'arent < this

=ere ,e create and manage t e $anel control$ In t is e)ample+ it is used as a container #or our buttons$ It is glued to t e bottom border o# t e #orm$ And t e buttons are placed ,it in t e panel$
o3"Te;t < =D3=8 o3"'arent < panel8 o3"(ocation < ne. 'oint(+!9T5-;, /)8 o3"6nchor < 6nchor4t/les"1ight8

T e 1' button7s parent is set to t e panel ,idget$ T e location is computed$ And t e Anchor property is set to t e rig t$ T e ot er button is created similarly$

Figure2 Anc ored buttons

$la*er s%eleton
T e last e)ample o# t is part o# t e Mono "in#orms tutorial s o,s a more comple) e)ample$ It is a skeleton o# a music player$
pla/er"cs using 4/stem8 using 4/stem"9ra.ing8 using 4/stem"+in#o.s")orms8 class M)orm , )orm : public M)orm() : Te;t < ='la/er=8 4i0e < ne. 4i0e(@>0, 280)8 MainMenu mainMenu < ne. MainMenu()8 Menu!tem $ile < mainMenu"Menu!tems"6##(=E)ile=)8 Menu!tem pla/m < mainMenu"Menu!tems"6##(=E'la/=)8 Menu!tem ie. < mainMenu"Menu!tems"6##(=EIie.=)8 Menu!tem tools < mainMenu"Menu!tems"6##(=ETools=)8 Menu!tem $a ourites < mainMenu"Menu!tems"6##(=E)a ourites=)8 Menu!tem help < mainMenu"Menu!tems"6##(=E5elp=)8 $ile"Menu!tems"6##(ne. Menu!tem(=7E;it=, ne. 7 ent5an#ler(this"Dn7;it), 4hortcut"CtrlF))8 Menu < mainMenu8 'anel panel < ne. 'anel()8 panel"'arent < this8 panel"2ac3Color < Color"2lac38 panel"9oc3 < 9oc34t/le")ill8 'anel button'anel < ne. 'anel()8 button'anel"'arent < this8 button'anel"5eight < J08 button'anel"9oc3 < 9oc34t/le"2ottom8 2utton pause < ne. 2utton()8 pause")lat4t/le < )lat4t/le"'opup8 pause"'arent < button'anel8 pause"(ocation < ne. 'oint(>, 10)8 pause"4i0e < ne. 4i0e(2>, 2>)8 pause"!mage < ne. 2itmap(=pause"png=)8

2utton pla/ < ne. 2utton()8 pla/")lat4t/le < )lat4t/le"'opup8 pla/"'arent < button'anel8 pla/"(ocation < ne. 'oint(@>, 10)8 pla/"4i0e < ne. 4i0e(2>, 2>)8 pla/"!mage < ne. 2itmap(=pla/"png=)8 2utton $or.ar# < ne. 2utton()8 $or.ar#")lat4t/le < )lat4t/le"'opup8 $or.ar#"'arent < button'anel8 $or.ar#"(ocation < ne. 'oint(80, 10)8 $or.ar#"4i0e < ne. 4i0e(2>, 2>)8 $or.ar#"!mage < ne. 2itmap(=$or.ar#"png=)8 2utton bac3.ar# < ne. 2utton()8 bac3.ar#")lat4t/le < )lat4t/le"'opup8 bac3.ar#"'arent < button'anel8 bac3.ar#"(ocation < ne. 'oint(110, 10)8 bac3.ar#"4i0e < ne. 4i0e(2>, 2>)8 bac3.ar#"!mage < ne. 2itmap(=bac3.ar#"png=)8 Trac32ar tb < ne. Trac32ar()8 tb"'arent < button'anel8 tb"Tic34t/le < Tic34t/le"None8 tb"4i0e < ne. 4i0e(1>0, 2>)8 tb"(ocation < ne. 'oint(200, 10)8 tb"6nchor < 6nchor4t/les"1ight8 2utton au#io < ne. 2utton()8 au#io")lat4t/le < )lat4t/le"'opup8 au#io"'arent < button'anel8 au#io"4i0e < ne. 4i0e(2>, 2>)8 au#io"!mage < ne. 2itmap(=au#io"png=)8 au#io"(ocation < ne. 'oint(1A0, 10)8 au#io"6nchor < 6nchor4t/les"1ight8 4tatus2ar sb < ne. 4tatus2ar()8 sb"'arent < this8 sb"Te;t < =1ea#/=8 CenterTo4creen()8 ? oi# Dn7;it(ob*ect sen#er, 7 ent6rgs e) : Close()8 ? ?

class M6pplication : public static oi# Main() : 6pplication"1un(ne. M)orm())8 ? ?

T is is a more comple) e)ample s o,ing bot +oc% and Anchor properties in action$
MainMenu mainMenu < ne. MainMenu()8 Menu!tem $ile < mainMenu"Menu!tems"6##(=E)ile=)8 """ Menu < mainMenu8

=ere ,e create t e menubar$

'anel panel < ne. 'anel()8 panel"'arent < this8 panel"2ac3Color < Color"2lac38 panel"9oc3 < 9oc34t/le")ill8

T is is t e black panel+ , ic takes all t e remaining space+ le#t by t e menubar+ statusbar and t e control panel$
'anel button'anel < ne. 'anel()8 button'anel"'arent < this8 button'anel"5eight < J08 button'anel"9oc3 < 9oc34t/le"2ottom8

T is is t e control panel$ Its parent is t e #orm container$ It is glued to t e bottom o# t e #orm$ Its eig t is G;p)$ Inside t is control panel+ ,e place all t e buttons and t e trackar$
2utton pause < ne. 2utton()8 pause")lat4t/le < )lat4t/le"'opup8 pause"'arent < button'anel8 pause"(ocation < ne. 'oint(>, 10)8 pause"4i0e < ne. 4i0e(2>, 2>)8 pause"!mage < ne. 2itmap(=pause"png=)8

T e pause button is one o# t e #our buttons+ t at as t e de#ault Anchor property value$ T e style o# t e button is set to #lat+ because it looks better$ "e put a bitmap on t e button$
tb"6nchor < 6nchor4t/les"1ight8 """ au#io"6nchor < 6nchor4t/les"1ight8

T e last t,o controls are anc ored to t e rig t$

Figure2 Player skeleton T is part o# t e Mono "in#orms tutorial ,as about t e layout management o# controls$ "e practised various possibilities t at t e "in#orms library o##ers$

Menus and toolbars in Mono Winforms


In t is part o# t e Mono "in#orms tutorial+ ,e ,ill talk about menus and toolbars$ A menubar is one o# t e most visible parts o# t e &(I application$ It is a group o# commands located in various menus$ " ile in console applications you ad to remember all t ose arcane commands+ ere ,e ave most o# t e commands grouped into logical parts$ T ere are accepted standards t at #urt er reduce t e amount o# time spending to learn a ne, application$

Simple menu
In t e #irst e)ample+ ,e create a simple menu$
simplemenu"cs using 4/stem8 using 4/stem"9ra.ing8 using 4/stem"+in#o.s")orms8 class M)orm , )orm : public M)orm() : Te;t < =4imple menu=8 Menu4trip ms < ne. Menu4trip()8 ms"'arent < this8 Tool4tripMenu!tem $ile < ne. Tool4tripMenu!tem(=E)ile=)8 Tool4tripMenu!tem e;it < ne. Tool4tripMenu!tem(=E7;it=, null, ne. 7 ent5an#ler(Dn7;it))8 e;it"4hortcutKe/s < Ke/s"Control L Ke/s"F8 $ile"9rop9o.n!tems"6##(e;it)8 ms"!tems"6##($ile)8 MainMenu4trip < ms8 4i0e < ne. 4i0e(2>0, 200)8 ? CenterTo4creen()8 oi# Dn7;it(ob*ect sen#er, 7 ent6rgs e) : Close()8

? ?

class M6pplication : public static oi# Main() : 6pplication"1un(ne. M)orm())8 ? ?

In our e)ample+ ,e ave a menubar and one menu$ Inside a menu t ere is one menu item$ I# ,e select t e menu item+ application is closed$ *otice t e ,ays o, ,e can close t e application$ "e can close it by using t e CtrlH. s orcut or by pressing Alt+ F+ / keys$
Menu4trip ms < ne. Menu4trip()8

MenuStrip creates a menu system #or our #orm$ "e add ToolStripMenuItem ob-ects to t e MenuStrip t at represent t e individual menu commands in t e menu structure$ /ac ToolStripMenuItem can be a command #or your application or a parent menu #or ot er submenu

items$
Tool4tripMenu!tem $ile < ne. Tool4tripMenu!tem(=E)ile=)8

=ere ,e create a menu$


Tool4tripMenu!tem e;it < ne. Tool4tripMenu!tem(=E7;it=, null, ne. 7 ent5an#ler(Dn7;it))8

T is line creates t e e)it menu item$


e;it"4hortcutKe/s < Ke/s"Control L Ke/s"F8

"e provide a s ortcut #or t e e)it menu item$


$ile"9rop9o.n!tems"6##(e;it)8

T e e)it menu item is added to t e drop do,n items o# t e menu ob-ect$


ms"!tems"6##($ile)8

=ere ,e add t e menu ob-ect into t e menu strip$


MainMenu4trip < ms8

T e MenuStrip is plugged into t e #orm$

Figure2 Simple menu

Submenu
/ac menu item can also ave a submenu$ T is ,ay ,e can group similar commnads into groups$ For e)ample ,e can place commands t at ideBs o, various toolbars like personal bar+ address bar+ status bar or navigation bar into a submenu called toolbars$
submenu"cs using 4/stem8 using 4/stem"9ra.ing8 using 4/stem"+in#o.s")orms8 class M)orm , )orm : public M)orm() : Te;t < =4ubmenu=8

Menu4trip ms < ne. Menu4trip()8 ms"'arent < this8 Tool4tripMenu!tem $ile < ne. Tool4tripMenu!tem(=E)ile=)8 Tool4tripMenu!tem e;it < ne. Tool4tripMenu!tem(=E7;it=, null, ne. 7 ent5an#ler(Dn7;it))8 Tool4tripMenu!tem import < ne. Tool4tripMenu!tem()8 import"Te;t < =!mport=8 $ile"9rop9o.n!tems"6##(import)8 Tool4tripMenu!tem temp < ne. Tool4tripMenu!tem()8 temp"Te;t < =!mport ne.s$ee# list"""=8 import"9rop9o.n!tems"6##(temp)8 temp < ne. Tool4tripMenu!tem()8 temp"Te;t < =!mport boo3mar3s"""=8 import"9rop9o.n!tems"6##(temp)8 temp < ne. Tool4tripMenu!tem()8 temp"Te;t < =!mport mail"""=8 import"9rop9o.n!tems"6##(temp)8 $ile"9rop9o.n!tems"6##(e;it)8 ms"!tems"6##($ile)8 MainMenu4trip < ms8 4i0e < ne. 4i0e(@80, 200)8 CenterTo4creen()8 ? oi# Dn7;it(ob*ect sen#er, 7 ent6rgs e) : Close()8 ? ?

class M6pplication : public static oi# Main() : 6pplication"1un(ne. M)orm())8 ? ?

In t is e)ample+ ,e create one submenu$ T e submenu Import as t ree menu items$


Tool4tripMenu!tem import < ne. Tool4tripMenu!tem()8 import"Te;t < =!mport=8

A ToolStripMenuItem can be a menu or a menu item$ =ere it ,ill act as a submenu$


Tool4tripMenu!tem temp < ne. Tool4tripMenu!tem()8 temp"Te;t < =!mport ne.s$ee# list"""=8 import"9rop9o.n!tems"6##(temp)8

=ere ,e create a menu item and add it to t e Import submenu$

Figure2 Submenu

Chec% menu item


T e ne)t code e)ample demonstrates+ o, to create a c ecked menu item$
chec3menuitem"cs using 4/stem8 using 4/stem"9ra.ing8 using 4/stem"+in#o.s")orms8 class M)orm , )orm : pri ate 4tatus2ar sb8 pri ate Menu!tem ie.4tatus2ar8 public M)orm() : Te;t < =Chec3 menu item=8 sb < ne. 4tatus2ar()8 sb"'arent < this8 sb"Te;t < =1ea#/=8 MainMenu mainMenu < ne. MainMenu()8 Menu!tem $ile < mainMenu"Menu!tems"6##(=E)ile=)8 $ile"Menu!tems"6##(ne. Menu!tem(=7E;it=, ne. 7 ent5an#ler(Dn7;it), 4hortcut"CtrlF))8 Menu!tem ie. < mainMenu"Menu!tems"6##(=EIie.=)8 ie.4tatus2ar < ne. Menu!tem(=Iie. 4tatus2ar=)8 ie.4tatus2ar"Chec3e# < true8 ie.4tatus2ar"Clic3 C< ne. 7 ent5an#ler(Toggle4tatus2ar)8 ie."Menu!tems"6##( ie.4tatus2ar)8 Menu < mainMenu8 4i0e < ne. 4i0e(2>0, 200)8 ? CenterTo4creen()8 oi# Dn7;it(ob*ect sen#er, 7 ent6rgs e) : Close()8 oi# Toggle4tatus2ar(ob*ect sen#er, 7 ent6rgs e) :

? ?

bool chec3 < ie.4tatus2ar"Chec3e#8 i$ (chec3) : sb"Iisible < $alse8 ie.4tatus2ar"Chec3e# < $alse8 ? else : sb"Iisible < true8 ie.4tatus2ar"Chec3e# < true8 ?

class M6pplication : public static oi# Main() : 6pplication"1un(ne. M)orm())8 ? ?

"e ave t,o menus$ File and 4ie,$ T e 4ie, menu as a menu item t at toggles t e visibility o# t e statusbar$
MainMenu mainMenu < ne. MainMenu()8

In t is e)ample+ ,e use t e MainMenu control$ To create a menubar+ ,e can use eit er MainMenu or MenuStrip controls$ T e latter as some additional #unctionality$
ie.4tatus2ar"Chec3e# < true8

T is menu item is c ecked by de#ault+ because t e statusbar is visible #rom t e start o# t e application$
bool chec3 < ie.4tatus2ar"Chec3e#8 i$ (chec3) : sb"Iisible < $alse8 ie.4tatus2ar"Chec3e# < $alse8 ? else : sb"Iisible < true8 ie.4tatus2ar"Chec3e# < true8 ?

"e determine+ i# t e menu item is c ecked$ "e s o, and ide t e statusbar and t e c eck tick depending on t e chec% value$

Figure2 C eck menu item

Images0 separator
"e ,ill #urt er en ance our kno,ledge o# t e MenuStrip control$ "e ,ill create a menu item ,it an image and s o, o, to separate t em ,it a separator$
menustrip"cs using 4/stem8 using 4/stem"9ra.ing8 using 4/stem"+in#o.s")orms8 class M)orm , )orm : public M)orm() : Te;t < =Menu4trip=8 4i0e < ne. 4i0e(2>0, 200)8 Menu4trip menu4trip < ne. Menu4trip()8 Tool4tripMenu!tem titem1 < ne. Tool4tripMenu!tem(=)ile=)8 menu4trip"!tems"6##(titem1)8 Tool4tripMenu!tem titem2 < ne. Tool4tripMenu!tem(=Tools=)8 menu4trip"!tems"6##(titem2)8 Tool4tripMenu!tem subm1 < ne. Tool4tripMenu!tem(=Ne.=)8 subm1"!mage < !mage")rom)ile(=ne."png=)8 titem1"9rop9o.n!tems"6##(subm1)8 Tool4tripMenu!tem subm2 < ne. Tool4tripMenu!tem(=Dpen=)8 subm2"!mage < !mage")rom)ile(=open"png=)8 titem1"9rop9o.n!tems"6##(subm2)8 titem1"9rop9o.n!tems"6##(ne. Tool4trip4eparator())8 Tool4tripMenu!tem subm@ < ne. Tool4tripMenu!tem(=7;it=)8 subm@"!mage < !mage")rom)ile(=e;it"png=)8 titem1"9rop9o.n!tems"6##(subm@)8 subm@"Clic3 C< Dn7;it8 Controls"6##(menu4trip)8 MainMenu4trip < menu4trip8 CenterTo4creen()8 ? public static oi# Main() : 6pplication"1un(ne. M)orm())8 ? oi# Dn7;it(ob*ect sen#er, 7 ent6rgs e) : Close()8 ? ?

"e ave t,o menus in our code e)ample$ File and Tools$ In t e File ,e ave t ree menu items ,it images$ "e ave also one separator$ In t is e)ample+ t e png images must be located in t e current ,orking directory$
Tool4tripMenu!tem subm1 < ne. Tool4tripMenu!tem(=Ne.=)8 subm1"!mage < !mage")rom)ile(=ne."png=)8

titem1"9rop9o.n!tems"6##(subm1)8

=ere ,e create t e #irst menu item$ To add an image to t e item+ ,e set t e Image property to our image$ "e create an Image #rom t e speci#ied #ile using t e static FromFile() met od$
titem1"9rop9o.n!tems"6##(ne. Tool4trip4eparator())8

=ere ,e add a separator to t e File menu$

Figure2 Images and separator

Tool"ar
Menus group all commands t at ,e can use in an application$ Toolbars provide a >uick access to t e most #re>uently used commands$ Tool"ar control is used to display Tool"ar"utton controls$ "e can assign images to t e buttons by creating an Image#ist$ "e t an assign t e image list to t e Image#ist property o# t e toolbar and assign t e image inde) value to t e ImageIndex property #or eac Tool"ar"utton$
toolbar"cs using 4/stem8 using 4/stem"9ra.ing8 using 4/stem"+in#o.s")orms8 public class M)orm , )orm : pri ate !mage(ist tool2ar!cons8 pri ate Tool2ar2utton sa e8 pri ate Tool2ar2utton e;it8 pri ate Tool2ar tool2ar8 public M)orm() : 4i0e < ne. 4i0e(2>0, 200)8 Te;t < =4imple toolbar=8 tool2ar < ne. Tool2ar()8 tool2ar"'arent < this8 tool2ar!cons < ne. !mage(ist()8 sa e < ne. Tool2ar2utton()8 e;it < ne. Tool2ar2utton()8 sa e"!mage!n#e; < 08 sa e"Tag < =4a e=8 e;it"!mage!n#e; < 18

e;it"Tag < =7;it=8 tool2ar"!mage(ist < tool2ar!cons8 tool2ar"4ho.ToolTips < true8 tool2ar"2uttons"6##1ange(ne. Tool2ar2uttonMN :sa e, e;it?)8 tool2ar"2uttonClic3 C< ne. Tool2ar2uttonClic37 ent5an#ler(DnClic3e#)8 tool2ar!cons"!mages"6##(ne. !con(=ne."ico=))8 tool2ar!cons"!mages"6##(ne. !con(=e;it"ico=))8 CenterTo4creen()8 ? static oi# Main() : 6pplication"1un(ne. M)orm())8 ? oi# DnClic3e#(ob*ect sen#er, Tool2ar2uttonClic37 ent6rgs e) : ? ? i$ (e"2utton"Tag"7Ouals(=7;it=)) Close()8

In our e)ample+ ,e s o, t,o buttons on t e toolbar$


tool2ar < ne. Tool2ar()8

=ere ,e create t e Tool"ar control$


tool2ar!cons < ne. !mage(ist()8

An image list is created$


sa e < ne. Tool2ar2utton()8 e;it < ne. Tool2ar2utton()8

T ese are t,o toolbar buttons$


sa e"!mage!n#e; < 08

"e determine , ic icon #rom t e image list ,ill be used #or t e save toolbar button$
tool2ar"2uttons"6##1ange(ne. Tool2ar2uttonMN :sa e, e;it?)8

T e Tool"ar"utton controls are added to t e toolbar$


tool2ar!cons"!mages"6##(ne. !con(=ne."ico=))8 tool2ar!cons"!mages"6##(ne. !con(=e;it"ico=))8

Icons are added to t e image list$


i$ (e"2utton"Tag"7Ouals(=7;it=)) Close()8

I# t e tag o# t e button e>uals to ?/)it?+ ,e close t e application$

Figure2 Tool3ar T is part o# t e "in#orms tutorial ,as about menus and toolbars$

"asic Controls in Mono Winforms


T is part o# t e Mono "in#orms programming tutorial ,ill be about basic controls$ "in#orms controls are basic building blocks o# an application$ "in#orms as a ,ide range o# various controls$ 3uttons+ c eck bo)es+ sliders+ list bo)es etc$ /veryt ing a programmer needs #or is -ob$ In t is section o# t e tutorial+ ,e ,ill describe several use#ul controls$

#abel Control
#abel is a simple control #or displaying te)t or images$ It does not receive #ocus$
label"cs using 4/stem8 using 4/stem"9ra.ing8 using 4/stem"+in#o.s")orms8 class M)orm , )orm : string te;t < P=4ometimes ! $eel !Q e got to 1un a.a/ !Q e got to %et a.a/ )rom the pain that /ou #ri e into the heart o$ me The lo e .e share 4eems to go no.here !Q e lost m/ lights ! toss an# turn ! canQt sleep at night Dnce ! ran to /ou (! ran) No. !Qll run $rom /ou This tainte# lo e /ouQ e gi en ! gi e /ou all a bo/ coul# gi e /ou Ta3e m/ tears an# thatQs not nearl/ all Tainte# lo e Tainte# lo e=8 public M)orm() : Te;t < =Tainte# (o e=8 )ont $ont < ne. )ont(=4eri$=, 10)8 (abel l/rics < ne. (abel()8

l/rics"'arent < this8 l/rics"Te;t < te;t8 l/rics")ont < $ont8 l/rics"(ocation < ne. 'oint(10, 10)8 l/rics"4i0e < ne. 4i0e (2R0, 2R0)8 CenterTo4creen()8 ? ? class M6pplication : public static oi# Main() : 6pplication"1un(ne. M)orm())8 ? ?

In our e)ample+ ,e s o, lyrics o# Tainted Love song$


(abel l/rics < ne. (abel()8

#abel control is created$


string te;t < P=4ometimes ! $eel !Q e got """

T e I c aracter is used to denote a multiline string$


)ont $ont < ne. )ont(=4eri$=, 10)8 """ l/rics")ont < $ont8

T e #ont o# t e te)t o# t e label is set to Seri#+ 8;p)$

Figure2 Label

Chec%"ox
Chec%"ox is a control t at as t,o states$ 1n and 1##$ It is a bo) ,it a label or an image$ I# t e C eck3o) is c ecked+ it is represented by a tick in a bo)$ A C eck3o) can be used to s o,B ide splas screen at startup+ toggle visibility o# a toolbar etc$
chec3bo;"cs using 4/stem8 using 4/stem"9ra.ing8 using 4/stem"+in#o.s")orms8 class M)orm , )orm : pri ate Chec32o; cb8 public M)orm() : Te;t < =Chec32o;=8 4i0e < ne. 4i0e(220, 1A0)8 cb < ne. Chec32o;()8 cb"'arent < this8 cb"(ocation < ne. 'oint(@0, @0)8 cb"Te;t < =4ho. Title=8 cb"Chec3e# < true8 cb"Chec3e#Change# C< ne. 7 ent5an#ler(DnChange#)8 CenterTo4creen()8 ? oi# DnChange#(ob*ect sen#er, 7 ent6rgs e) : i$ (cb"Chec3e#) : Te;t < =Chec32o;=8 ? else : Te;t < ==8 ? ? ?

class M6pplication : public static oi# Main() : 6pplication"1un(ne. M)orm())8 ? ?

1ur code e)ample s o,s or ides t e title o# t e ,indo, depending on it7s state$
cb < ne. Chec32o;()8

Chec%"ox control is created$


cb"Te;t < =4ho. Title=8 cb"Chec3e# < true8

" en t e application starts+ ,e s o, t e title$ And ,e set t e Chec%"ox control to c ecked state$
cb"Chec3e#Change# C< ne. 7 ent5an#ler(DnChange#)8

" en ,e click on t e Chec%"ox control+ t e Chec%edChanged event is triggered$


i$ (cb"Chec3e#) : Te;t < =Chec32o;=8

? else : Te;t < ==8 ?

=ere ,e toggle t e title o# t e ,indo,$

Figure2 C eck3o)

Trac%"ar
Trac%"ar is a component t at lets t e user grap ically select a value by sliding a knob ,it in a bounded interval$ 1ur e)ample ,ill s o, a volume control$
trac3bar"cs using 4/stem8 using 4/stem"9ra.ing8 using 4/stem"+in#o.s")orms8 class M)orm , )orm : 'icture2o; pb8 Trac32ar tb8 2itmap mute, min, me#, ma;8 public M)orm() : Te;t < =Trac32ar=8 4i0e < ne. 4i0e(2S0, 1R0)8 tb < ne. Trac32ar()8 tb"'arent < this8 tb"4i0e < ne. 4i0e(1S0, @0)8 tb"(ocation < ne. 'oint(20, J0)8 tb"Tic34t/le < Tic34t/le"None8 tb"IalueChange# C< ne. 7 ent5an#ler(DnChange#)8 (oa#!mages()8 pb < ne. 'icture2o;()8 pb"'arent < this8 pb"(ocation < ne. 'oint(210, >0)8 pb"!mage < mute8 CenterTo4creen()8 ?

oi# (oa#!mages() : mute < ne. 2itmap(=mute"png=)8 min < ne. 2itmap(=min"png=)8 me# < ne. 2itmap(=me#"png=)8 ma; < ne. 2itmap(=ma;"png=)8 ? oi# DnChange#(ob*ect sen#er, 7 ent6rgs e) : int al < tb"Ialue8 i$ ( al << 0) : pb"!mage < mute8 ? else i$ ( al T 0 EE pb"!mage < min8 ? else i$ ( al T @ EE pb"!mage < me#8 ? else : pb"!mage < ma;8 ?

al U< @) : al U 8) :

? ?

class M6pplication : public static oi# Main() : 6pplication"1un(ne. M)orm())8 ? ?

In t e code e)ample+ ,e s o, a Trac%"ar and a $icture"ox$ 3y dragging t e track bar+ ,e c ange t e image on t e $icture"ox control$
tb < ne. Trac32ar()8

Trac%"ar control is created$


tb"Tic34t/le < Tic34t/le"None8

"e s o, no ticks #or t is Trac%"ar$


pb < ne. 'icture2o;()8 """ pb"!mage < mute8

Picture3o) control is created$ It is used to display an image$ At t e start+ it s o,s t e mute image$
oi# (oa#!mages() : mute < ne. 2itmap(=mute"png=)8 min < ne. 2itmap(=min"png=)8 me# < ne. 2itmap(=me#"png=)8 ma; < ne. 2itmap(=ma;"png=)8 ?

=ere ,e load #our images+ t at ,e ,ill use$


int al < tb"Ialue8 i$ ( al << 0) : pb"!mage < mute8 ? else i$ ( al T 0 EE pb"!mage < min8 ? else i$ ( al T @ EE pb"!mage < me#8

al U< @) : al U 8) :

? else : pb"!mage < ma;8 ?

"e determine t e value o# t e Trac%"ar$ Depending on its value+ ,e update t e $icture"ox control$

Figure2 Track3ar

Combo"ox
Combo"ox is a control t at combines a button or editable #ield and a dropCdo,n list$ T e user can select a value #rom t e dropCdo,n list+ , ic appears at t e user7s re>uest$ I# you make t e combo bo) editable+ t en t e combo bo) includes an editable #ield into , ic t e user can type a value$
combobo;"cs using 4/stem8 using 4/stem"9ra.ing8 using 4/stem"+in#o.s")orms8 class M)orm , )orm : pri ate Combo2o; cb8 pri ate (abel label8 public M)orm() : Te;t < =Combo2o;=8 4i0e < ne. 4i0e(2J0, 2J0)8 cb < ne. Combo2o;()8 cb"'arent < this8 cb"(ocation < ne. 'oint(>0, @0)8 cb"!tems"6##1ange(ne. ob*ectMN :=&buntu=, =Man#ri a=, =1e# 5at=, =)e#ora=, =%entoo=?)8 cb"4electionChangeCommitte# C< ne. 7 ent5an#ler(DnChange#)8 label < ne. (abel()8 label"(ocation < ne. 'oint(>0, 1J0)8 label"'arent < this8 label"Te;t < ="""=8 CenterTo4creen()8 ?

? ?

oi# DnChange#(ob*ect sen#er, 7 ent6rgs e) : Combo2o; combo < (Combo2o;) sen#er8 label"Te;t < combo"Te;t8

class M6pplication : public static oi# Main() : 6pplication"1un(ne. M)orm())8 ? ?

1ur code programming e)ample s o,s a combobo) ,it #ive items$ T e selected item is s o,n in a label control$
cb < ne. Combo2o;()8

Combo"ox control is created$


cb"!tems"6##1ange(ne. ob*ectMN :=&buntu=, =Man#ri a=, =1e# 5at=, =)e#ora=, =%entoo=?)8

T e Combo"ox control is #illed ,it items$


cb"4electionChangeCommitte# C< ne. 7 ent5an#ler(DnChange#)8

I# ,e select an item #rom t e combobo)+ t e SelectionChangeCommitted event is triggered$


oi# DnChange#(ob*ect sen#er, 7 ent6rgs e) : Combo2o; combo < (Combo2o;) sen#er8 label"Te;t < combo"Te;t8 ?

=ere t e selected te)t #rom t e combobo) is copied to t e label$

Figure2 Combo3o)

MonthCalendar
In t e ne)t e)ample+ ,e ,ill s o, a MonthCalendar control$ T e Mont Calendar control allo,s t e user to select a date using a visual display$
monthcalen#ar"cs using 4/stem8 using 4/stem"9ra.ing8 using 4/stem"+in#o.s")orms8 class M)orm , )orm : pri ate MonthCalen#ar calen#ar8 pri ate (abel #ate8 public M)orm() : Te;t < =Month Calen#ar=8 4i0e < ne. 4i0e(2J0, 2J0)8 calen#ar < ne. MonthCalen#ar()8 calen#ar"'arent < this8 calen#ar"(ocation < ne. 'oint(20, 20)8 calen#ar"9ate4electe# C< ne. 9ate1ange7 ent5an#ler(Dn4electe#)8 #ate < ne. (abel()8 #ate"(ocation < ne. 'oint(J0, 1A0)8 #ate"'arent < this8 9ateTime #t < calen#ar"4election4tart8 #ate"Te;t < #t"Month C =-= C #t"9a/ C =-= C #t"Vear8 CenterTo4creen()8 ? oi# Dn4electe#(ob*ect sen#er, 7 ent6rgs e) : 9ateTime #t < calen#ar"4election4tart8 #ate"Te;t < #t"Month C =-= C #t"9a/ C =-= C #t"Vear8

? ?

class M6pplication : public static oi# Main() : 6pplication"1un(ne. M)orm())8 ? ?

In t e e)ample+ ,e s o, a MonthCalendar and a #abel$


pri ate MonthCalen#ar calen#ar8 pri ate (abel #ate8

"e ave t,o controls$ A MonthCalendar and a #abel$ T e latter s o,s t e currently selected date$
oi# Dn4electe#(ob*ect sen#er, 7 ent6rgs e) : 9ateTime #t < calen#ar"4election4tart8 #ate"Te;t < #t"Month C =-= C #t"9a/ C =-= C #t"Vear8 ?

" en ,e select a date #rom t e MonthCalendar+ t e 'nSelected() met od is called$ T e SelectionStart property gets t e start date o# t e selected range o# dates$

Figure2 Mont Calendar

Text"ox
T e Text"ox control is used to display or accept some te)t$ T e te)t can be single or multiline$ T is control is also capable o# pass,ord masking$
te;tbo;"cs using 4/stem8 using 4/stem"9ra.ing8 using 4/stem"+in#o.s")orms8 class M)orm , )orm : pri ate (abel te;t8 public M)orm() : Te;t < =Te;t2o;=8 4i0e < ne. 4i0e(2>0, 200)8 CenterTo4creen()8 te;t < ne. (abel()8 te;t"'arent < this8 te;t"Te;t < ="""=8 te;t"(ocation < ne. 'oint(S0, J0)8 te;t"6uto4i0e < true8 Te;t2o; tbo; < ne. Te;t2o;()8 tbo;"'arent < this8 tbo;"(ocation < ne. 'oint(S0, 100)8 tbo;"Ke/&p C< ne. Ke/7 ent5an#ler(DnKe/&p)8 ? oi# DnKe/&p(ob*ect sen#er, Ke/7 ent6rgs e) : Te;t2o; tb < (Te;t2o;) sen#er8 this"te;t"Te;t < tb"Te;t8

? ?

class M6pplication : public static oi# Main() : 6pplication"1un(ne. M)orm())8 ? ?

T is e)ample s o,s a te)t bo) and a label$ T e te)t t at ,e key in t e te)t bo) is displayed immediately in t e label control$
te;t < ne. (abel()8 """ te;t"6uto4i0e < true8

T e #abel control is created$ T e AutoSi e property ensures+ t at t e Label gro,s to s o, t e te)t$


Te;t2o; tbo; < ne. Te;t2o;()8 """ tbo;"Ke/&p C< ne. Ke/7 ent5an#ler(DnKe/&p)8

"e plug in t e 1e*.p event$ " en ,e release t e key+ 'n1e*.p() met od is called$
oi# DnKe/&p(ob*ect sen#er, Ke/7 ent6rgs e) : Te;t2o; tb < (Te;t2o;) sen#er8 this"te;t"Te;t < tb"Te;t8 ?

In t e 'n1e*.p() met od ,e update t e label control ,it t e te)t #rom t e te)t bo) control$

Figure2 Te)t3o) "e ave #inis ed c apter o# t e Mono "in#orms tutorial+ dedicated to basic controls$

Advanced Controls in Mono Winforms


In t is part o# t e Mono "in#orms tutorial+ ,e introduce some more advanced controls$ *amely t e #ist"ox+ t e #ist2ie3 and t e Tree2ie3 control$

#ist"ox Control
T e #ist"ox control is used to display a list o# items$ (sers can select one or more items by clicking on t em$
listbo;"cs using 4/stem8 using 4/stem"9ra.ing8 using 4/stem"+in#o.s")orms8 class M)orm , )orm : pri ate 4tatus2ar sb8

public M)orm() : Te;t < =(ist2o;=8 4i0e < ne. 4i0e(210, 210)8 (ist2o; lb < ne. (ist2o;()8 lb"'arent < this8 lb"!tems"6##(=Wessica=)8 lb"!tems"6##(=1achel=)8 lb"!tems"6##(=6ngelina=)8 lb"!tems"6##(=6m/=)8 lb"!tems"6##(=Wenni$er=)8 lb"!tems"6##(=4carlett=)8 lb"9oc3 < 9oc34t/le")ill8 lb"4electe#!n#e;Change# C< ne. 7 ent5an#ler(DnChange#)8 sb < ne. 4tatus2ar()8 sb"'arent < this8 ? CenterTo4creen()8

? ?

oi# DnChange#(ob*ect sen#er, 7 ent6rgs e) : (ist2o; lb < ((ist2o;) sen#er8 sb"Te;t < lb"4electe#!tem"To4tring()8

class M6pplication : public static oi# Main() : 6pplication"1un(ne. M)orm())8 ? ?

1ur e)ample s o,s a listbo) ,it si) names$ T e selected item is s o,n in t e statusbar$
(ist2o; lb < ne. (ist2o;()8 lb"'arent < this8

#ist"ox control is created$


lb"!tems"6##(=Wessica=)8

T is is o, ,e add a ne, item to t e #ist"ox control$ T e control as t e Items property$ T e property is a re#erence to t e list o# items in a listbo)$ (sing t is re#erence+ ,e can add+ remove or get count o# items o# t e listbo)$
lb"4electe#!n#e;Change# C< ne. 7 ent5an#ler(DnChange#)8

SelectedIndexChanged event is triggered+ , en ,e select an item$


(ist2o; lb < ((ist2o;) sen#er8 sb"Te;t < lb"4electe#!tem"To4tring()8

Inside t e 'nChange() met od+ ,e get t e re#erence to t e listbo) and set t e selected te)t to t e statusbar$

Figure2 List3o)

#ist2ie3
#ist2ie3 control is used to display collections o# items$ It is a more sop isticated control t an t e #ist"ox control$ It can display data in various vie,s is mostly used to display data in multicolumn vie,s$
list ie."cs using using using using 4/stem8 4/stem"9ra.ing8 4/stem"+in#o.s")orms8 4/stem"Collections"%eneric8

public class 6ctress : public string name8 public int /ear8 public 6ctress(string name, int /ear) : this"name < name8 this"/ear < /ear8 ?

class M)orm , )orm : pri ate 4tatus2ar sb8 public M)orm() : Te;t < =(istIie.=8 4i0e < ne. 4i0e(@>0, @00)8 (istU6ctressT actresses < ne. (istU6ctressT()8 actresses"6##(ne. actresses"6##(ne. actresses"6##(ne. actresses"6##(ne. actresses"6##(ne. 6ctress(=Wessica 6lba=, 1R81))8 6ctress(=6ngelina Wolie=, 1RA>))8 6ctress(=Natalie 'ortman=, 1R81))8 6ctress(=1achel +eiss=, 1RA1))8 6ctress(=4carlett Wohansson=, 1R8J))8

Column5ea#er name < ne. Column5ea#er()8 name"Te;t < =Name=8 name"+i#th < -18 Column5ea#er /ear < ne. Column5ea#er()8 /ear"Te;t < =Vear=8 4uspen#(a/out()8 (istIie. l < ne. (istIie.()8 l "'arent < this8 l ")ull1o.4elect < true8 l "%ri#(ines < true8 l "6llo.Column1eor#er < true8 l "4orting < 4ortDr#er"6scen#ing8 l "Columns"6##1ange(ne. Column5ea#erMN :name, /ear?)8 l "ColumnClic3 C< ne. ColumnClic37 ent5an#ler(ColumnClic3)8 $oreach (6ctress act in actresses) : (istIie.!tem item < ne. (istIie.!tem()8 item"Te;t < act"name8 item"4ub!tems"6##(act"/ear"To4tring())8 l "!tems"6##(item)8 ? l "9oc3 < 9oc34t/le")ill8 l "Clic3 C< ne. 7 ent5an#ler(DnChange#)8 sb < ne. 4tatus2ar()8 sb"'arent < this8 l "Iie. < Iie."9etails8 1esume(a/out()8 ? CenterTo4creen()8 oi# DnChange#(ob*ect sen#er, 7 ent6rgs e) : (istIie. l < ((istIie.) sen#er8 string name < l "4electe#!temsM0N"4ub!temsM0N"Te;t8 string born < l "4electe#!temsM0N"4ub!temsM1N"Te;t8 sb"Te;t < name C =, = C born8 ? : oi# ColumnClic3(ob*ect sen#er, ColumnClic37 ent6rgs e) (istIie. l < ((istIie.) sen#er8

? ?

i$ (l "4orting << 4ortDr#er"6scen#ing) : l "4orting < 4ortDr#er"9escen#ing8 ? else : l "4orting < 4ortDr#er"6scen#ing8 ?

class M6pplication : public static oi# Main() : 6pplication"1un(ne. M)orm())8 ? ?

In our e)ample+ ,e ave a listvie, ,it t,o columns$ In t e #irst column+ ,e display t e name o# t e actress$ In t e second one t eir date o# birt $ T e data is store in a #ist collection$ 3y selecting a ro,+ t e data in a ro, is displayed in t e statusbar$ Also+ by clicking on t e column eader+ t e data is sorted$
public class 6ctress : """ ?

"e use t e Actress class to store our data$


(istU6ctressT actresses < ne. (istU6ctressT()8 actresses"6##(ne. 6ctress(=Wessica 6lba=, 1R81))8 actresses"6##(ne. 6ctress(=6ngelina Wolie=, 1RA>))8 """

"e create and #ill our collection ,it items$


Column5ea#er name < ne. Column5ea#er()8 name"Te;t < =Name=8 name"+i#th < -18

For eac column in a listvie,+ ,e create a Column,eader$ 3y setting t e Width to C8+ t e ,idt o# t e column is e>ual to t e longest item in t e column$
(istIie. l < ne. (istIie.()8 l "'arent < this8

#ist2ie3 control is created$


l l l l ")ull1o.4elect < true8 "%ri#(ines < true8 "6llo.Column1eor#er < true8 "4orting < 4ortDr#er"6scen#ing8

=ere ,e set #our properties o# t e control$ T is code lines enable #ull ro, selection+ s o, grid lines+ allo, column reordering by dragging t e columns and sort t e data in ascending order$
l "Columns"6##1ange(ne. Column5ea#erMN :name, /ear?)8

=ere ,e add t,o Column,eaders to t e #ist2ie3 control$


$oreach (6ctress act in actresses) : (istIie.!tem item < ne. (istIie.!tem()8 item"Te;t < act"name8 item"4ub!tems"6##(act"/ear"To4tring())8 l "!tems"6##(item)8 ?

T is cycle populates t e listvie, control$ /ac ro, is added to t e listvie, as a #ist2ie3Item class$
l "Iie. < Iie."9etails8

T e #ist2ie3 control can ave di##erent vie,s$ Di##erent vie,s display data di##erently$
(istIie. l < ((istIie.) sen#er8 string name < l "4electe#!temsM0N"4ub!temsM0N"Te;t8 string born < l "4electe#!temsM0N"4ub!temsM1N"Te;t8 sb"Te;t < name C =, = C born8

Inside t e 'nChanged() met od+ ,e get t e data #rom t e selected ro, and s o, it on t e statusbar$
i$ (l "4orting << 4ortDr#er"6scen#ing) : l "4orting < 4ortDr#er"9escen#ing8 ? else : l "4orting < 4ortDr#er"6scen#ing8 ?

=ere ,e toggle t e sorting order o# t e column$

Figure2 List4ie,

Tree2ie3
Tree2ie3 control displays ierarc ical collection o# items$ /ac item in t is control is represented by a TreeNode ob-ect$
tree ie."cs using 4/stem8 using 4/stem"9ra.ing8 using 4/stem"+in#o.s")orms8 class M)orm , )orm : 4tatus2ar sb8 public M)orm() : Te;t < =TreeIie.=8 4i0e < ne. 4i0e(2>0, 2>0)8 TreeIie. t < ne. TreeIie.()8

TreeNo#e root < ne. TreeNo#e()8 root"Te;t < =(anguages=8 TreeNo#e chil#1 < ne. TreeNo#e()8 chil#1"Te;t < ='/thon=8 TreeNo#e chil#2 < ne. TreeNo#e()8 chil#2"Te;t < =1ub/=8 TreeNo#e chil#@ < ne. TreeNo#e()8

chil#@"Te;t < =Wa a=8 root"No#es"6##1ange(ne. TreeNo#eMN :chil#1, chil#2, chil#@?)8 t t t t "'arent < this8 "No#es"6##(root)8 "9oc3 < 9oc34t/le")ill8 "6$ter4elect C< ne. TreeIie.7 ent5an#ler(6$ter4elect)8

sb < ne. 4tatus2ar()8 sb"'arent < this8 CenterTo4creen()8 ? : ? ? class M6pplication : public static oi# Main() : 6pplication"1un(ne. M)orm())8 ? ? oi# 6$ter4elect(ob*ect sen#er, TreeIie.7 ent6rgs e) sb"Te;t < e"No#e"Te;t8

T is is a very simple demonstration o# t e Tree2ie3 control$ "e ave one root item and t ree c ildren$
TreeIie. t < ne. TreeIie.()8

"e create t e Tree2ie3 control$


TreeNo#e root < ne. TreeNo#e()8 root"Te;t < =(anguages=8 """ t "No#es"6##(root)8

=ere ,e create a root node$


TreeNo#e chil#1 < ne. TreeNo#e()8 chil#1"Te;t < ='/thon=8

C ild node is created in a similar ,ay$


root"No#es"6##1ange(ne. TreeNo#eMN :chil#1, chil#2, chil#@?)8

C ild nodes are plugged into t e Nodes property o# t e root node$

Figure2 Tree4ie,

+irectories
T e #ollo,ing code e)ample ,ill e)amine t e Tree2ie3 control more inCdept $
#irectories"cs using using using using 4/stem8 4/stem"9ra.ing8 4/stem"+in#o.s")orms8 4/stem"!D8

public class M)orm , )orm : pri ate TreeIie. t 8 pri ate 2utton e;pan#8 pri ate 2utton e;pan#6ll8 pri ate 2utton collapse8 pri ate 2utton collapse6ll8 pri ate 4tatus2ar sb8 pri ate const string 5DM7G9!1 < =-home- rons3i*=8 public M)orm() : 4i0e < ne. 4i0e(J00, J00)8 Te;t < =9irectories=8 t < ne. TreeIie.()8

4uspen#(a/out()8 t t t t "'arent < this8 "(ocation < ne. 'oint(10,10)8 "4i0e < ne. 4i0e(Client4i0e"+i#th - 20, 5eight - 200)8 "6nchor < 6nchor4t/les"Top L 6nchor4t/les"(e$t L 6nchor4t/les"1ight 8 ")ull1o.4elect < $alse8 "4ho.(ines < true8 "4ho.'lusMinus < true8 "4crollable < true8 "6$ter4elect C< ne. TreeIie.7 ent5an#ler(6$ter4elect)8

t t t t t

e;pan# < ne. 2utton()8 e;pan#"'arent < this8 e;pan#"(ocation < ne. 'oint(20, t "2ottom C 20)8 e;pan#"Te;t < =7;pan#=8 e;pan#"6nchor < 6nchor4t/les"(e$t L 6nchor4t/les"Top8 e;pan#"Clic3 C< ne. 7 ent5an#ler(Dn7;pan#)8 e;pan#6ll < ne. 2utton()8 e;pan#6ll"'arent < this8 e;pan#6ll"(ocation < ne. 'oint(20, e;pan#"2ottom C >)8 e;pan#6ll"Te;t < =7;pan# 6ll=8 e;pan#6ll"6nchor < 6nchor4t/les"(e$t L 6nchor4t/les"Top8 e;pan#6ll"Clic3 C< ne. 7 ent5an#ler(Dn7;pan#6ll)8 collapse < ne. 2utton()8 collapse"'arent < this8 collapse"(ocation < ne. 'oint(e;pan#6ll"1ight C >, e;pan#"Top )8 collapse"Te;t < =Collapse=8 collapse"6nchor < 6nchor4t/les"(e$t L 6nchor4t/les"Top8 collapse"Clic3 C< ne. 7 ent5an#ler(DnCollapse)8 collapse6ll < ne. 2utton()8 collapse6ll"'arent < this8 collapse6ll"(ocation < ne. 'oint(collapse"(e$t, collapse"2ottom C >)8 collapse6ll"Te;t < =Collapse 6ll=8 collapse6ll"6nchor < 6nchor4t/les"(e$t L 6nchor4t/les"Top8 collapse6ll"Clic3 C< ne. 7 ent5an#ler(DnCollapse6ll)8 sb < ne. 4tatus2ar()8 sb"'arent < this8 4ho.9irectories(t "No#es, 5DM7G9!1)8 1esume(a/out()8 ? CenterTo4creen()8

: ?

oi# 6$ter4elect(ob*ect sen#er, TreeIie.7 ent6rgs e) sb"Te;t < e"No#e"Te;t8

oi# 4ho.9irectories(TreeNo#eCollection tr No#e, string path) 9irector/!n$o #ir!n$o < ne. 9irector/!n$o(path)8 i$ (#ir!n$o X< null) : 9irector/!n$oMN sub9irs < #ir!n$o"%et9irectories()8 TreeNo#e tr < ne. TreeNo#e(#ir!n$o"Name)8 i$ (sub9irs"(ength T 0) : $oreach (9irector/!n$o #r in sub9irs) : i$ (X#r"Name"4tarts+ith(="=)) 4ho.9irectories(tr"No#es, #r")ullName)8 ? ? tr No#e"6##(tr)8

? ? oi# Dn7;pan#(ob*ect sen#er, 7 ent6rgs e) : ? : ? : ? : ? oi# DnCollapse6ll(ob*ect sen#er, 7 ent6rgs e) t "Collapse6ll()8 oi# Dn7;pan#6ll(ob*ect sen#er, 7 ent6rgs e) t "7;pan#6ll()8 oi# DnCollapse(ob*ect sen#er, 7 ent6rgs e) t "4electe#No#e"Collapse()8 t "4electe#No#e"7;pan#()8

static oi# Main() : 6pplication"1un(ne. M)orm())8 ? ?

1ur code e)ample s o,s t e directories o# t e speci#ied ome directory in a Tree2ie3 control$ T e application starts ,it some delay+ because it reads t e directory structure o# t e ome directory #irst$ "e ave also #our buttons on t e #orm$ T e buttons e)pand and collapse nodes programatically$
t "4crollable < true8

"e make t e treevie, control scrollable+ because t e control s o,s lots o# directories$
4ho.9irectories(t "No#es, 5DM7G9!1)8

T e Sho3+irectories() met od #ills t e nodes o# t e trevie, control ,it directories available in t e speci#ied ome directory$
i$ (sub9irs"(ength T 0) : """ ?

"e c eck i# t ere are any subdirectories$


$oreach (9irector/!n$o #r in sub9irs) : i$ (X#r"Name"4tarts+ith(="=)) 4ho.9irectories(tr"No#es, #r")ullName)8 ?

"e loop t roug all directories$ For t is+ ,e use t e recursion algorit m$ "e also skip t e idden directories$ T ey begin ,it a dot on (ni) systems$
tr No#e"6##(tr)8

T is code line actually adds t e directory to t e treevie, control$


oi# Dn7;pan#(ob*ect sen#er, 7 ent6rgs e) : ? t "4electe#No#e"7;pan#()8

All #our buttons ave events plugged to a met od$ =ere is a met od #or t e /)pand button$ It calls t e &xpand() met od o# t e currently selected node$

Figure2 Directories In t is part o# t e Mono "in#orms tutorial+ ,e covered several advanced controls available in "in#orms library$

+ialogs
In t is part o# t e 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 t e application$ A dialog is used to input data+ modi#y data+ c ange t e application settings etc$ Dialogs are important means o# communication bet,een a user and a computer program$ T ere are essentially t,o types o# dialogs$ Prede#ined dialogs and custom dialogs$

Folder"ro3ser+ialog
T is dialog prompts t e user to select a #older$
$ol#erbro.ser#ialog"cs

using using using using

4/stem8 4/stem"!D8 4/stem"9ra.ing8 4/stem"+in#o.s")orms8

class M)orm , )orm : pri ate Tool2ar toolbar8 pri ate Tool2ar2utton open8 pri ate 4tatus2ar statusbar8 public M)orm() : Te;t < =)ol#er2ro.ser9ialog=8 toolbar < ne. Tool2ar()8 open < ne. Tool2ar2utton()8 statusbar < ne. 4tatus2ar()8 statusbar"'arent < this8 toolbar"2uttons"6##(open)8 toolbar"2uttonClic3 C< ne. Tool2ar2uttonClic37 ent5an#ler(DnClic3e#)8 Controls"6##(toolbar)8 ? CenterTo4creen()8

oi# DnClic3e#(ob*ect sen#er, Tool2ar2uttonClic37 ent6rgs e) : )ol#er2ro.ser9ialog #ialog < ne. )ol#er2ro.ser9ialog()8 i$ (#ialog"4ho.9ialog(this) << 9ialog1esult"DK) : statusbar"Te;t < #ialog"4electe#'ath8 ? ? ?

class M6pplication : public static oi# Main() : 6pplication"1un(ne. M)orm())8 ? ?

"e ave a toolbar and one toolbar button$ 3y clicking on t e button+ t e Folder"ro3ser+ialog appears on t e screen$ T e name o# t e selected #older is s o,n in t e statusbar$
)ol#er2ro.ser9ialog #ialog < ne. )ol#er2ro.ser9ialog()8

T e Folder"ro3ser+ialog is created$
i$ (#ialog"4ho.9ialog(this) << 9ialog1esult"DK) : statusbar"Te;t < #ialog"4electe#'ath8 ?

T e Sho3+ialog() met od s o,s t e dialog on t e screen$ I# ,e click on t e 1' button o# t e dialog+ t e selected directory pat is s o,n on t e statusbar$

Figure2 Folder3ro,serDialog

Color+ialog
T is dialog displays available colors along ,it controls t at enable t e user to de#ine custom colors$
color#ialog"cs using 4/stem8 using 4/stem"9ra.ing8 using 4/stem"+in#o.s")orms8 class M)orm , )orm : pri ate Tool2ar toolbar8 pri ate Tool2ar2utton open8 pri ate Color color8 pri ate int rect+i#th < 1008 pri ate int rect5eight < 1008 pri ate 1ectangle r8 public M)orm() : Te;t < =Color9ialog=8 color < Color"2lue8 toolbar < ne. Tool2ar()8 open < ne. Tool2ar2utton()8 toolbar"2uttons"6##(open)8 toolbar"2uttonClic3 C< ne. Tool2ar2uttonClic37 ent5an#ler(DnClic3e#)8 (ocate1ect()8 4et4t/le (Control4t/les"1esi0e1e#ra., true)8 Controls"6##(toolbar)8 'aint C< ne. 'aint7 ent5an#ler(Dn'aint)8

CenterTo4creen()8 ? oi# Dn'aint(ob*ect sen#er, 'aint7 ent6rgs e) : %raphics g < e"%raphics8 (ocate1ect()8 4oli#2rush brush < ne. 4oli#2rush(color)8 ? g")ill1ectangle(brush, r)8

oi# DnClic3e#(ob*ect sen#er, Tool2ar2uttonClic37 ent6rgs e) : Color9ialog #ialog < ne. Color9ialog()8 i$ (#ialog"4ho.9ialog(this) << 9ialog1esult"DK) : color < #ialog"Color8 !n ali#ate()8 ? ? oi# (ocate1ect() : int ; < (Client4i0e"+i#th - rect+i#th) - 28 int / < (Client4i0e"5eight - rect5eight) - 28 r < ne. 1ectangle(;, /, rect+i#th, rect5eight)8 ? ?

class M6pplication : public static oi# Main() : 6pplication"1un(ne. M)orm())8 ? ?

In t is code e)ample+ ,e use t e Color+ialog to c oose a color #or a rectangle+ t at is located in t e middle o# t e #orm control$
color < Color"2lue8

At t e beginning+ t e color o# t e rectangle is blue$ "e use t e color variable to determine t e color o# t e rectangle$
Color9ialog #ialog < ne. Color9ialog()8

T e Color+ialog is created$
i$ (#ialog"4ho.9ialog(this) << 9ialog1esult"DK) : color < #ialog"Color8 !n ali#ate()8 ?

T e code s o,s t e color dialog$ I# ,e click on t e 1' button+ ,e get t e selected color and call t e Invalidate() met od$ T e met od invalidates t e entire sur#ace o# t e control and causes t e control to be redra,n$ T e result is t at t e rectangle is dra,n ,it a ne, color value$

Figure2 ColorDialog

Font+ialog
T e Font+ialog is used to select #onts$
$ont#ialog"cs using 4/stem8 using 4/stem"9ra.ing8 using 4/stem"+in#o.s")orms8 class M)orm , )orm : pri ate (abel te;t8 pri ate Tool2ar toolbar8 pri ate Tool2ar2utton open8 public M)orm() : Te;t < =)ont9ialog=8 te;t < ne. (abel()8 te;t"'arent < this8 te;t"Te;t < =+in$orms tutorial=8 (ocateTe;t()8 toolbar < ne. Tool2ar()8 toolbar"'arent < this8 open < ne. Tool2ar2utton()8 toolbar"2uttons"6##(open)8 toolbar"2uttonClic3 C< ne. Tool2ar2uttonClic37 ent5an#ler(DnClic3e#)8 te;t"6uto4i0e < true8 1esi0e C< ne. 7 ent5an#ler(Dn1esi0e)8 CenterTo4creen()8 ?

oi# Dn1esi0e(ob*ect sen#er, 7 ent6rgs e): (ocateTe;t()8 oi# DnClic3e#(ob*ect sen#er, Tool2ar2uttonClic37 ent6rgs e) : )ont9ialog #ialog < ne. )ont9ialog()8 i$ (#ialog"4ho.9ialog(this) << 9ialog1esult"DK) : te;t")ont < #ialog")ont8 (ocateTe;t()8 ? oi# (ocateTe;t() : te;t"Top < (this"Client4i0e"5eight - te;t"5eight) - 28 te;t"(e$t < (this"Client4i0e"+i#th - te;t"+i#th) - 28

class M6pplication : public static oi# Main() : 6pplication"1un(ne. M)orm())8 ? ?

"e dra, some te)t in t e middle o# t e #orm control$ "e use t e #ont dialog to c ange #ont #or t is te)t$
)ont9ialog #ialog < ne. )ont9ialog()8

Font+ialog is created$
i$ (#ialog"4ho.9ialog(this) << 9ialog1esult"DK) : te;t")ont < #ialog")ont8 (ocateTe;t()8 ?

" en ,e click on t e 1' button+ ,e set a ne,ly selected #ont #or t e #abel control$ 3ecause t e si@e o# t e te)t c anges ,it di##erent #onts+ ,e must call t e #ocateText() met od+ , ic locates t e te)t in t e middle o# t e #orm control$

Figure2 FontDialog

'pen+ialog
T is dialog is used to open #iles$
open#ialog"cs using using using using 4/stem8 4/stem"!D8 4/stem"9ra.ing8 4/stem"+in#o.s")orms8

class M)orm , )orm : pri ate Tool2ar toolbar8 pri ate Tool2ar2utton open8 pri ate Te;t2o; te;tbo;8 public M)orm() : Te;t < =Dpen)ile9ialog=8 toolbar < ne. Tool2ar()8 open < ne. Tool2ar2utton()8 te;tbo; < ne. Te;t2o;()8 te;tbo;"Multiline < true8 te;tbo;"4croll2ars < 4croll2ars"2oth8 te;tbo;"+or#+rap < $alse8 te;tbo;"'arent < this8 te;tbo;"9oc3 < 9oc34t/le")ill8 toolbar"2uttons"6##(open)8 toolbar"2uttonClic3 C< ne. Tool2ar2uttonClic37 ent5an#ler(DnClic3e#)8 Controls"6##(toolbar)8 Controls"6##(te;tbo;)8 ? CenterTo4creen()8 oi# DnClic3e#(ob*ect sen#er, Tool2ar2uttonClic37 ent6rgs e) : Dpen)ile9ialog #ialog < ne. Dpen)ile9ialog()8 #ialog")ilter < =C# $iles (H"cs)LH"cs=8 i$ (#ialog"4ho.9ialog(this) << 9ialog1esult"DK) : 4tream1ea#er rea#er < ne. 4tream1ea#er(#ialog")ileName)8 string #ata < rea#er"1ea#To7n#()8 rea#er"Close()8 te;tbo;"Te;t < #ata8 ?

? ?

class M6pplication : public static oi# Main() : 6pplication"1un(ne. M)orm())8 ? ?

"e use t e 'pen+ialog control to open C! source #iles$ "e ave a Text"ox control+ , ere ,e display t e #ile$
Dpen)ile9ialog #ialog < ne. Dpen)ile9ialog()8

T e 'pen+ialog is created$
#ialog")ilter < =C# $iles (H"cs)LH"cs=8

"e set t e Filter property to C! source #iles$ 1nly C! #iles can be c osen ,it t is dialog instance$
i$ (#ialog"4ho.9ialog(this) << 9ialog1esult"DK) : 4tream1ea#er rea#er < ne. 4tream1ea#er(#ialog")ileName)8 string #ata < rea#er"1ea#To7n#()8 rea#er"Close()8 te;tbo;"Te;t < #ata8 ?

A#ter clicking 1'+ ,e read t e contents o# t e c osen #ile and put it into t e Text"ox control$

Figure2 1penDialog In t is part o# t e Mono "in#orms tutorial+ ,e s o,ed various dialogs$

+rag 4 +rop in Mono Winforms


JKCC googleLadLclient D ?pubCME;NE;MEA88M8A9:?O googleLadL,idt D 8:;O googleLadL eig t D N;;O googleLadL#ormat D ?8:;)N;;Las?O googleLadLtype D ?te)t?O BB:;;EC;EC8:2 kanal8 googleLadLc annel D ?E89:M<:G:9?O googleLcolorLborder D ?9D<8//?O

googleLcolorLbg D ?FFFFFF?O googleLcolorLlink D ?9D<8//?O googleLcolorLte)t D ?;;;;;;?O googleLcolorLurl D ?;;<;;;?O googleLuiL#eatures D ?rc28;?O BBCCP,indo,$googleLrenderLad56OJKCC googleLadLclient D ?pubCME;NE;MEA88M8A9:?O googleLadL,idt D GN<O googleLadL eig t D N;O googleLadL#ormat D ?GN<)N;Las?O googleLadLtype D ?te)t?O BB:;;EC;EC8:2 ori@ont googleLadLc annel D ?EN;EAA9:9M?O googleLcolorLborder D ?/N/N/N?O googleLcolorLbg D ?FFFFFF?O googleLcolorLlink D ?;;;;FF?O googleLcolorLte)t D ?;;;;;;?O googleLcolorLurl D ?;;<;;;?O googleLuiL#eatures D ?rc28;?O BBCCP ,indo,$googleLrenderLad56OT is part o# t e Mono "in#orms tutorial ,ill be dedicated to t e drag & drop operations$ In computer grap ical user inter#aces+ dragCandCdrop is t e action o# 5or support #or t e action o#6 clicking on a virtual ob-ect and dragging it to a di##erent location or onto anot er 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# t e most visible aspects o# t e grap ical user inter#ace$ Drag and drop operation enables you to do comple) t ings intuitively$

+ragging a button
In t e #irst e)ample+ ,e ,ill do t e drag & drop operation on t e button control$ T e e)ample does t e -ob outside t e drag & drop protocol$
#ragbutton"cs using 4/stem8 using 4/stem"9ra.ing8 using 4/stem"+in#o.s")orms8 public class M)orm , )orm : pri ate bool is9ragging < $alse8 pri ate int ol#F, ol#V8 pri ate 2utton button8 public M)orm() : Te;t < =9rag E #rop button=8 4i0e < ne. 4i0e(2A0, 180)8 button < ne. 2utton()8 button"'arent < this8 button"Cursor < Cursors"5an#8 button"Te;t < =2utton=8 button"(ocation < ne. 'oint(20, 20)8

button"Mouse9o.n C< ne. Mouse7 ent5an#ler(DnMouse9o.n)8 button"Mouse&p C< ne. Mouse7 ent5an#ler(DnMouse&p)8 button"MouseMo e C< ne. Mouse7 ent5an#ler(DnMouseMo e)8 ? CenterTo4creen()8

public static oi# Main() : 6pplication"1un(ne. M)orm())8 ? pri ate oi# DnMouse9o.n(ob*ect sen#er, Mouse7 ent6rgs e) : is9ragging < true8 ol#F < e"F8 ol#V < e"V8 ? pri ate oi# DnMouseMo e(ob*ect sen#er, Mouse7 ent6rgs e) : i$ (is9ragging) : button"Top < button"Top C (e"V - ol#V)8 button"(e$t < button"(e$t C (e"F - ol#F)8 ? ? pri ate oi# DnMouse&p(ob*ect sen#er, Mouse7 ent6rgs e) : is9ragging < $alse8 ? ?

T e code e)ample puts a regular button control on t e #orm container$ 3y clicking on t e button sur#ace and simultaneously dragging it ,it a mouse ,e can relocate t e button$
pri ate bool is9ragging < $alse8 pri ate int ol#F, ol#V8

T ese are t e supporting variables #or our e)ample$ T e is+ragging variable tells us+ , et er ,e are in t e process o# dragging an ob-ect$ T e old5 and old6 variables store t e )+ y coordinates -ust be#ore t e dragging process begins$
button"Mouse9o.n C< ne. Mouse7 ent5an#ler(DnMouse9o.n)8 button"Mouse&p C< ne. Mouse7 ent5an#ler(DnMouse&p)8 button"MouseMo e C< ne. Mouse7 ent5an#ler(DnMouseMo e)8

"e plug in t ree di##erent mouse andlers #or our button$ T ey implement t ree di##erent stages o# t e drag & drop process$ T e process begins+ , en ,e click on t e button$ T is is andled by t e 'nMouse+o3n() met od$ T e second part is t e movement$ T is is , en ,e move t e ob-ect to a ne, position$ It is andled in t e 'nMouseMove() met od$ T e #inal part is , en t e process stops$ It appens , en ,e release t e mouse button$ T e appropriate task is delegated to t e 'nMouse.p() met od$
pri ate oi# DnMouse9o.n(ob*ect sen#er, Mouse7 ent6rgs e) : is9ragging < true8 ol#F < e"F8 ol#V < e"V8 ?

T e 'nMouse+o3n() met od implements t e #irst part o# t e process$ It sets t ree necessary variables$
pri ate oi# DnMouseMo e(ob*ect sen#er, Mouse7 ent6rgs e) : i$ (is9ragging) : button"Top < button"Top C (e"V - ol#V)8 button"(e$t < button"(e$t C (e"F - ol#F)8 ? ?

In t e 'nMouseMove() met od+ ,e relocate t e button$ "e calculate t e di##erence bet,een t e stored )+ y coordinates and t e ne, coordinates o# t e mouse pointer$ T e di##erence is added to t e Top and #eft properties o# t e button+ t us moving it to a ne, position$

Figure2 Dragging a button

+ragging Text
In t e previous e)ample+ ,e did drag & drop on t e control$ *e)t ,e ,ill do a drag & drop operation on t e te)tual data$ =ere ,e ,ill use t e drag & drop protocol provided by t e "in#orms library$ Drag & drop operation is a standardi@ed communication protocol in "in#orms$ "e ave t,o basic ob-ects$ T e drag source and t e drop target$
#ragte;t"cs using 4/stem8 using 4/stem"9ra.ing8 using 4/stem"+in#o.s")orms8 public class M)orm , )orm : pri ate Te;t2o; te;t2o;8 pri ate 2utton button8 public M)orm() : !nit)orm()8 CenterTo4creen()8 ? pri ate oi# DnMouse9o.n(ob*ect sen#er, Mouse7 ent6rgs e)

: Te;t2o; t;t < (Te;t2o;) sen#er8 t;t"9o9rag9rop(t;t"Te;t, 9rag9rop7$$ects"Cop/)8 ? pri ate oi# Dn9rag7nter(ob*ect sen#er, 9rag7 ent6rgs e) : e"7$$ect < 9rag9rop7$$ects"Cop/8 ? pri ate oi# Dn9rag9rop(ob*ect sen#er, 9rag7 ent6rgs e) : 2utton button < (2utton) sen#er8 button"Te;t < (string) e"9ata"%et9ata(9ata)ormats"Te;t)8 ? pri ate oi# !nit)orm() : Te;t < =9rag E #rop=8 button < ne. 2utton()8 te;t2o; < ne. Te;t2o;()8 4uspen#(a/out()8 button"6llo.9rop < true8 button"(ocation < ne. 'oint(1>0, >0)8 te;t2o;"(ocation < ne. 'oint(1>, >0)8 button"9rag9rop C< ne. 9rag7 ent5an#ler(Dn9rag9rop)8 button"9rag7nter C< ne. 9rag7 ent5an#ler(Dn9rag7nter)8 te;t2o;"Mouse9o.n C< ne. Mouse7 ent5an#ler(DnMouse9o.n)8 Client4i0e < ne. 4i0e(2>0, 200)8 Controls"6##(button)8 Controls"6##(te;t2o;)8 1esume(a/out()8

public static oi# Main(stringMN args) : 6pplication"1un(ne. M)orm())8 ? ?

"e ave t,o controls on t e #orm$ A button and a te)t bo)$ "e ,ill drag te)t #rom t e te)t bo) and drop it on t e button$
!nit)orm()8

Setting up o# t e #orm is delegated to t e InitForm() met od$ T is is usually done in larger applications$
4uspen#(a/out()8 """ 1esume(a/out()8

"e lay out our controls bet,een t ese t,o met od calls$ T is is an optimali@ation$ It s ould eliminate #licker$
button"6llo.9rop < true8

"e set t e Allo3+rop property to true$ Dropping is not enabled by de#ault$

button"9rag9rop C< ne. 9rag7 ent5an#ler(Dn9rag9rop)8 button"9rag7nter C< ne. 9rag7 ent5an#ler(Dn9rag7nter)8 te;t2o;"Mouse9o.n C< ne. Mouse7 ent5an#ler(DnMouse9o.n)8

Again+ t e drag & drop process is divided into t ree steps$ "e ave t ree met ods #or eac particular step$
pri ate oi# DnMouse9o.n(ob*ect sen#er, Mouse7 ent6rgs e) : Te;t2o; t;t < (Te;t2o;) sen#er8 t;t"9o9rag9rop(t;t"Te;t, 9rag9rop7$$ects"Cop/)8 ?

In t e 'nMouse+o3n() met od ,e initiali@e t e drap & drop process$ "e initiate t e process ,it t e +o+rag+rop() met od$ T e +rag+rop&ffects!Cop*parameter speci#ies t e type o# t e operation$ /sentially+ ,e can eit er copy t e te)t or move it during t e drag & drop operation$
pri ate oi# Dn9rag7nter(ob*ect sen#er, 9rag7 ent6rgs e) : e"7$$ect < 9rag9rop7$$ects"Cop/8 ?

T e +rag&nter event is launc ed , en t e mouse pointer enters t e area o# t e drop target control$ T e &ffect property must be set$ T e DragDrop/##ects o# t e drag source and drop target must be e>ual$ 1t er,ise t e operation ,ill not ,ork$
pri ate oi# Dn9rag9rop(ob*ect sen#er, 9rag7 ent6rgs e) : 2utton button < (2utton) sen#er8 button"Te;t < (string) e"9ata"%et9ata(9ata)ormats"Te;t)8 ?

Finally ,e ave t e 'n+rag+rop() met od$ =ere ,e get t e data #rom t e event ob-ect and set it to t e button Text property$

Figure2 Drag & drop o# te)t

+ragging Image
In our last e)ample+ ,e ,ill drag & drop image on t e #orm$
#ragimage"cs using 4/stem8 using 4/stem"9ra.ing8

using 4/stem"+in#o.s")orms8 public class !mage9rag9rop , )orm : pri ate bool is9ragging8 pri ate int ol#F, ol#V8 pri pri pri pri ate ate ate ate 1ectangle #rop1ect8 'icture2o; pic2o;8 2itmap image8 2rush brush8

public !mage9rag9rop() : Client4i0e < ne. 4i0e(@>0, 2>0)8 Te;t < =9ragging !mage=8 'aint C< ne. 'aint7 ent5an#ler(Dn'aint)8 is9ragging < $alse8 #rop1ect < ne. 1ectangle(10, 10, 200, 1S0)8 brush < 2rushes"%ra/8 pic2o; < ne. 'icture2o;()8 loa#!mage()8 pic2o;"'arent < this8 pic2o;"(ocation < ne. 'oint(100, >0)8 pic2o;"4i0e < ne. 4i0e(image"+i#th, image"5eight)8 pic2o;"!mage < image8 pic2o;"Cursor < Cursors"5an#8 pic2o;"Mouse9o.n C< ne. Mouse7 ent5an#ler(DnMouse9o.n)8 pic2o;"Mouse&p C< ne. Mouse7 ent5an#ler(DnMouse&p)8 pic2o;"MouseMo e C< ne. Mouse7 ent5an#ler(DnMouseMo e)8 CenterTo4creen()8 ? oi# loa#!mage() : tr/ : image < ne. 2itmap(=image"*pg=)8 ? catch : Console"+rite(ine(=7rror rea#ing image=)8 7n ironment"7;it(1)8 ? ? public static oi# Main() : 6pplication"1un(ne. !mage9rag9rop())8 ? pri ate oi# DnMouse9o.n(ob*ect sen#er, Mouse7 ent6rgs e) : is9ragging < true8 ol#F < e"F8 ol#V < e"V8 ? pri ate oi# DnMouseMo e(ob*ect sen#er, Mouse7 ent6rgs e) : i$ (is9ragging) : pic2o;"Top < pic2o;"Top C (e"V - ol#V)8

pic2o;"(e$t < pic2o;"(e$t C (e"F - ol#F)8 ? ?

pri ate oi# DnMouse&p(ob*ect sen#er, Mouse7 ent6rgs e) : is9ragging < $alse8 i$(#rop1ect"Contains(pic2o;"2oun#s)) : brush < 2rushes"%ol#8 ? else : brush < 2rushes"%ra/8 ? 1e$resh()8 ? pri ate oi# Dn'aint(ob*ect sen#er, 'aint7 ent6rgs e) : %raphics g < e"%raphics8 g")ill1ectangle(brush, #rop1ect)8 ?

In our e)ample ,e ave a $icture"ox and ,e dra, a gray rectangle$ I# ,e drop t e picture inside t e rectangle+ t e color o# t e rectangle c anges to gold$
brush < 2rushes"%ra/8

T e brush variable olds t e brus o# t e rectangle$ It is a gray color by de#ault$


oi# loa#!mage() : tr/ : image < ne. 2itmap(=image"*pg=)8 ? catch : Console"+rite(ine(=7rror rea#ing image=)8 7n ironment"7;it(1)8 ? ?

T e loadImage() loads a bitmap #or t e $icture"ox control$


i$ (#rop1ect"Contains(pic2o;"2oun#s)) : brush < 2rushes"%ol#8 ? else : brush < 2rushes"%ra/8 ?

In t e 'nMouse.p() met od+ ,e determine t e brus o# t e rectangle$ I# t e bounds o# t e picture bo) are inside t e rectangle+ t e brus is o# gold colorO gray ot er,ise$
1e$resh()8

"e must call t e 7efresh() met od to activate t e ne, brus color$

Figure2 Drag & drop image T is c apter ,as dedicated to drag & drop operations using t e Mono "in#orms library$

$ainting in Mono Winforms


JKCC googleLadLclient D ?pubCME;NE;MEA88M8A9:?O googleLadL,idt D 8:;O googleLadL eig t D N;;O googleLadL#ormat D ?8:;)N;;Las?O googleLadLtype D ?te)t?O BB:;;EC;EC8:2 kanal8 googleLadLc annel D ?E89:M<:G:9?O googleLcolorLborder D ?9D<8//?O googleLcolorLbg D ?FFFFFF?O googleLcolorLlink D ?9D<8//?O googleLcolorLte)t D ?;;;;;;?O googleLcolorLurl D ?;;<;;;?O googleLuiL#eatures D ?rc28;?O BBCCP,indo,$googleLrenderLad56OJKCC googleLadLclient D ?pubCME;NE;MEA88M8A9:?O googleLadL,idt D GN<O googleLadL eig t D N;O googleLadL#ormat D ?GN<)N;Las?O googleLadLtype D ?te)t?O BB:;;EC;EC8:2 ori@ont googleLadLc annel D ?EN;EAA9:9M?O googleLcolorLborder D ?/N/N/N?O googleLcolorLbg D ?FFFFFF?O googleLcolorLlink D ?;;;;FF?O googleLcolorLte)t D ?;;;;;;?O googleLcolorLurl D ?;;<;;;?O googleLuiL#eatures D ?rc28;?O BBCCP ,indo,$googleLrenderLad56OIn t is part o# t e Mono "in#orms tutorial+ ,e ,ill do some painting$ Painting is used+ , en ,e ,ant to c ange or en ance an e)isting control$ 1r i# ,e are creating a

custom control #rom scratc $ To do t e painting+ ,e use t e painting API provided by t e "in#orms library$ T e painting is done ,it in a met od+ t at ,e plug into t e $aint event$ T e S*stem!+ra3ing namespace provides access to -+I8 basic grap ics #unctionality$ More advanced #unctionality is provided in t e System$Dra,ing$Dra,ing:D+ System$Dra,ing$Imaging+ and System$Dra,ing$Te)t namespaces$ T e -raphics class provides met ods #or dra,ing on t e #orm$

#ines
1ur #irst e)ample ,ill dra, lines on t e Form control$
lines"cs using using using using 4/stem8 4/stem"9ra.ing8 4/stem"9ra.ing"9ra.ing298 4/stem"+in#o.s")orms8

class M)orm , )orm : public M)orm() : Te;t < =(ines=8 4i0e < ne. 4i0e(280, 2A0)8 1esi0e1e#ra. < true8 'aint C< ne. 'aint7 ent5an#ler(Dn'aint)8 CenterTo4creen()8

oi# Dn'aint(ob*ect sen#er, 'aint7 ent6rgs e) %raphics g < e"%raphics8 'en pen < ne. 'en(Color"2lac3, 1)8 pen"9ash4t/le < 9ash4t/le"9ot8 g"9ra.(ine(pen, 20, J0, 2>0, J0)8 pen"9ash4t/le < 9ash4t/le"9ash9ot8 g"9ra.(ine(pen, 20, 80, 2>0, 80)8 pen"9ash4t/le < 9ash4t/le"9ash8 g"9ra.(ine(pen, 20, 120, 2>0, 120)8 pen"9ash4t/le < 9ash4t/le"9ash9ot9ot8 g"9ra.(ine(pen, 20, 1S0, 2>0, 1S0)8 pen"9ash'attern < ne. $loatMN :S$, 8$, 1$, 1$, 1$, 1$, 1$, 1$ ?8 g"9ra.(ine(pen, 20, 200, 2>0, 200)8

? ?

g"9ispose()8

class M6pplication : public static oi# Main() : 6pplication"1un(ne. M)orm())8 ? ?

"e dra, #ive lines on t e #orm$ /ac line as di##erent +ashSt*le$

1esi0e1e#ra. < true8

" en ,e resi@e t e #orm+ it is automatically redra,n$ T is is not t e de#ault be avior$


'aint C< ne. 'aint7 ent5an#ler(Dn'aint)8

Paint events are delivered to t e 'n$aint() met od$


oi# Dn'aint(ob*ect sen#er, 'aint7 ent6rgs e) : ? """

T is is t e signature o# t e 'n$aint() met od$


%raphics g < e"%raphics8

In order to paint on t e #orm+ ,e must get t e -raphics ob-ect$ Painting on a #orm is actually calling various met ods o# t e -raphics ob-ect$
'en pen < ne. 'en(Color"2lac3, 1)8 pen"9ash4t/le < 9ash4t/le"9ot8 g"9ra.(ine(pen, 20, J0, 2>0, J0)8

"e create a $en ob-ect$ T is ob-ect is used to dra, outlines o# s apes$ T an ,e set a dotted +ashSt*le$ Finally ,e dra, t e line ,it t e +ra3#ine() met od$ T e #irst parameter is t e pen ob-ect$ T e ne)t #our values are ) and y values o# starting and ending points o# t e line$
pen"9ash'attern < ne. $loatMN :S$, 8$, 1$, 1$, 1$, 1$, 1$, 1$ ?8

T ere are several builtCin +ashSt*le values$ "e can create our o,n style by using t e +ash$attern property$ It may look di##icult at t e #irst sig t$ 3ut t e pattern is simply an array o# #ill and empty values$
g"9ispose()8

"e kno, t at C! language uses garbage collection$ So , y are ,e e)plicitely releasing resourcesQ T is is to increase e##iciency$ "e are elping t e garbage collector$

Figure2 Lines

Colors
A color in "in#orms library represents an A0&3 5alp a+ red+ green+ blue6 color$ It is a combination o# Alp a+ 0ed+ &reen+ and 3lue 50&36 intensity values$ T ere are also prede#ined color names+ t at ,e can use in painting$ ttp2BB@etcode$comBtutorialsBmono,in#ormstutorialBimages
colors"cs using using using using 4/stem8 4/stem"9ra.ing8 4/stem"9ra.ing"9ra.ing298 4/stem"+in#o.s")orms8

class M)orm , )orm : public M)orm() : Te;t < =Colors=8 4i0e < ne. 4i0e(@S0, @00)8 http,--0etco#e"com-tutorials-mono.in$ormstutorial-images 'aint C< ne. 'aint7 ent5an#ler(Dn'aint)8 ? CenterTo4creen()8

oi# Dn'aint(ob*ect sen#er, 'aint7 ent6rgs e) %raphics g < e"%raphics8 g")ill1ectangle(2rushes"4ienna, 10, 1>, R0, S0)8 g")ill1ectangle(2rushes"%reen, 1@0, 1>, R0, S0)8 g")ill1ectangle(2rushes"Maroon, 2>0, 1>, R0, S0)8 g")ill1ectangle(2rushes"Chocolate, 10, 10>, R0, S0)8 g")ill1ectangle(2rushes"%ra/, 1@0, 10>, R0, S0)8 g")ill1ectangle(2rushes"Coral, 2>0, 10>, R0, S0)8 g")ill1ectangle(2rushes"2ro.n, 10, 1R>, R0, S0)8 g")ill1ectangle(2rushes"Teal, 1@0, 1R>, R0, S0)8 g")ill1ectangle(2rushes"%ol#enro#, 2>0, 1R>, R0, S0)8 g"9ispose()8http,--0etco#e"com-tutorials-mono.in$ormstutorial-images

class M6pplication : public static oi# Main() : 6pplication"1un(ne. M)orm())8 ? ?

"e dra, nine rectangles ,it nine di##erent colors$


g")ill1ectangle(2rushes"4ienna, 10, 1>, R0, S0)8

T e Fill7ectagle() met od #ills a speci#ied rectangle ,it a brus $ A brus can be a color or a pattern$ T ere are some prede#ined colors available$ "e can get t em #rom t e "rushes enumeration$ T e last #our values are t e )+ y values o# t e tople#t point and t e ,idt and eig t o# t e rectangle$

Figure2 Colors

,atches
T e ,atch"rush ob-ect is used to #ill t e interiors o# t e s apes$ T ere are several builtCin patterns+ t at ,e can use$
hatches"cs using using using using 4/stem8 4/stem"9ra.ing8 4/stem"9ra.ing"9ra.ing298 4/stem"+in#o.s")orms8

class M)orm , )orm : public M)orm() : Te;t < =5atches=8 4i0e < ne. 4i0e(@S0, @00)8 'aint C< ne. 'aint7 ent5an#ler(Dn'aint)8 CenterTo4creen()8 ? oi# Dn'aint(ob*ect sen#er, 'aint7 ent6rgs e) : %raphics g < e"%raphics8

5atch2rush hb < ne. 5atch2rush(5atch4t/le"Cross, Color"2lac3, this"2ac3Color)8 g")ill1ectangle(hb, 10, 1>, R0, S0)8 hb < ne. 5atch2rush(5atch4t/le"'ercent0>, Color"2lac3, this"2ac3Color)8 g")ill1ectangle(hb, 1@0, 1>, R0, S0)8 hb < ne. 5atch2rush(5atch4t/le"4oli#9iamon#, Color"2lac3, this"2ac3Color)8 g")ill1ectangle(hb, 2>0, 1>, R0, S0)8 hb < ne. 5atch2rush(5atch4t/le"9iagonal2ric3, Color"2lac3, this"2ac3Color)8

g")ill1ectangle(hb, 10, 10>, R0, S0)8 hb < ne. 5atch2rush(5atch4t/le"9i ot, Color"2lac3, this"2ac3Color)8 g")ill1ectangle(hb, 1@0, 10>, R0, S0)8 hb < ne. 5atch2rush(5atch4t/le"+a e, Color"2lac3, this"2ac3Color)8 g")ill1ectangle(hb, 2>0, 10>, R0, S0)8 hb < ne. 5atch2rush(5atch4t/le"YigYag, Color"2lac3, this"2ac3Color)8 g")ill1ectangle(hb, 10, 1R>, R0, S0)8 hb < ne. 5atch2rush(5atch4t/le"4phere, Color"2lac3, this"2ac3Color)8 g")ill1ectangle(hb, 1@0, 1R>, R0, S0)8 hb < ne. 5atch2rush(5atch4t/le"4hingle, Color"2lac3, this"2ac3Color)8 g")ill1ectangle(hb, 2>0, 1R>, R0, S0)8 hb"9ispose()8 g"9ispose()8 ? ?

class M6pplication : public static oi# Main() : 6pplication"1un(ne. M)orm())8 ? ?

T is time ,e #ill nine rectangles ,it nine di##erent patterns+ called atc es$
5atch2rush hb < ne. 5atch2rush(5atch4t/le"Cross, Color"2lac3, this"2ac3Color)8

=ere ,e create a ,atch"rush ob-ect$ T e parameters are t e atc style and t e #oreground and t e background colors$ T e background color is set to t e color o# t e #orm+ so t at it looks like ,e ave dra,n onto t e #orm$
g")ill1ectangle(hb, 10, 1>, R0, S0)8

"e #ill t e rectangle ,it t e speci#ied atc brus $

Figure2 =atc es

-radients
In computer grap ics+ gradient is a smoot blending o# s ades #rom lig t to dark or #rom one color to anot er$ 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 lig ts and s ado,s$ 5ans,ers$com6
gra#ients"cs using using using using 4/stem8 4/stem"9ra.ing8 4/stem"9ra.ing"9ra.ing298 4/stem"+in#o.s")orms8

class M)orm , )orm : public M)orm() : Te;t < =%ra#ients=8 4i0e < ne. 4i0e(@>0, @>0)8 'aint C< ne. 'aint7 ent5an#ler(Dn'aint)8 ? CenterTo4creen()8

oi# Dn'aint(ob*ect sen#er, 'aint7 ent6rgs e) %raphics g < e"%raphics8 'oint pt1 < ne. 'oint(>, >)8 'oint pt2 < ne. 'oint(2>, 2>)8 2rush lg < ne. (inear%ra#ient2rush(pt1, pt2, Color"1e#, Color"2lac3)8 g")ill1ectangle(lg, 20, 20, @00, J0)8 pt1 < ne. 'oint(>, 2>)8 pt2 < ne. 'oint(20, 2)8 lg < ne. (inear%ra#ient2rush(pt1, pt2, Color"Vello., Color"2lac3)8 g")ill1ectangle(lg, 20, 80, @00, J0)8 pt1 < ne. 'oint(>, 2>)8 pt2 < ne. 'oint(2, 2)8 lg < ne. (inear%ra#ient2rush(pt1, pt2, Color"%reen, Color"2lac3)8 g")ill1ectangle(lg, 20, 1J0, @00, J0)8 pt1 < ne. 'oint(2>, 2>)8 pt2 < ne. 'oint(1>, 2>)8 lg < ne. (inear%ra#ient2rush(pt1, pt2, Color"2lue, Color"2lac3)8 g")ill1ectangle(lg, 20, 200, @00, J0)8 pt1 < ne. 'oint(0, 10)8 pt2 < ne. 'oint(0, 20)8 lg < ne. (inear%ra#ient2rush(pt1, pt2, Color"Drange, Color"2lac3)8 g")ill1ectangle(lg, 20, 2S0, @00, J0)8 lg"9ispose()8 g"9ispose()8

? ?

class M6pplication : public static oi# Main() : 6pplication"1un(ne. M)orm())8 ? ?

"e dra, #ive rectangles , ic are #illed ,it di##erent linear gradients$ ttp2BB@etcode$comBtutorialsBmono,in#ormstutorialBimages
'oint pt1 < ne. 'oint(>, >)8 'oint pt2 < ne. 'oint(2>, 2>)8

T ese t,o are t e controlling points o# t e linear gradient brus $


2rush lg < ne. (inear%ra#ient2rush(pt1, pt2, Color"1e#, Color"2lac3)8

"e create t e #inear-radient"rush ob-ect$ "e use t,o controlling points and t,o blending colors$

Figure2 &radients

+ra3ing string
To dra, string on t e "in#orms Form+ ,e use t e +ra3String() met od$
l/rics"cs using using using using 4/stem8 4/stem"9ra.ing8 4/stem"9ra.ing"9ra.ing298 4/stem"+in#o.s")orms8

class M)orm , )orm : public M)orm() : Te;t < =Vou 3no. !Qm No %oo#=8 4i0e < ne. 4i0e(@80, J>0)8 'aint C< ne. 'aint7 ent5an#ler(Dn'aint)8 CenterTo4creen()8

oi# Dn'aint(ob*ect sen#er, 'aint7 ent6rgs e) : %raphics g < e"%raphics8 )ont $t < ne. )ont(='urisa=, 10)8 4oli#2rush br < ne. 4oli#2rush(Color"2lac3)8 'oint) pt < ne. 'oint)(20"0$, 20"0$)8 g"9ra.4tring(=Meet /ou #o.nstairs in the bar an# hear#=, $t, br, pt)8 pt < ne. 'oint)(20"0$, >0"0$)8 g"9ra.4tring(=Vour rolle# up slee es an# /our s3ull t-shirt=, $t, br, pt < ne. 'oint)(20"0$, 80"0$)8 g"9ra.4tring(=Vou sa/ .h/ #i# /ou #o it .ith him to#a/Z=, $t, br, pt)8 pt < ne. 'oint)(20"0$, 110"0$)8 g"9ra.4tring(=6n# sni$$e# me out li3e ! .as tanOuera/=, $t, br, pt)8 pt < ne. 'oint)(20"0$, 1S0"0$)8 g"9ra.4tring(=Cause /ou[re m/ $ella, m/ gu/=, $t, br, pt)8 pt < ne. 'oint)(20"0$, 1R0"0$)8 g"9ra.4tring(=5an# me /our stella an# $l/=, $t, br, pt)8 pt < ne. 'oint)(20"0$, 220"0$)8 g"9ra.4tring(=2/ the time ![m out the #oor=, $t, br, pt)8 pt < ne. 'oint)(20"0$, 2>0"0$)8 g"9ra.4tring(=Vou tear me #o.n li3e roger moore=, $t, br, pt)8 pt < ne. 'oint)(20"0$, @00"0$)8 g"9ra.4tring(=! cheate# m/sel$=, $t, br, pt)8 pt < ne. 'oint)(20"0$, @@0"0$)8 g"9ra.4tring(=(i3e ! 3ne. ! .oul#=, $t, br, pt)8 pt < ne. 'oint)(20"0$, @S0"0$)8 g"9ra.4tring(=! tol# /a, ! .as trouble=, $t, br, pt)8 pt < ne. 'oint)(20"0$, @R0"0$)8 g"9ra.4tring(=Vou 3no. that ![m no goo#=, $t, br, pt)8 g"9ispose()8 ? ?

pt)8

class M6pplication : public static oi# Main() : 6pplication"1un(ne. M)orm())8 ? ?

In our e)ample+ ,e dra, lyrics o# a song on t e "in#orms #orm$


)ont $t < ne. )ont(='urisa=, 10)8

"e use t e Purisa #ont+ o# 8; pts eig t$


'oint) pt < ne. 'oint)(20"0$, 20"0$)8

To dra, string on t e #orm+ ,e must use #loating point values$

g"9ra.4tring(=Meet /ou #o.nstairs in the bar an# hear#=, $t, br, pt)8

T e +ra3String() met od takes t e #ollo,ing parameters2 te)t to dra,+ #ont+ brus and t e $ointF ob-ect$

Figure2 Lyrics

+ra3ing image
In our last e)ample ,e ,ill dra, an image on t e Form control$
re#roc3"cs using 4/stem8 using 4/stem"9ra.ing8 using 4/stem"+in#o.s")orms8 class M)orm , )orm : pri ate 2itmap castle8 public M)orm() : Te;t < =1e# 1oc3=8 loa#!mage()8 Client4i0e < ne. 4i0e(castle"+i#th, castle"5eight)8 'aint C< ne. 'aint7 ent5an#ler(Dn'aint)8 CenterTo4creen()8 ? oi# loa#!mage() :

? :

tr/ : castle < ne. 2itmap(=re#roc3"png=)8 ? catch (7;ception e) : Console"+rite(ine(e"Message)8 7n ironment"7;it(1)8 ? oi# Dn'aint(ob*ect sen#er, 'aint7 ent6rgs e) %raphics g < e"%raphics8 1ectangle r < ne. 1ectangle(1, 1, castle"+i#th, castle"5eight)8 g"9ra.!mage(castle, r)8

? ?

class M6pplication : public static oi# Main() : 6pplication"1un(ne. M)orm())8 ? ?

T is code e)ample dra,s an image o# a castle on t e #orm$


tr/ : castle < ne. 2itmap(=re#roc3"png=)8 ? catch (7;ception e) : Console"+rite(ine(e"Message)8 7n ironment"7;it(1)8 ?

"e load an image o# a castle$


1ectangle r < ne. 1ectangle(1, 1, castle"+i#th, castle"5eight)8

"e determine t e rectangle t at ,e ,ill dra,$


g"9ra.!mage(castle, r)8

T is line actually dra,s t e image$

Figure2 Image

In t is c apter+ ,e did some painting in Mono "in#orms library$

Sna%e in Mono Winforms


JKCC googleLadLclient D ?pubCME;NE;MEA88M8A9:?O googleLadL,idt D 8:;O googleLadL eig t D N;;O googleLadL#ormat D ?8:;)N;;Las?O googleLadLtype D ?te)t?O BB:;;EC;EC8:2 kanal8 googleLadLc annel D ?E89:M<:G:9?O googleLcolorLborder D ?9D<8//?O googleLcolorLbg D ?FFFFFF?O googleLcolorLlink D ?9D<8//?O googleLcolorLte)t D ?;;;;;;?O googleLcolorLurl D ?;;<;;;?O googleLuiL#eatures D ?rc28;?O BBCCP,indo,$googleLrenderLad56OJKCC googleLadLclient D ?pubCME;NE;MEA88M8A9:?O googleLadL,idt D GN<O googleLadL eig t D N;O googleLadL#ormat D ?GN<)N;Las?O googleLadLtype D ?te)t?O BB:;;EC;EC8:2 ori@ont googleLadLc annel D ?EN;EAA9:9M?O googleLcolorLborder D ?/N/N/N?O googleLcolorLbg D ?FFFFFF?O googleLcolorLlink D ?;;;;FF?O googleLcolorLte)t D ?;;;;;;?O googleLcolorLurl D ?;;<;;;?O googleLuiL#eatures D ?rc28;?O BBCCP ,indo,$googleLrenderLad56OIn t is part o# t e Mono "in#orms programming tutorial+ ,e ,ill create a Snake game clone$

Sna%e game
Sna%e is an older classic video game$ It ,as #irst created in late E;s$ Later it ,as broug t to PCs$ In t is game t e player controls a snake$ T e ob-ective is to eat as many apples as possible$ /ac time t e snake eats an apple+ its body gro,s$ T e snake must avoid t e ,alls and its o,n body$ T is game is sometimes called Nibbles$

+evelopment
T e si@e o# eac o# t e -oints o# a snake is 8;p)$ T e snake is controlled ,it t e cursor keys$ Initially t e snake as t ree -oints$ T e game is started by pressing one o# t e cursor keys$ I# t e game is #inis ed+ ,e display &ame 1ver message in t e middle o# t e 3oard$
2oar#"cs using 4/stem8

using using using using using

4/stem"Collections8 4/stem"ComponentMo#el8 4/stem"9ra.ing8 4/stem"9ata8 4/stem"+in#o.s")orms8

public class 2oar# , &serControl : pri pri pri pri pri ate ate ate ate ate const const const const const int int int int int +!9T5 < @008 57!%5T < @008 9DTG4!Y7 < 108 6((G9DT4 < R008 16N9G'D4 < 2A8

pri ate intMN ; < ne. intM6((G9DT4N8 pri ate intMN / < ne. intM6((G9DT4N8 pri ate int #ots8 pri ate int appleG;8 pri ate int appleG/8 pri pri pri pri pri ate ate ate ate ate bool bool bool bool bool le$t < $alse8 right < true8 up < $alse8 #o.n < $alse8 in%ame < true8

pri ate Timer timer8 pri ate 2itmap #ot8 pri ate 2itmap apple8 pri ate 2itmap hea#8 pri ate !Container components8 public int 2D1971G+!9T58 public int T!T(7261G57!%5T8 public 2oar#() : components < ne. Container()8 2ac3Color < Color"2lac38 9ouble2u$$ere# < true8 this"Client4i0e < ne. 4i0e(+!9T5, 57!%5T)8 tr/ : #ot < ne. 2itmap(=#ot"png=)8 apple < ne. 2itmap(=apple"png=)8 hea# < ne. 2itmap(=hea#"png=)8 ? catch (7;ception e) : Console"+rite(ine(e"Message)8 7n ironment"7;it(1)8 ? ? init%ame()8 oi# DnTic3(ob*ect sen#er, 7 ent6rgs e) :

pri ate

i$ (in%ame) : chec36pple()8

? this"1e$resh()8

chec3Collision()8 mo e()8

pri ate

oi# init%ame() :

#ots < @8 $or (int 0 < 08 0 U #ots8 0CC) : ;M0N < >0 - 0 H 108 /M0N < >08 ? locate6pple()8 Ke/&p C< ne. Ke/7 ent5an#ler(DnKe/&p)8 timer < ne. Timer(this"components)8 timer"7nable# < true8 timer"!nter al < 1008 timer"Tic3 C< ne. 4/stem"7 ent5an#ler(this"DnTic3)8 'aint C< ne. 'aint7 ent5an#ler(this"Dn'aint)8 ? pri ate oi# Dn'aint(ob*ect sen#er, 'aint7 ent6rgs e) :

%raphics g < e"%raphics8 i$ (in%ame) : g"9ra.!mage(apple, appleG;, appleG/)8 $or (int 0 < 08 0 U #ots8 0CC) : i$ (0 << 0) : g"9ra.!mage(hea#, ;M0N, /M0N)8 ? else : g"9ra.!mage(#ot, ;M0N, /M0N)8 ? ? ? else : gameD er(g)8 ?

pri ate

oi# gameD er(%raphics g) :

4tring msg < =%ame D er=8 4tring)ormat $ormat < ne. 4tring)ormat()8 $ormat"6lignment < 4tring6lignment"Center8 $ormat"(ine6lignment < 4tring6lignment"Center8 g"9ra.4tring(msg, )ont, 2rushes"+hite, Client1ectangle, $ormat)8 timer"4top()8

pri ate

oi# chec36pple() :

i$ ((;M0N << appleG;) EE (/M0N << appleG/)) : #otsCC8 locate6pple()8 ? ? pri ate oi# mo e() :

$or (int 0 < #ots8 0 T 08 0--) : ;M0N < ;M(0 - 1)N8 /M0N < /M(0 - 1)N8 ? i$ (le$t) : ;M0N -< 9DTG4!Y78 ? i$ (right) : ;M0N C< 9DTG4!Y78 ? i$ (up) : /M0N -< 9DTG4!Y78 ? i$ (#o.n) : /M0N C< 9DTG4!Y78 ? ? pri ate oi# chec3Collision() :

$or (int 0 < #ots8 0 T 08 0--) : i$ ((0 T J) EE (;M0N << ;M0N) EE (/M0N << /M0N)) : in%ame < $alse8 ? ? i$ (/M0N T 57!%5T - 9DTG4!Y7 - T!T(7261G57!%5T - 2D1971G+!9T5) : in%ame < $alse8 ? i$ (/M0N U 0) : in%ame < $alse8 ? i$ (;M0N T +!9T5 - 9DTG4!Y7 - 2 H 2D1971G+!9T5) : in%ame < $alse8 ? i$ (;M0N U 0) : in%ame < $alse8 ?

pri ate oi# locate6pple() : 1an#om ran# < ne. 1an#om()8 int r < (int)(ran#"Ne;t(16N9G'D4))8 appleG; < ((r H 9DTG4!Y7))8 r < (int)(ran#"Ne;t(16N9G'D4))8 appleG/ < ((r H 9DTG4!Y7))8

? pri ate oi# DnKe/&p(ob*ect sen#er, Ke/7 ent6rgs e) :

int 3e/ < (int) e"Ke/Co#e8 i$ ((3e/ le$t up < #o.n ? << (int) Ke/s"(e$t) EE (Xright)) : < true8 $alse8 < $alse8

i$ ((3e/ << (int) Ke/s"1ight) EE (Xle$t)) : right < true8 up < $alse8 #o.n < $alse8 ? i$ ((3e/ << (int) Ke/s"&p) EE (X#o.n)) : up < true8 right < $alse8 le$t < $alse8 ? i$ ((3e/ << (int) Ke/s"9o.n) EE (Xup)) : #o.n < true8 right < $alse8 le$t < $alse8 ?

? ?

First ,e ,ill de#ine t e constants used in our game$ T e WI+T, and ,&I-,T constants determine t e si@e o# t e 3oard$ T e +'T/SI9& is t e si@e o# t e apple and t e dot o# t e snake$ T e A##/+'TS constant de#ines t e ma)imum number o# possible dots on t e 3oard$ 5M;; D 9;;R9;;B8;R8;6 T e 7AN+/$'S constant is used to calculate a random position o# an apple$ T e +&#A6 constant determines t e speed o# t e game$
pri ate intMN ; < ne. intM6((G9DT4N8 pri ate intMN / < ne. intM6((G9DT4N8

T ese t,o arrays store )+ y coordinates o# all -oints o# a snake$ In t e move() met od ,e ave t e key algorit m o# t e game$ To understand it+ look at o, t e snake is moving$ %ou control t e ead o# t e snake$ %ou can c ange its direction ,it t e cursor keys$ T e rest o# t e -oints move one position up t e c ain$ T e second -oint moves , ere t e #irst ,as+ t e t ird -oint , ere t e second ,as etc$
$or (int 0 < #ots8 0 T 08 0--) : ;M0N < ;M(0 - 1)N8 /M0N < /M(0 - 1)N8 ?

T is code moves t e -oints up t e c ain$


i$ (le$t) : ;M0N -< 9DTG4!Y78 ?

Move t e ead to t e le#t$

In t e chec%Collision() met od+ ,e determine i# t e snake as it itsel# or one o# t e ,alls$


$or (int 0 < #ots8 0 T 08 0--) : i$ ((0 T J) EE (;M0N << ;M0N) EE (/M0N << /M0N)) : in%ame < $alse8 ? ?

Finis t e game+ i# t e snake its one o# its -oints ,it t e ead$


i$ (/M0N T 57!%5T - 9DTG4!Y7 - T!T(7261G57!%5T - 2D1971G+!9T5) : in%ame < $alse8 ?

Finis t e game+ i# t e snake its t e bottom o# t e 3oard$


4na3e"cs using 4/stem8 using 4/stem"9ra.ing8 using 4/stem"+in#o.s")orms8 class 4na3e , )orm : public 4na3e() : Te;t < =4na3e=8 9ouble2u$$ere# < true8 )orm2or#er4t/le < )orm2or#er4t/le")i;e#4ingle8 int bor#er+i#th < (this"+i#th - this"Client4i0e"+i#th) - 28 int title2ar5eight < this"5eight - this"Client4i0e"5eight - bor#er+i#th8 2oar# boar# < ne. 2oar#()8 boar#"2D1971G+!9T5 < bor#er+i#th8 boar#"T!T(7261G57!%5T < title2ar5eight8 Controls"6##(boar#)8 CenterTo4creen()8 ? ?

class M6pplication : public static oi# Main() : 6pplication"1un(ne. 4na3e())8 ? ?

T is is t e main class$

Figure2 Snake T is ,as t e Snake game programmed using t e Mono "in#orms library$

You might also like