0% found this document useful (0 votes)
70 views

Mono C Winforms Tutorial

This document provides an overview and introduction to the Mono C# Winforms tutorial. It discusses the basics of the Mono platform and Winforms library. It summarizes the goals of the tutorial, which is to teach beginner programmers the fundamentals of GUI programming in Mono Winforms. The tutorial will cover topics like controls, menus, dialogs, painting and more. Code examples are provided and explained to demonstrate simple Winforms applications.

Uploaded by

median
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

Mono C Winforms Tutorial

This document provides an overview and introduction to the Mono C# Winforms tutorial. It discusses the basics of the Mono platform and Winforms library. It summarizes the goals of the tutorial, which is to teach beginner programmers the fundamentals of GUI programming in Mono Winforms. The tutorial will cover topics like controls, menus, dialogs, painting and more. Code examples are provided and explained to demonstrate simple Winforms applications.

Uploaded by

median
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
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 !caa, ai 'ratap, Martin aulig,
Mare3 4aar, a*a  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"orms
using 4/stem"9ra.ing

public class 4imple  orm


:
  public 4imple()
  :
  Te;t < =4imple=
  4ie < ne. 4ie(2>0, 200)
  CenterTo4creen()
  ?

  static public oi Main()


  :
  6pplication"un(ne. 4imple())
  ?
?

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"orms
using 4/stem"9ra.ing

public class 4imple  orm


:
  public 4imple()
  :
  Te;t < =4imple=
  4ie < ne. 4ie(2>0, 200)
  CenterTo4creen()
  ?

  static public oi Main()


  :
  6pplication"un(ne. 4imple())
  ?
?

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


using 4/stem"+ino.s"orms
 using 4/stem"9ra.ing

=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=
 4ie < ne. 4ie(2>0, 200)

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 :;):;; p)$
CenterTo4creen()

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


static public oi Main()
 :
  6pplication"un(ne. 4imple())
 

 ?

"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"orms
using 4/stem"9ra.ing
using 4/stem

public class Morm  orm


:
  public Morm()
  :
  Te;t < =!con=
  4ie < ne. 4ie(2>0, 200)

  tr/ :
  !con < ne. !con(=.eb"ico=)
  ? catch (7;ception e) :
  Console"+riteine(e"Message)
  7nironment"7;it(1)
  ?

  CenterTo4creen()
  ?

  static public oi Main()


  :
  6pplication"un(ne. Morm())
  ?
?

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=)
 ? catch (7;ception e) :
  Console"+riteine(e"Message)
  7nironment"7;it(1)
 ?

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 popup ,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/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms

class Morm  orm :

  public Morm() :
  Te;t < =Tooltips=
  4ie < ne. 4ie(2>0, 200)

  ToolTip btnTlp < ne. ToolTip()

  btnTlp"4etToolTip(this, =This is a orm=)

  utton button < ne. utton()


  btnTlp"4etToolTip(button, =This is a utton Control=)
  button"Te;t < =utton=
  button"ocation < ne. 'oint(@0, A0)
  button"'arent < this

  CenterTo4creen()
  ?
?
 

class M6pplication :
  static oi Main() :
  6pplication"un(ne. Morm())
  ?
?

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

=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=)

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


btnTlp"4etToolTip(button, =This is a utton Control=)

And ere #or our button$


utton button < ne. utton()
 btnTlp"4etToolTip(button, =This is a utton Control=)
 button"Te;t < =utton=
 button"ocation < ne. 'oint(@0, A0)
 button"'arent < this

 *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 )9;+ y  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/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms
 

class Morm  orm :


  public Morm() :
  Te;t < =utton=
  4ie < ne. 4ie(2>0, 200)

  utton button < ne. utton()

  button"ocation < ne. 'oint(@0, 20)


  button"Te;t < =Buit=
  button"Clic3 < ne. 7ent5anler(DnClic3)
  button"Mouse7nter < ne. 7ent5anler(Dn7nter)

  Controls"6(button)
  CenterTo4creen()
  ?

  oi DnClic3(ob*ect sener, 7ent6rgs e) :


  Close()
  ?

  oi Dn7nter(ob*ect sener, 7ent6rgs e) :


  Console"+riteine(=utton 7ntere=)
  ?

class M6pplication :
  public static oi Main() :
  6pplication"un(ne. Morm())
  ?
?

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 < ne. 7ent5anler(DnClic3)

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 < ne. 7ent5anler(Dn7nter)

"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()
 ?

Te metod closes te application$


oi Dn7nter(ob*ect sener, 7ent6rgs e) :
  Console"+riteine(=utton 7ntere=)
 ?

"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  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/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms

class Morm  orm :

  public Morm() :
  Te;t < =6nchor=
  4ie < ne. 4ie(210, 210)

  utton btn1 < ne. utton()


  btn1"Te;t < =utton=
  btn1"'arent < this
  btn1"ocation < ne. 'oint(@0, @0)

  utton btn2 < ne. utton()


  btn2"Te;t < =utton=
  btn2"'arent < this
  btn2"ocation < ne. 'oint(@0, 80)
  btn2"6nchor < 6nchor4t/les"ight

  CenterTo4creen()
  ?
?

class M6pplication :
  public static oi Main() :
  Morm m < ne. Morm()
  6pplication"un(m)
  ?
?
 

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  AncorStyles$Le#t$ Te second button as e)plicitely set te
AncorStyles$0igt$
btn2"6nchor < 6nchor4t/les"ight

"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/stem
using 4/stem"9ra.ing
 

using 4/stem"+ino.s"orms

class Morm  orm :

  public Morm() :
  Te;t < =7itor=
  4ie < ne. 4ie(210, 180)

  MainMenu mainMenu < ne. MainMenu()


  Menu!tem ile < mainMenu"Menu!tems"6(=Eile=)
  ile"Menu!tems"6(ne. Menu!tem(=7E;it=,
  ne. 7ent5anler(this"Dn7;it), 4hortcut"CtrlF))

  Menu < mainMenu

  Te;to; tb < ne. Te;to;()


  tb"'arent < this
  tb"9oc3 < 9oc34t/le"ill
  tb"Multiline < true

  4tatusar sb < ne. 4tatusar()


  sb"'arent < this
  sb"Te;t < =ea/=

  CenterTo4creen()
  ?

  oi Dn7;it(ob*ect sener, 7ent6rgs e) :


  Close()
  ?
?

class M6pplication :
  public static oi Main() :
  6pplication"un(ne. Morm())
  ?
?

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

=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"ill

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/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms

class Morm  orm :

  priate int +!9T5 < 2>0


  priate int 57!%5T < 1>0
  priate int &TTDN4G4'6C7 < 1>
  priate int '6N7G4'6C7 < 8
  priate int CD47G4'6C7 < 10

  public Morm() :
  Te;t < =6nchor=
  4ie < ne. 4ie(+!9T5, 57!%5T)

  utton o3 < ne. utton()

  int '6N7G57!%5T < o3"5eight  '6N7G4'6C7

  'anel panel < ne. 'anel()


  panel"5eight < '6N7G57!%5T
  panel"9oc3 < 9oc34t/le"ottom
  panel"'arent < this

int ; < o3"+ith H 2  &TTDN4G4'6C7


  int / < ('6N7G57!%5T - o3"5eight)  2

  o3"Te;t < =D3=


  o3"'arent < panel
  o3"ocation < ne. 'oint(+!9T5-;, /)
  o3"6nchor < 6nchor4t/les"ight

  utton close < ne. utton()


 
 

  ; < close"+ith

  close"Te;t < =Close=


  close"'arent < panel
  close"ocation < ne. 'oint(+!9T5-;-CD47G4'6C7, /)
  close"6nchor < 6nchor4t/les"ight

  CenterTo4creen()
  ?
?

class M6pplication :
  public static oi Main() :
  6pplication"un(ne. Morm())
  ?
?

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>0
 priate int 57!%5T < 1>0

Te WI+T, and ,&I-,T variables determine te ,idt and eigt o# te application ,indo,$
priate int &TTDN4G4'6C7 < 1>
 priate int '6N7G4'6C7 < 8
 priate int CD47G4'6C7 < 10

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  '6N7G4'6C7

=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()
 panel"5eight < '6N7G57!%5T
 panel"9oc3 < 9oc34t/le"ottom
 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=
 o3"'arent < panel
 o3"ocation < ne. 'oint(+!9T5-;, /)
 o3"6nchor < 6nchor4t/les"ight

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/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms

class Morm  orm :

  public Morm() :
  Te;t < ='la/er=
  4ie < ne. 4ie(@>0, 280)

  MainMenu mainMenu < ne. MainMenu()


  Menu!tem ile < mainMenu"Menu!tems"6(=Eile=)
  Menu!tem pla/m < mainMenu"Menu!tems"6(=E'la/=)
  Menu!tem ie. < mainMenu"Menu!tems"6(=EIie.=)

  Menu!tem tools < mainMenu"Menu!tems"6(=ETools=)


  Menu!tem aourites < mainMenu"Menu!tems"6(=Eaourites=)
  Menu!tem help < mainMenu"Menu!tems"6(=E5elp=)
  ile"Menu!tems"6(ne. Menu!tem(=7E;it=,
  ne. 7ent5anler(this"Dn7;it), 4hortcut"CtrlF))

  Menu < mainMenu

  'anel panel < ne. 'anel()


  panel"'arent < this
  panel"ac3Color < Color"lac3
  panel"9oc3 < 9oc34t/le"ill
 
'anel button'anel < ne. 'anel()
  button'anel"'arent < this
  button'anel"5eight < J0
  button'anel"9oc3 < 9oc34t/le"ottom

  utton pause < ne. utton()


  pause"lat4t/le < lat4t/le"'opup
  pause"'arent < button'anel
  pause"ocation < ne. 'oint(>, 10)
  pause"4ie < ne. 4ie(2>, 2>)
  pause"!mage < ne. itmap(=pause"png=)
 

  utton pla/ < ne. utton()


  pla/"lat4t/le < lat4t/le"'opup
  pla/"'arent < button'anel
  pla/"ocation < ne. 'oint(@>, 10)
  pla/"4ie < ne. 4ie(2>, 2>)
  pla/"!mage < ne. itmap(=pla/"png=)

  utton or.ar < ne. utton()


  or.ar"lat4t/le < lat4t/le"'opup
  or.ar"'arent < button'anel
  or.ar"ocation < ne. 'oint(80, 10)
  or.ar"4ie < ne. 4ie(2>, 2>)
  or.ar"!mage < ne. itmap(=or.ar"png=)

  utton bac3.ar < ne. utton()


  bac3.ar"lat4t/le < lat4t/le"'opup
  bac3.ar"'arent < button'anel
  bac3.ar"ocation < ne. 'oint(110, 10)
  bac3.ar"4ie < ne. 4ie(2>, 2>)
  bac3.ar"!mage < ne. itmap(=bac3.ar"png=)

  Trac3ar tb < ne. Trac3ar()


  tb"'arent < button'anel
  tb"Tic34t/le < Tic34t/le"None
  tb"4ie < ne. 4ie(1>0, 2>)
  tb"ocation < ne. 'oint(200, 10)
  tb"6nchor < 6nchor4t/les"ight

  utton auio < ne. utton()


  auio"lat4t/le < lat4t/le"'opup
  auio"'arent < button'anel
  auio"4ie < ne. 4ie(2>, 2>)
  auio"!mage < ne. itmap(=auio"png=)
  auio"ocation < ne. 'oint(1A0, 10)
  auio"6nchor < 6nchor4t/les"ight

  4tatusar sb < ne. 4tatusar()


  sb"'arent < this
  sb"Te;t < =ea/=

  CenterTo4creen()
  ?

  oi Dn7;it(ob*ect sener, 7ent6rgs e) :


  Close()
  ?
?

class M6pplication :
  public static oi Main() :
  6pplication"un(ne. Morm())
  ?
?

Tis is a more comple) e)ample so,ing bot +oc%  and Anchor properties in action$


MainMenu mainMenu < ne. MainMenu()
 Menu!tem ile < mainMenu"Menu!tems"6(=Eile=)
 """
 Menu < mainMenu

=ere ,e create te menubar$


 

 'anel panel < ne. 'anel()


 panel"'arent < this
 panel"ac3Color < Color"lac3
 panel"9oc3 < 9oc34t/le"ill

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()
 button'anel"'arent < this
 button'anel"5eight < J0
 button'anel"9oc3 < 9oc34t/le"ottom

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$
utton pause < ne. utton()
 pause"lat4t/le < lat4t/le"'opup
 pause"'arent < button'anel
 pause"ocation < ne. 'oint(>, 10)
 pause"4ie < ne. 4ie(2>, 2>)
 pause"!mage < ne. itmap(=pause"png=)

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"ight
 """
auio"6nchor < 6nchor4t/les"ight

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/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms

class Morm  orm :


  public Morm() :
  Te;t < =4imple menu=

  Menu4trip ms < ne. Menu4trip()


  ms"'arent < this
 
Tool4tripMenu!tem ile < ne. Tool4tripMenu!tem(=Eile=)
Tool4tripMenu!tem e;it < ne. Tool4tripMenu!tem(=E7;it=, null,
  ne. 7ent5anler(Dn7;it))
e;it"4hortcutKe/s < Ke/s"Control L Ke/s"F
  ile"9rop9o.n!tems"6(e;it)

  ms"!tems"6(ile)
  MainMenu4trip < ms
  4ie < ne. 4ie(2>0, 200)

  CenterTo4creen()
  ?

  oi Dn7;it(ob*ect sener, 7ent6rgs e) :


  Close()
  ?
?

class M6pplication :
  public static oi Main() :
  6pplication"un(ne. Morm())
  ?
?

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()

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=)

=ere ,e create a menu$


Tool4tripMenu!tem e;it < ne. Tool4tripMenu!tem(=E7;it=, null,
  ne. 7ent5anler(Dn7;it))

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


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

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


ile"9rop9o.n!tems"6(e;it)

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)

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


MainMenu4trip < ms

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/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms

class Morm  orm :

  public Morm() :
  Te;t < =4ubmenu=
 

  Menu4trip ms < ne. Menu4trip()


  ms"'arent < this

  Tool4tripMenu!tem ile < ne. Tool4tripMenu!tem(=Eile=)


Tool4tripMenu!tem e;it < ne. Tool4tripMenu!tem(=E7;it=, null,
  ne. 7ent5anler(Dn7;it))

Tool4tripMenu!tem import < ne. Tool4tripMenu!tem()


  import"Te;t < =!mport=

  ile"9rop9o.n!tems"6(import)

  Tool4tripMenu!tem temp < ne. Tool4tripMenu!tem()


  temp"Te;t < =!mport ne.see list"""=
  import"9rop9o.n!tems"6(temp)

  temp < ne. Tool4tripMenu!tem()


  temp"Te;t < =!mport boo3mar3s"""=
  import"9rop9o.n!tems"6(temp)

  temp < ne. Tool4tripMenu!tem()


  temp"Te;t < =!mport mail"""=
  import"9rop9o.n!tems"6(temp)

  ile"9rop9o.n!tems"6(e;it)

  ms"!tems"6(ile)
  MainMenu4trip < ms
  4ie < ne. 4ie(@80, 200)

  CenterTo4creen()
  ?

  oi Dn7;it(ob*ect sener, 7ent6rgs e) :


  Close()
  ?
?

class M6pplication :
  public static oi Main() :
  6pplication"un(ne. Morm())
  ?
?

In tis e)ample+ ,e create one submenu$ Te submenu Import as tree menu items$
Tool4tripMenu!tem import < ne. Tool4tripMenu!tem()
 import"Te;t < =!mport=

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


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

=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/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms

class Morm  orm :

  priate 4tatusar sb


  priate Menu!tem ie.4tatusar

  public Morm() :
  Te;t < =Chec3 menu item=

  sb < ne. 4tatusar()


  sb"'arent < this
  sb"Te;t < =ea/=

  MainMenu mainMenu < ne. MainMenu()

  Menu!tem ile < mainMenu"Menu!tems"6(=Eile=)


ile"Menu!tems"6(ne. Menu!tem(=7E;it=,
ne. 7ent5anler(Dn7;it), 4hortcut"CtrlF))

  Menu!tem ie. < mainMenu"Menu!tems"6(=EIie.=)


  ie.4tatusar < ne. Menu!tem(=Iie. 4tatusar=)
  ie.4tatusar"Chec3e < true
  ie.4tatusar"Clic3 < ne. 7ent5anler(Toggle4tatusar)
  ie."Menu!tems"6(ie.4tatusar)

  Menu < mainMenu


  4ie < ne. 4ie(2>0, 200)

  CenterTo4creen()
  ?

  oi Dn7;it(ob*ect sener, 7ent6rgs e) :


  Close()
  ?

  oi Toggle4tatusar(ob*ect sener, 7ent6rgs e) :


 

  bool chec3 < ie.4tatusar"Chec3e


  i (chec3) :
  sb"Iisible < alse
  ie.4tatusar"Chec3e < alse
  ? else :
  sb"Iisible < true
  ie.4tatusar"Chec3e < true
  ?
  ?

class M6pplication :
  public static oi Main() :
  6pplication"un(ne. Morm())
  ?
?

"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()

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.4tatusar"Chec3e < true

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.4tatusar"Chec3e
 i (chec3) :
  sb"Iisible < alse
  ie.4tatusar"Chec3e < alse
 ? else :
  sb"Iisible < true
  ie.4tatusar"Chec3e < true
 ?

"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/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms

class Morm  orm :

public Morm() :
Te;t < =Menu4trip=
4ie < ne. 4ie(2>0, 200)
 
Menu4trip menu4trip < ne. Menu4trip()

Tool4tripMenu!tem titem1 < ne. Tool4tripMenu!tem(=ile=)


menu4trip"!tems"6(titem1)

Tool4tripMenu!tem titem2 < ne. Tool4tripMenu!tem(=Tools=)


menu4trip"!tems"6(titem2)

Tool4tripMenu!tem subm1 < ne. Tool4tripMenu!tem(=Ne.=)


subm1"!mage < !mage"romile(=ne."png=)
  titem1"9rop9o.n!tems"6(subm1)

Tool4tripMenu!tem subm2 < ne. Tool4tripMenu!tem(=Dpen=)


subm2"!mage < !mage"romile(=open"png=)
  titem1"9rop9o.n!tems"6(subm2)

titem1"9rop9o.n!tems"6(ne. Tool4trip4eparator())

  Tool4tripMenu!tem subm@ < ne. Tool4tripMenu!tem(=7;it=)


subm@"!mage < !mage"romile(=e;it"png=)
  titem1"9rop9o.n!tems"6(subm@)

subm@"Clic3 < Dn7;it


Controls"6(menu4trip)

MainMenu4trip < menu4trip

CenterTo4creen()
  ?

public static oi Main() :


6pplication"un(ne. Morm())
?

oi Dn7;it(ob*ect sener, 7ent6rgs e) :


  Close()
  ?
?

"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.=)
subm1"!mage < !mage"romile(=ne."png=)
 

  titem1"9rop9o.n!tems"6(subm1)

=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())

=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/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms

public class Morm  orm


:
  priate !mageist toolar!cons
  priate Toolarutton sae
  priate Toolarutton e;it
  priate Toolar toolar

  public Morm()
:
  4ie < ne. 4ie(2>0, 200)
  Te;t < =4imple toolbar=

  toolar < ne. Toolar()


  toolar"'arent < this
  toolar!cons < ne. !mageist()
  sae < ne. Toolarutton()
  e;it < ne. Toolarutton()

  sae"!mage!ne; < 0
  sae"Tag < =4ae=
  e;it"!mage!ne; < 1
 

  e;it"Tag < =7;it=

  toolar"!mageist < toolar!cons


  toolar"4ho.ToolTips < true
  toolar"uttons"6ange(ne. Toolarutton :sae, e;it?)
  toolar"uttonClic3 < ne. ToolaruttonClic37ent5anler(DnClic3e)
 
toolar!cons"!mages"6(ne. !con(=ne."ico=))
  toolar!cons"!mages"6(ne. !con(=e;it"ico=))

  CenterTo4creen()
  ?

  static oi Main()


:
  6pplication"un(ne. Morm())
  ?
 
oi DnClic3e(ob*ect sener, ToolaruttonClic37ent6rgs e)
:
  i (e"utton"Tag"7Ouals(=7;it=))
  Close()
  ?
?

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


toolar < ne. Toolar()

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


toolar!cons < ne. !mageist()

An image list is created$


sae < ne. Toolarutton()
 e;it < ne. Toolarutton()

Tese are t,o toolbar buttons$


sae"!mage!ne; < 0

"e determine ,ic icon #rom te image list ,ill be used #or te save toolbar button$
toolar"uttons"6ange(ne. Toolarutton :sae, e;it?)

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


toolar!cons"!mages"6(ne. !con(=ne."ico=))
  toolar!cons"!mages"6(ne. !con(=e;it"ico=))

Icons are added to te image list$


i (e"utton"Tag"7Ouals(=7;it=))
  Close()

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/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms

class Morm  orm :

  string te;t < P=4ometimes ! eel !Qe got to


un 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=

  public Morm() :
  Te;t < =Tainte oe=

  ont ont < ne. ont(=4eri=, 10)

  abel l/rics < ne. abel()


 

  l/rics"'arent < this


  l/rics"Te;t < te;t
  l/rics"ont < ont
  l/rics"ocation < ne. 'oint(10, 10)
  l/rics"4ie < ne. 4ie (2R0, 2R0)

  CenterTo4creen()

  ?
?

class M6pplication :
  public static oi Main() :
  6pplication"un(ne. Morm())
  ?
?

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


abel l/rics < ne. abel()

abel control is created$
string te;t < P=4ometimes ! eel !Qe got """

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


ont ont < ne. ont(=4eri=, 10)
 """
 l/rics"ont < ont

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/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms

class Morm  orm :

  priate Chec3o; cb

  public Morm() :
  Te;t < =Chec3o;=
  4ie < ne. 4ie(220, 1A0)

  cb < ne. Chec3o;()


  cb"'arent < this
  cb"ocation < ne. 'oint(@0, @0)
  cb"Te;t < =4ho. Title=
  cb"Chec3e < true

  cb"Chec3eChange < ne. 7ent5anler(DnChange)

  CenterTo4creen()
  ?

  oi DnChange(ob*ect sener, 7ent6rgs e) :


  i (cb"Chec3e) :
  Te;t < =Chec3o;=
  ? else :
  Te;t < ==
  ?
  ?
?

class M6pplication :
  public static oi Main() :
  6pplication"un(ne. Morm())
  ?
?

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

Chec%"ox control is created$
cb"Te;t < =4ho. Title=
 cb"Chec3e < true

"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 < ne. 7ent5anler(DnChange)

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


i (cb"Chec3e) :
  Te;t < =Chec3o;=
 

 ? else :
  Te;t < ==
 ?

=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/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms

class Morm  orm :


 
'ictureo; pb
  Trac3ar tb
  itmap mute, min, me, ma;

  public Morm() :
  Te;t < =Trac3ar=
  4ie < ne. 4ie(2S0, 1R0)

  tb < ne. Trac3ar()


  tb"'arent < this
  tb"4ie < ne. 4ie(1S0, @0)
  tb"ocation < ne. 'oint(20, J0)
  tb"Tic34t/le < Tic34t/le"None

  tb"IalueChange < ne. 7ent5anler(DnChange)


 
oa!mages()

  pb < ne. 'ictureo;()


  pb"'arent < this
  pb"ocation < ne. 'oint(210, >0)
  pb"!mage < mute
 
CenterTo4creen()

  ?
 

  oi oa!mages() :
  mute < ne. itmap(=mute"png=)
  min < ne. itmap(=min"png=)
  me < ne. itmap(=me"png=)
  ma; < ne. itmap(=ma;"png=)
  ?

  oi DnChange(ob*ect sener, 7ent6rgs e) :


  int al < tb"Ialue

  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. Morm())
  ?
?

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. Trac3ar()

Trac%"ar  control is created$


tb"Tic34t/le < Tic34t/le"None

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


pb < ne. 'ictureo;()
 """
 pb"!mage < mute

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. itmap(=mute"png=)
  min < ne. itmap(=min"png=)
  me < ne. itmap(=me"png=)
  ma; < ne. itmap(=ma;"png=)
 ?

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


int al < tb"Ialue

 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;
 ?

"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 dropdo,n list$ Te user can
select a value #rom te dropdo,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/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms

class Morm  orm :

  priate Comboo; cb


  priate abel label

  public Morm() :
  Te;t < =Comboo;=
  4ie < ne. 4ie(2J0, 2J0)

  cb < ne. Comboo;()


  cb"'arent < this
  cb"ocation < ne. 'oint(>0, @0)

  cb"!tems"6ange(ne. ob*ect :=&buntu=,


  =Manria=,
  =e 5at=,
  =eora=,
  =%entoo=?)

  cb"4electionChangeCommitte < ne. 7ent5anler(DnChange)

  label < ne. abel()


  label"ocation < ne. 'oint(>0, 1J0)
  label"'arent < this
  label"Te;t < ="""=

  CenterTo4creen()
  ?
 

  oi DnChange(ob*ect sener, 7ent6rgs e) :


  Comboo; combo < (Comboo;) sener
  label"Te;t < combo"Te;t
  ?
?

class M6pplication :
  public static oi Main() :
  6pplication"un(ne. Morm())
  ?
?

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. Comboo;()

Combo"ox control is created$
cb"!tems"6ange(ne. ob*ect :=&buntu=,
  =Manria=,
  =e 5at=,
  =eora=,
  =%entoo=?)

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


cb"4electionChangeCommitte < ne. 7ent5anler(DnChange)

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


oi DnChange(ob*ect sener, 7ent6rgs e) :
  Comboo; combo < (Comboo;) sener
  label"Te;t < combo"Te;t
 ?

=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/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms

class Morm  orm :

  priate MonthCalenar calenar


  priate abel ate

  public Morm() :
  Te;t < =Month Calenar=
  4ie < ne. 4ie(2J0, 2J0)

  calenar < ne. MonthCalenar()


  calenar"'arent < this
  calenar"ocation < ne. 'oint(20, 20)
  calenar"9ate4electe < ne. 9ateange7ent5anler(Dn4electe)

  ate < ne. abel()


  ate"ocation < ne. 'oint(J0, 1A0)
  ate"'arent < this
  9ateTime t < calenar"4election4tart
  ate"Te;t < t"Month  ==  t"9a/  ==  t"Vear

  CenterTo4creen()
  ?

  oi Dn4electe(ob*ect sener, 7ent6rgs e) :


  9ateTime t < calenar"4election4tart
  ate"Te;t < t"Month  ==  t"9a/  ==  t"Vear
  ?
?

class M6pplication :
  public static oi Main() :
  6pplication"un(ne. Morm())
  ?
?

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


priate MonthCalenar calenar
  priate abel ate

"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"4election4tart
  ate"Te;t < t"Month  ==  t"9a/  ==  t"Vear
  ?

"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/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms

class Morm  orm :

  priate abel te;t

  public Morm() :
  Te;t < =Te;to;=
  4ie < ne. 4ie(2>0, 200)
  CenterTo4creen()

  te;t < ne. abel()


  te;t"'arent < this
  te;t"Te;t < ="""=
  te;t"ocation < ne. 'oint(S0, J0)
  te;t"6uto4ie < true

  Te;to; tbo; < ne. Te;to;()


  tbo;"'arent < this
  tbo;"ocation < ne. 'oint(S0, 100)
  tbo;"Ke/&p < ne. Ke/7ent5anler(DnKe/&p)

  ?

  oi DnKe/&p(ob*ect sener, Ke/7ent6rgs e) :


  Te;to; tb < (Te;to;) sener
  this"te;t"Te;t < tb"Te;t
  ?
?

class M6pplication :
  public static oi Main() :
  6pplication"un(ne. Morm())
  ?
?
 

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()
 """
 te;t"6uto4ie < true

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;to; tbo; < ne. Te;to;()
 """
 tbo;"Ke/&p < ne. Ke/7ent5anler(DnKe/&p)

"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;to; tb < (Te;to;) sener
  this"te;t"Te;t < tb"Te;t
 ?

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/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms

class Morm  orm :

  priate 4tatusar sb


 

  public Morm() :
  Te;t < =isto;=
  4ie < ne. 4ie(210, 210)

  isto; lb < ne. isto;()


  lb"'arent < this
  lb"!tems"6(=Wessica=)
  lb"!tems"6(=achel=)
  lb"!tems"6(=6ngelina=)
  lb"!tems"6(=6m/=)
  lb"!tems"6(=Wennier=)
  lb"!tems"6(=4carlett=)

  lb"9oc3 < 9oc34t/le"ill


  lb"4electe!ne;Change < ne. 7ent5anler(DnChange)

  sb < ne. 4tatusar()


  sb"'arent < this

  CenterTo4creen()
  ?

  oi DnChange(ob*ect sener, 7ent6rgs e) :


  isto; lb < (isto;) sener
  sb"Te;t < lb"4electe!tem"To4tring()
  ?
?

class M6pplication :
  public static oi Main() :
  6pplication"un(ne. Morm())
  ?
?

1ur e)ample so,s a listbo) ,it si) names$ Te selected item is so,n in te statusbar$
isto; lb < ne. isto;()
 lb"'arent < this

ist"ox control is created$
lb"!tems"6(=Wessica=)

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 < ne. 7ent5anler(DnChange)

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


isto; lb < (isto;) sener
 sb"Te;t < lb"4electe!tem"To4tring()

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 4/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms
using 4/stem"Collections"%eneric

public class 6ctress


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

class Morm  orm :

  priate 4tatusar sb

  public Morm() :
  Te;t < =istIie.=
  4ie < ne. 4ie(@>0, @00)

  istU6ctress actresses < ne. istU6ctress()

  actresses"6(ne. 6ctress(=Wessica 6lba=, 1R81))


  actresses"6(ne. 6ctress(=6ngelina Wolie=, 1RA>))
  actresses"6(ne. 6ctress(=Natalie 'ortman=, 1R81))
  actresses"6(ne. 6ctress(=achel +eiss=, 1RA1))
  actresses"6(ne. 6ctress(=4carlett Wohansson=, 1R8J))
 

  Column5eaer name < ne. Column5eaer()


  name"Te;t < =Name=
  name"+ith < -1
  Column5eaer /ear < ne. Column5eaer()
  /ear"Te;t < =Vear=

  4uspena/out()

  istIie. l < ne. istIie.()


  l"'arent < this
  l"ullo.4elect < true
  l"%riines < true
  l"6llo.Columneorer < true
  l"4orting < 4ortDrer"6scening
  l"Columns"6ange(ne. Column5eaer :name, /ear?)
  l"ColumnClic3 < ne. ColumnClic37ent5anler(ColumnClic3)

  oreach (6ctress act in actresses) :


  istIie.!tem item < ne. istIie.!tem()
  item"Te;t < act"name
  item"4ub!tems"6(act"/ear"To4tring())
  l"!tems"6(item)
  ?

  l"9oc3 < 9oc34t/le"ill


  l"Clic3 < ne. 7ent5anler(DnChange)

  sb < ne. 4tatusar()


  sb"'arent < this
  l"Iie. < Iie."9etails

  esumea/out()

  CenterTo4creen()
  ?

  oi DnChange(ob*ect sener, 7ent6rgs e) :


  istIie. l < (istIie.) sener
  string name < l"4electe!tems0"4ub!tems0"Te;t
  string born < l"4electe!tems0"4ub!tems1"Te;t
  sb"Te;t < name  =, =  born
  ?

  oi ColumnClic3(ob*ect sener, ColumnClic37ent6rgs e)


  :
  istIie. l < (istIie.) sener

  i (l"4orting << 4ortDrer"6scening) :


  l"4orting < 4ortDrer"9escening
  ? else :
  l"4orting < 4ortDrer"6scening
  ?
?
?

class M6pplication :
  public static oi Main() :
  6pplication"un(ne. Morm())
  ?
?
 

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$


istU6ctress actresses < ne. istU6ctress()

 actresses"6(ne. 6ctress(=Wessica 6lba=, 1R81))


 actresses"6(ne. 6ctress(=6ngelina Wolie=, 1RA>))
 """

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


Column5eaer name < ne. Column5eaer()
 name"Te;t < =Name=
 name"+ith < -1

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

ist2ie3 control is created$
l"ullo.4elect < true
 l"%riines < true
 l"6llo.Columneorer < true
l"4orting < 4ortDrer"6scening

=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ange(ne. Column5eaer :name, /ear?)

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


oreach (6ctress act in actresses) :
  istIie.!tem item < ne. istIie.!tem()
  item"Te;t < act"name
  item"4ub!tems"6(act"/ear"To4tring())
  l"!tems"6(item)
 ?

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

Te ist2ie3 control can ave di##erent vie,s$ Di##erent vie,s display data di##erently$
istIie. l < (istIie.) sener
 string name < l"4electe!tems0"4ub!tems0"Te;t
 string born < l"4electe!tems0"4ub!tems1"Te;t
 sb"Te;t < name  =, =  born
 

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ing
 ? else :
  l"4orting < 4ortDrer"6scening
 ?

=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/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms

class Morm  orm :

  4tatusar sb

  public Morm() :
  Te;t < =TreeIie.=
  4ie < ne. 4ie(2>0, 2>0)

  TreeIie. t < ne. TreeIie.()

  TreeNoe root < ne. TreeNoe()


  root"Te;t < =anguages=

  TreeNoe chil1 < ne. TreeNoe()


  chil1"Te;t < ='/thon=

  TreeNoe chil2 < ne. TreeNoe()


  chil2"Te;t < =ub/=

  TreeNoe chil@ < ne. TreeNoe()


 

  chil@"Te;t < =Waa=

  root"Noes"6ange(ne. TreeNoe :chil1, chil2, chil@?)

  t"'arent < this


  t"Noes"6(root)
  t"9oc3 < 9oc34t/le"ill
  t"6ter4elect < ne. TreeIie.7ent5anler(6ter4elect)

  sb < ne. 4tatusar()


  sb"'arent < this

  CenterTo4creen()
  ?

  oi 6ter4elect(ob*ect sener, TreeIie.7ent6rgs e)


  :
  sb"Te;t < e"Noe"Te;t
  ?
?

class M6pplication :
  public static oi Main() :
  6pplication"un(ne. Morm())
  ?
?

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.()

"e create te Tree2ie3 control$


TreeNoe root < ne. TreeNoe()
 root"Te;t < =anguages=
 """
  t"Noes"6(root)

=ere ,e create a root node$


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

Cild node is created in a similar ,ay$


root"Noes"6ange(ne. TreeNoe :chil1, chil2, chil@?)

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 indept$
irectories"cs

using 4/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms
using 4/stem"!D

public class Morm  orm


:
  priate TreeIie. t
  priate utton e;pan
  priate utton e;pan6ll
  priate utton collapse
  priate utton collapse6ll
  priate 4tatusar sb

  priate const string 5DM7G9! < =homerons3i*=

  public Morm()
  :
  4ie < ne. 4ie(J00, J00)
  Te;t < =9irectories=

  t < ne. TreeIie.()

  4uspena/out()

  t"'arent < this


  t"ocation < ne. 'oint(10,10)
  t"4ie < ne. 4ie(Client4ie"+ith - 20, 5eight - 200)
  t"6nchor < 6nchor4t/les"Top L 6nchor4t/les"et L
6nchor4t/les"ight 

  t"ullo.4elect < alse


t"4ho.ines < true
t"4ho.'lusMinus < true
t"4crollable < true
t"6ter4elect < ne. TreeIie.7ent5anler(6ter4elect)
 

  e;pan < ne. utton()


  e;pan"'arent < this
  e;pan"ocation < ne. 'oint(20, t"ottom  20)
  e;pan"Te;t < =7;pan=
  e;pan"6nchor < 6nchor4t/les"et L 6nchor4t/les"Top
  e;pan"Clic3 < ne. 7ent5anler(Dn7;pan)

  e;pan6ll < ne. utton()


  e;pan6ll"'arent < this
  e;pan6ll"ocation < ne. 'oint(20, e;pan"ottom  >)
  e;pan6ll"Te;t < =7;pan 6ll=
  e;pan6ll"6nchor < 6nchor4t/les"et L 6nchor4t/les"Top
  e;pan6ll"Clic3 < ne. 7ent5anler(Dn7;pan6ll)

  collapse < ne. utton()


  collapse"'arent < this
  collapse"ocation < ne. 'oint(e;pan6ll"ight  >, e;pan"Top )
  collapse"Te;t < =Collapse=
  collapse"6nchor < 6nchor4t/les"et L 6nchor4t/les"Top
  collapse"Clic3 < ne. 7ent5anler(DnCollapse)

  collapse6ll < ne. utton()


  collapse6ll"'arent < this
  collapse6ll"ocation < ne. 'oint(collapse"et, collapse"ottom  >)
  collapse6ll"Te;t < =Collapse 6ll=
  collapse6ll"6nchor < 6nchor4t/les"et L 6nchor4t/les"Top
  collapse6ll"Clic3 < ne. 7ent5anler(DnCollapse6ll)

  sb < ne. 4tatusar()


  sb"'arent < this

  4ho.9irectories(t"Noes, 5DM7G9!)

  esumea/out()

  CenterTo4creen()
  ?

  oi 6ter4elect(ob*ect sener, TreeIie.7ent6rgs e)


  :
  sb"Te;t < e"Noe"Te;t
  ?

  oi 4ho.9irectories(TreeNoeCollection trNoe, string path)


  :
  9irector/!no ir!no < ne. 9irector/!no(path)
  i (ir!no X< null)
  :
  9irector/!no sub9irs < ir!no"%et9irectories()
  TreeNoe tr < ne. TreeNoe(ir!no"Name)

  i (sub9irs"ength  0)
  :
  oreach (9irector/!no r in sub9irs)
  :
i (Xr"Name"4tarts+ith(="=))
  4ho.9irectories(tr"Noes, r"ullName)
?
  ?
  trNoe"6(tr)
 

  ?
  ?

  oi Dn7;pan(ob*ect sener, 7ent6rgs e)


  :
  t"4electeNoe"7;pan()
  ?

  oi Dn7;pan6ll(ob*ect sener, 7ent6rgs e)


  :
  t"7;pan6ll()
  ?

  oi DnCollapse(ob*ect sener, 7ent6rgs e)


  :
  t"4electeNoe"Collapse()
  ?

  oi DnCollapse6ll(ob*ect sener, 7ent6rgs e)


  :
  t"Collapse6ll()
  ?

  static oi Main()


:
  6pplication"un(ne. Morm())
  ?

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 < true

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

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  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)
?

"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)
 

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()
  ?

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 4/stem
using 4/stem"!D
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms

class Morm  orm :

  priate Toolar toolbar


  priate Toolarutton open
  priate 4tatusar statusbar

  public Morm() :
  Te;t < =olerro.ser9ialog=
 
toolbar < ne. Toolar()
  open < ne. Toolarutton()

  statusbar < ne. 4tatusar()


  statusbar"'arent < this

  toolbar"uttons"6(open)
  toolbar"uttonClic3 < ne. ToolaruttonClic37ent5anler(DnClic3e)

  Controls"6(toolbar)

  CenterTo4creen()
  ?

  oi DnClic3e(ob*ect sener, ToolaruttonClic37ent6rgs e) :


  olerro.ser9ialog ialog < ne. olerro.ser9ialog()

  i (ialog"4ho.9ialog(this) << 9ialogesult"DK) :


  statusbar"Te;t < ialog"4electe'ath
  ?
  ?
?

class M6pplication :
  public static oi Main() :
  6pplication"un(ne. Morm())
  ?
?

"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erro.ser9ialog ialog < ne. olerro.ser9ialog()

Te Folder"ro3ser+ialog  is created$


i (ialog"4ho.9ialog(this) << 9ialogesult"DK) :
  statusbar"Te;t < ialog"4electe'ath
 ?

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/stem
 using 4/stem"9ra.ing
 using 4/stem"+ino.s"orms

class Morm  orm :

  priate Toolar toolbar


  priate Toolarutton open
  priate Color color

  priate int rect+ith < 100


  priate int rect5eight < 100
  priate ectangle r

  public Morm() :
  Te;t < =Color9ialog=

  color < Color"lue


 
toolbar < ne. Toolar()
  open < ne. Toolarutton()

  toolbar"uttons"6(open)
  toolbar"uttonClic3 < ne. ToolaruttonClic37ent5anler(DnClic3e)

  ocateect()

  4et4t/le (Control4t/les"esieera., true)


  Controls"6(toolbar)
  'aint < ne. 'aint7ent5anler(Dn'aint)
 

  CenterTo4creen()
  ?

  oi Dn'aint(ob*ect sener, 'aint7ent6rgs e)


  :
  %raphics g < e"%raphics
  ocateect()

  4olirush brush < ne. 4olirush(color)

  g"illectangle(brush, r)
  ?

  oi DnClic3e(ob*ect sener, ToolaruttonClic37ent6rgs e) :


  Color9ialog ialog < ne. Color9ialog()
  i (ialog"4ho.9ialog(this) << 9ialogesult"DK) :
  color < ialog"Color
  !naliate()
  ?
  ?

  oi ocateect() :
  int ; < (Client4ie"+ith - rect+ith)  2
  int / < (Client4ie"5eight - rect5eight)  2
  r < ne. ectangle(;, /, rect+ith, rect5eight)
  ?
?

class M6pplication :
  public static oi Main() :
  6pplication"un(ne. Morm())
  ?
?

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"lue

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()

Te Color+ialog is created$


i (ialog"4ho.9ialog(this) << 9ialogesult"DK) :
  color < ialog"Color
  !naliate()
 ?

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/stem
 using 4/stem"9ra.ing
 using 4/stem"+ino.s"orms

class Morm  orm :

  priate abel te;t


  priate Toolar toolbar
  priate Toolarutton open

  public Morm() :
  Te;t < =ont9ialog=

  te;t < ne. abel()


  te;t"'arent < this
  te;t"Te;t < =+inorms tutorial=

  ocateTe;t()
 
toolbar < ne. Toolar()
  toolbar"'arent < this
  open < ne. Toolarutton()

  toolbar"uttons"6(open)
  toolbar"uttonClic3 < ne. ToolaruttonClic37ent5anler(DnClic3e)

  te;t"6uto4ie < true


  esie < ne. 7ent5anler(Dnesie)

  CenterTo4creen()
  ?
 

  oi Dnesie(ob*ect sener, 7ent6rgs e):


  ocateTe;t()
  ?

  oi DnClic3e(ob*ect sener, ToolaruttonClic37ent6rgs e) :


  ont9ialog ialog < ne. ont9ialog()
  i (ialog"4ho.9ialog(this) << 9ialogesult"DK) :
  te;t"ont < ialog"ont
  ocateTe;t()
  ?
  ?

  oi ocateTe;t() :
  te;t"Top < (this"Client4ie"5eight - te;t"5eight)  2
  te;t"et < (this"Client4ie"+ith - te;t"+ith)  2
  ?
?

class M6pplication :
  public static oi Main() :
  6pplication"un(ne. Morm())
  ?
?

"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()

Font+ialog is created$
i (ialog"4ho.9ialog(this) << 9ialogesult"DK) :
  te;t"ont < ialog"ont
  ocateTe;t()
 ?

"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 4/stem
using 4/stem"!D
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms

class Morm  orm :

  priate Toolar toolbar


  priate Toolarutton open
  priate Te;to; te;tbo;

  public Morm() :
  Te;t < =Dpenile9ialog=

  toolbar < ne. Toolar()


  open < ne. Toolarutton()

  te;tbo; < ne. Te;to;()


  te;tbo;"Multiline < true
  te;tbo;"4crollars < 4crollars"oth
  te;tbo;"+or+rap < alse
  te;tbo;"'arent < this
  te;tbo;"9oc3 < 9oc34t/le"ill
 

toolbar"uttons"6(open)
  toolbar"uttonClic3 < ne. ToolaruttonClic37ent5anler(DnClic3e)

  Controls"6(toolbar)
  Controls"6(te;tbo;)

  CenterTo4creen()
  ?

  oi DnClic3e(ob*ect sener, ToolaruttonClic37ent6rgs e) :


  Dpenile9ialog ialog < ne. Dpenile9ialog()
  ialog"ilter < =C# iles (H"cs)LH"cs=

  i (ialog"4ho.9ialog(this) << 9ialogesult"DK) :


  4treameaer reaer < ne. 4treameaer(ialog"ileName)
  string ata < reaer"eaTo7n()
  reaer"Close()
  te;tbo;"Te;t < ata
  ?
  ?
?

class M6pplication :
  public static oi Main() :
  6pplication"un(ne. Morm())
  ?
?

"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()
 

Te 'pen+ialog is created$


ialog"ilter < =C# iles (H"cs)LH"cs=

"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) << 9ialogesult"DK) :
  4treameaer reaer < ne. 4treameaer(ialog"ileName)
  string ata < reaer"eaTo7n()
  reaer"Close()
  te;tbo;"Te;t < ata
 ?

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


JK
googleadclient  ?pubE;NE;E8889:?O
googlead,idt  8:;O
googleadeigt  N;;O
googlead#ormat  ?8:;)N;;as?O
googleadtype  ?te)t?O
BB:;;E;E8:2 kanal8
googleadcannel  ?E89:<:G:9?O
googlecolorborder  ?9D<8//?O
 

googlecolorbg  ?FFFFFF?O
googlecolorlink  ?9D<8//?O
googlecolorte)t  ?;;;;;;?O
googlecolorurl  ?;;<;;;?O
googleui#eatures  ?rc28;?O
BB,indo,$googlerenderad56OJK
googleadclient  ?pubE;NE;E8889:?O
googlead,idt  GN<O
googleadeigt  N;O
googlead#ormat  ?GN<)N;as?O
googleadtype  ?te)t?O
BB:;;E;E8:2 ori@ont
googleadcannel  ?EN;E9:9?O
googlecolorborder  ?/N/N/N?O
googlecolorbg  ?FFFFFF?O
googlecolorlink  ?;;;;FF?O
googlecolorte)t  ?;;;;;;?O
googlecolorurl  ?;;<;;;?O
googleui#eatures  ?rc28;?O
BB
,indo,$googlerenderad56OT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+ draganddrop 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/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms

public class Morm  orm


:
  priate bool is9ragging < alse
  priate int olF, olV
  priate utton button

  public Morm()
  :
  Te;t < =9rag E rop button=
  4ie < ne. 4ie(2A0, 180)

  button < ne. utton()


  button"'arent < this
  button"Cursor < Cursors"5an
  button"Te;t < =utton=
  button"ocation < ne. 'oint(20, 20)
 

  button"Mouse9o.n < ne. Mouse7ent5anler(DnMouse9o.n)


  button"Mouse&p < ne. Mouse7ent5anler(DnMouse&p)
  button"MouseMoe < ne. Mouse7ent5anler(DnMouseMoe)

  CenterTo4creen()
  ?

  public static oi Main()


:
  6pplication"un(ne. Morm())
  ?

  priate oi DnMouse9o.n(ob*ect sener, Mouse7ent6rgs e)


:
  is9ragging < true
  olF < e"F
  olV < e"V
  ?

  priate oi DnMouseMoe(ob*ect sener, Mouse7ent6rgs e)


:
  i (is9ragging)
:
  button"Top < button"Top  (e"V - olV)
button"et < button"et  (e"F - olF)
?
  ?

  priate oi DnMouse&p(ob*ect sener, Mouse7ent6rgs e)


:
  is9ragging < alse
  ?
?

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 < alse
  priate int olF, olV

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 < ne. Mouse7ent5anler(DnMouse9o.n)
  button"Mouse&p < ne. Mouse7ent5anler(DnMouse&p)
  button"MouseMoe < ne. Mouse7ent5anler(DnMouseMoe)

"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 < true
  olF < e"F
  olV < e"V
  ?
 

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  (e"V - olV)
button"et < button"et  (e"F - olF)
?
  ?

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/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms

public class Morm  orm


:

  priate Te;to; te;to;


  priate utton button

  public Morm()
  :
  !nitorm()
  CenterTo4creen()
  ?

  priate oi DnMouse9o.n(ob*ect sener, Mouse7ent6rgs e)


 

  :
  Te;to; t;t < (Te;to;) sener
  t;t"9o9rag9rop(t;t"Te;t, 9rag9rop7ects"Cop/)
  ?

  priate oi Dn9rag7nter(ob*ect sener, 9rag7ent6rgs e)


  :
  e"7ect < 9rag9rop7ects"Cop/
  ?

  priate oi Dn9rag9rop(ob*ect sener, 9rag7ent6rgs e)


  :
  utton button < (utton) sener
  button"Te;t < (string) e"9ata"%et9ata(9ataormats"Te;t)
  ?

  priate oi !nitorm()


  :
  Te;t < =9rag E rop=
  button < ne. utton()
  te;to; < ne. Te;to;()
  4uspena/out()

  button"6llo.9rop < true


  button"ocation < ne. 'oint(1>0, >0)
  te;to;"ocation < ne. 'oint(1>, >0)

  button"9rag9rop < ne. 9rag7ent5anler(Dn9rag9rop)


  button"9rag7nter < ne. 9rag7ent5anler(Dn9rag7nter)
  te;to;"Mouse9o.n < ne. Mouse7ent5anler(DnMouse9o.n)

  Client4ie < ne. 4ie(2>0, 200)


  Controls"6(button)
  Controls"6(te;to;)
  esumea/out()
  ?

  public static oi Main(string args)


  :
  6pplication"un(ne. Morm())
  ?

"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()

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()
 """
 esumea/out()

"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 < true

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


 

 button"9rag9rop < ne. 9rag7ent5anler(Dn9rag9rop)


 button"9rag7nter < ne. 9rag7ent5anler(Dn9rag7nter)
 te;to;"Mouse9o.n < ne. Mouse7ent5anler(DnMouse9o.n)

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;to; t;t < (Te;to;) sener
  t;t"9o9rag9rop(t;t"Te;t, 9rag9rop7ects"Cop/)
 ?

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/
 ?

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)
 :
  utton button < (utton) sener
  button"Te;t < (string) e"9ata"%et9ata(9ataormats"Te;t)
 ?

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/stem
using 4/stem"9ra.ing
 

using 4/stem"+ino.s"orms

public class !mage9rag9rop  orm


:
  priate bool is9ragging
  priate int olF, olV

  priate ectangle ropect


  priate 'ictureo; pico;
  priate itmap image
  priate rush brush

  public !mage9rag9rop()
  :
  Client4ie < ne. 4ie(@>0, 2>0)
  Te;t < =9ragging !mage=
  'aint < ne. 'aint7ent5anler(Dn'aint)
 
is9ragging < alse
  ropect < ne. ectangle(10, 10, 200, 1S0)
  brush < rushes"%ra/
  pico; < ne. 'ictureo;()
  loa!mage()

  pico;"'arent < this


  pico;"ocation < ne. 'oint(100, >0)
  pico;"4ie < ne. 4ie(image"+ith, image"5eight)
  pico;"!mage < image
  pico;"Cursor < Cursors"5an

  pico;"Mouse9o.n < ne. Mouse7ent5anler(DnMouse9o.n)


  pico;"Mouse&p < ne. Mouse7ent5anler(DnMouse&p)
  pico;"MouseMoe < ne. Mouse7ent5anler(DnMouseMoe)

  CenterTo4creen()
  ?

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

  public static oi Main()


:
  6pplication"un(ne. !mage9rag9rop())
  ?

  priate oi DnMouse9o.n(ob*ect sener, Mouse7ent6rgs e)


:
  is9ragging < true
  olF < e"F
  olV < e"V
  ?

  priate oi DnMouseMoe(ob*ect sener, Mouse7ent6rgs e)


:
  i (is9ragging)
:
  pico;"Top < pico;"Top  (e"V - olV)
 

  pico;"et < pico;"et  (e"F - olF)


  ?
  ?

  priate oi DnMouse&p(ob*ect sener, Mouse7ent6rgs e)


:
  is9ragging < alse

  i(ropect"Contains(pico;"ouns)) :
  brush < rushes"%ol
  ? else :
  brush < rushes"%ra/
  ?

  eresh()
  ?

  priate oi Dn'aint(ob*ect sener, 'aint7ent6rgs e)


  :
  %raphics g < e"%raphics
  g"illectangle(brush, ropect)
  ?
?

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 < rushes"%ra/

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. itmap(=image"*pg=)
  ? catch :
  Console"+riteine(=7rror reaing image=)
  7nironment"7;it(1)
  ?
  ?

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


i (ropect"Contains(pico;"ouns)) :
  brush < rushes"%ol
 ? else :
  brush < rushes"%ra/
 ?

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$
eresh()

"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


JK
googleadclient  ?pubE;NE;E8889:?O
googlead,idt  8:;O
googleadeigt  N;;O
googlead#ormat  ?8:;)N;;as?O
googleadtype  ?te)t?O
BB:;;E;E8:2 kanal8
googleadcannel  ?E89:<:G:9?O
googlecolorborder  ?9D<8//?O
googlecolorbg  ?FFFFFF?O
googlecolorlink  ?9D<8//?O
googlecolorte)t  ?;;;;;;?O
googlecolorurl  ?;;<;;;?O
googleui#eatures  ?rc28;?O
BB,indo,$googlerenderad56OJK
googleadclient  ?pubE;NE;E8889:?O
googlead,idt  GN<O
googleadeigt  N;O
googlead#ormat  ?GN<)N;as?O
googleadtype  ?te)t?O
BB:;;E;E8:2 ori@ont
googleadcannel  ?EN;E9:9?O
googlecolorborder  ?/N/N/N?O
googlecolorbg  ?FFFFFF?O
googlecolorlink  ?;;;;FF?O
googlecolorte)t  ?;;;;;;?O
googlecolorurl  ?;;<;;;?O
googleui#eatures  ?rc28;?O
BB
,indo,$googlerenderad56OIn 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 4/stem
using 4/stem"9ra.ing
using 4/stem"9ra.ing"9ra.ing29
using 4/stem"+ino.s"orms

class Morm  orm :

  public Morm() :
  Te;t < =ines=
  4ie < ne. 4ie(280, 2A0)
  esieera. < true

  'aint < ne. 'aint7ent5anler(Dn'aint)


  CenterTo4creen()
  ?

  oi Dn'aint(ob*ect sener, 'aint7ent6rgs e)


  :
%raphics g < e"%raphics
  'en pen < ne. 'en(Color"lac3, 1)

  pen"9ash4t/le < 9ash4t/le"9ot


  g"9ra.ine(pen, 20, J0, 2>0, J0)

  pen"9ash4t/le < 9ash4t/le"9ash9ot


  g"9ra.ine(pen, 20, 80, 2>0, 80)

  pen"9ash4t/le < 9ash4t/le"9ash


  g"9ra.ine(pen, 20, 120, 2>0, 120)

  pen"9ash4t/le < 9ash4t/le"9ash9ot9ot


  g"9ra.ine(pen, 20, 1S0, 2>0, 1S0)

  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. Morm())
  ?
?

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

 esieera. < true

"en ,e resi@e te #orm+ it is automatically redra,n$ Tis is not te de#ault beavior$
'aint < ne. 'aint7ent5anler(Dn'aint)

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"%raphics

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"lac3, 1)

 pen"9ash4t/le < 9ash4t/le"9ot


 g"9ra.ine(pen, 20, J0, 2>0, J0)

"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. loat :S, 8, 1, 1, 1, 1, 1, 1 ?

Tere are several builtin +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()

"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 4/stem
using 4/stem"9ra.ing
using 4/stem"9ra.ing"9ra.ing29
using 4/stem"+ino.s"orms

class Morm  orm :

  public Morm() :
  Te;t < =Colors=
  4ie < ne. 4ie(@S0, @00)
httpetcoe"comtutorialsmono.inormstutorialimages
  'aint < ne. 'aint7ent5anler(Dn'aint)

  CenterTo4creen()
  ?

  oi Dn'aint(ob*ect sener, 'aint7ent6rgs e)


  :
%raphics g < e"%raphics
 
g"illectangle(rushes"4ienna, 10, 1>, R0, S0)
  g"illectangle(rushes"%reen, 1@0, 1>, R0, S0)
  g"illectangle(rushes"Maroon, 2>0, 1>, R0, S0)
  g"illectangle(rushes"Chocolate, 10, 10>, R0, S0)
  g"illectangle(rushes"%ra/, 1@0, 10>, R0, S0)
  g"illectangle(rushes"Coral, 2>0, 10>, R0, S0)
  g"illectangle(rushes"ro.n, 10, 1R>, R0, S0)
  g"illectangle(rushes"Teal, 1@0, 1R>, R0, S0)
  g"illectangle(rushes"%olenro, 2>0, 1R>, R0, S0)

  g"9ispose()httpetcoe"comtutorialsmono.inormstutorialimages

  ?
?

class M6pplication :
  public static oi Main() :
  6pplication"un(ne. Morm())
  ?
?

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


g"illectangle(rushes"4ienna, 10, 1>, R0, S0)

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 builtin patterns+
tat ,e can use$
hatches"cs

using 4/stem
using 4/stem"9ra.ing
using 4/stem"9ra.ing"9ra.ing29
using 4/stem"+ino.s"orms

class Morm  orm :

  public Morm() :
Te;t < =5atches=
  4ie < ne. 4ie(@S0, @00)

  'aint < ne. 'aint7ent5anler(Dn'aint)

  CenterTo4creen()
  ?

  oi Dn'aint(ob*ect sener, 'aint7ent6rgs e)


  :
%raphics g < e"%raphics
 
5atchrush hb < ne. 5atchrush(5atch4t/le"Cross, Color"lac3,
this"ac3Color)
  g"illectangle(hb, 10, 1>, R0, S0)

  hb < ne. 5atchrush(5atch4t/le"'ercent0>, Color"lac3, this"ac3Color)


  g"illectangle(hb, 1@0, 1>, R0, S0)

  hb < ne. 5atchrush(5atch4t/le"4oli9iamon, Color"lac3,


this"ac3Color)
  g"illectangle(hb, 2>0, 1>, R0, S0)

  hb < ne. 5atchrush(5atch4t/le"9iagonalric3, Color"lac3,


this"ac3Color)
 

  g"illectangle(hb, 10, 10>, R0, S0)

  hb < ne. 5atchrush(5atch4t/le"9iot, Color"lac3, this"ac3Color)


  g"illectangle(hb, 1@0, 10>, R0, S0)

  hb < ne. 5atchrush(5atch4t/le"+ae, Color"lac3, this"ac3Color)


  g"illectangle(hb, 2>0, 10>, R0, S0)

  hb < ne. 5atchrush(5atch4t/le"YigYag, Color"lac3, this"ac3Color)


  g"illectangle(hb, 10, 1R>, R0, S0)

  hb < ne. 5atchrush(5atch4t/le"4phere, Color"lac3, this"ac3Color)


  g"illectangle(hb, 1@0, 1R>, R0, S0)

  hb < ne. 5atchrush(5atch4t/le"4hingle, Color"lac3, this"ac3Color)


  g"illectangle(hb, 2>0, 1R>, R0, S0)

  hb"9ispose()
  g"9ispose()
  ?
?

class M6pplication :
  public static oi Main() :
  6pplication"un(ne. Morm())
  ?
?

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

=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"illectangle(hb, 10, 1>, R0, S0)

"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 4/stem
using 4/stem"9ra.ing
using 4/stem"9ra.ing"9ra.ing29
using 4/stem"+ino.s"orms

class Morm  orm :

  public Morm() :
  Te;t < =%raients=
  4ie < ne. 4ie(@>0, @>0)

  'aint < ne. 'aint7ent5anler(Dn'aint)

  CenterTo4creen()
  ?

  oi Dn'aint(ob*ect sener, 'aint7ent6rgs e)


  :
%raphics g < e"%raphics

  'oint pt1 < ne. 'oint(>, >)


  'oint pt2 < ne. 'oint(2>, 2>)
  rush lg < ne. inear%raientrush(pt1, pt2, Color"e, Color"lac3)
  g"illectangle(lg, 20, 20, @00, J0)
 
pt1 < ne. 'oint(>, 2>)
  pt2 < ne. 'oint(20, 2)
  lg < ne. inear%raientrush(pt1, pt2, Color"Vello., Color"lac3)
  g"illectangle(lg, 20, 80, @00, J0)

  pt1 < ne. 'oint(>, 2>)


  pt2 < ne. 'oint(2, 2)
  lg < ne. inear%raientrush(pt1, pt2, Color"%reen, Color"lac3)
  g"illectangle(lg, 20, 1J0, @00, J0)

  pt1 < ne. 'oint(2>, 2>)


  pt2 < ne. 'oint(1>, 2>)
  lg < ne. inear%raientrush(pt1, pt2, Color"lue, Color"lac3)
  g"illectangle(lg, 20, 200, @00, J0)
 
pt1 < ne. 'oint(0, 10)
  pt2 < ne. 'oint(0, 20)
  lg < ne. inear%raientrush(pt1, pt2, Color"Drange, Color"lac3)
  g"illectangle(lg, 20, 2S0, @00, J0)

lg"9ispose()
  g"9ispose()
  ?
?

class M6pplication :
  public static oi Main() :
  6pplication"un(ne. Morm())
  ?
?
 

"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>)

Tese t,o are te controlling points o# te linear gradient brus$
rush lg < ne. inear%raientrush(pt1, pt2, Color"e, Color"lac3)

"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 4/stem
using 4/stem"9ra.ing
using 4/stem"9ra.ing"9ra.ing29
using 4/stem"+ino.s"orms

class Morm  orm :

  public Morm() :

  Te;t < =Vou 3no. !Qm No %oo=


  4ie < ne. 4ie(@80, J>0)

  'aint < ne. 'aint7ent5anler(Dn'aint)


  CenterTo4creen()
  ?
 

  oi Dn'aint(ob*ect sener, 'aint7ent6rgs e)


  :
%raphics g < e"%raphics
 
ont t < ne. ont(='urisa=, 10)
  4olirush br < ne. 4olirush(Color"lac3)

  'oint pt < ne. 'oint(20"0, 20"0)


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

  pt < ne. 'oint(20"0, >0"0)


g"9ra.4tring(=Vour rolle up sleees an /our s3ull t-shirt=, t, br,
pt)

  pt < ne. 'oint(20"0, 80"0)


g"9ra.4tring(=Vou sa/ .h/ i /ou o it .ith him toa/Z=, t, br, pt)

  pt < ne. 'oint(20"0, 110"0)


g"9ra.4tring(=6n snie me out li3e ! .as tanOuera/=, t, br, pt)

  pt < ne. 'oint(20"0, 1S0"0)


g"9ra.4tring(=Cause /ou[re m/ ella, m/ gu/=, t, br, pt)

  pt < ne. 'oint(20"0, 1R0"0)


g"9ra.4tring(=5an me /our stella an l/=, t, br, pt)

  pt < ne. 'oint(20"0, 220"0)


g"9ra.4tring(=/ the time ![m out the oor=, t, br, pt)

  pt < ne. 'oint(20"0, 2>0"0)


g"9ra.4tring(=Vou tear me o.n li3e roger moore=, t, br, pt)

  pt < ne. 'oint(20"0, @00"0)


g"9ra.4tring(=! cheate m/sel=, t, br, pt)

  pt < ne. 'oint(20"0, @@0"0)


g"9ra.4tring(=i3e ! 3ne. ! .oul=, t, br, pt)

  pt < ne. 'oint(20"0, @S0"0)


g"9ra.4tring(=! tol /a, ! .as trouble=, t, br, pt)

  pt < ne. 'oint(20"0, @R0"0)


g"9ra.4tring(=Vou 3no. that ![m no goo=, t, br, pt)

  g"9ispose()
  ?
?

class M6pplication :
  public static oi Main() :
  6pplication"un(ne. Morm())
  ?
?

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


ont t < ne. ont(='urisa=, 10)

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


'oint pt < ne. 'oint(20"0, 20"0)

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)

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/stem
 using 4/stem"9ra.ing
 using 4/stem"+ino.s"orms

class Morm  orm :

  priate itmap castle

  public Morm() :
  Te;t < =e oc3=
  loa!mage()
  Client4ie < ne. 4ie(castle"+ith, castle"5eight)

  'aint < ne. 'aint7ent5anler(Dn'aint)

  CenterTo4creen()
  ?

  oi loa!mage() :
 

  tr/ :
castle < ne. itmap(=reroc3"png=)
  ? catch (7;ception e) :
  Console"+riteine(e"Message)
  7nironment"7;it(1)
  ?
  ?

  oi Dn'aint(ob*ect sener, 'aint7ent6rgs e)


  :
%raphics g < e"%raphics

  ectangle r < ne. ectangle(1, 1, castle"+ith, castle"5eight)


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

class M6pplication :
  public static oi Main() :
  6pplication"un(ne. Morm())
  ?
?

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


tr/ :
castle < ne. itmap(=reroc3"png=)
 ? catch (7;ception e) :
  Console"+riteine(e"Message)
  7nironment"7;it(1)
 ?

"e load an image o# a castle$


ectangle r < ne. ectangle(1, 1, castle"+ith, castle"5eight)

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


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

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


JK
googleadclient  ?pubE;NE;E8889:?O
googlead,idt  8:;O
googleadeigt  N;;O
googlead#ormat  ?8:;)N;;as?O
googleadtype  ?te)t?O
BB:;;E;E8:2 kanal8
googleadcannel  ?E89:<:G:9?O
googlecolorborder  ?9D<8//?O
googlecolorbg  ?FFFFFF?O
googlecolorlink  ?9D<8//?O
googlecolorte)t  ?;;;;;;?O
googlecolorurl  ?;;<;;;?O
googleui#eatures  ?rc28;?O
BB,indo,$googlerenderad56OJK
googleadclient  ?pubE;NE;E8889:?O
googlead,idt  GN<O
googleadeigt  N;O
googlead#ormat  ?GN<)N;as?O
googleadtype  ?te)t?O
BB:;;E;E8:2 ori@ont
googleadcannel  ?EN;E9:9?O
googlecolorborder  ?/N/N/N?O
googlecolorbg  ?FFFFFF?O
googlecolorlink  ?;;;;FF?O
googlecolorte)t  ?;;;;;;?O
googlecolorurl  ?;;<;;;?O
googleui#eatures  ?rc28;?O
BB
,indo,$googlerenderad56OIn 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$
oar"cs

using 4/stem
 

using 4/stem"Collections
using 4/stem"ComponentMoel
using 4/stem"9ra.ing
using 4/stem"9ata
using 4/stem"+ino.s"orms

public class oar  &serControl :

  priate const int +!9T5 < @00


  priate const int 57!%5T < @00
  priate const int 9DTG4!Y7 < 10
  priate const int 6G9DT4 < R00
  priate const int 6N9G'D4 < 2A

  priate int ; < ne. int6G9DT4


  priate int / < ne. int6G9DT4

  priate int ots


  priate int appleG;
  priate int appleG/

  priate bool let < alse


  priate bool right < true
  priate bool up < alse
  priate bool o.n < alse
  priate bool in%ame < true

  priate Timer timer

  priate itmap ot


  priate itmap apple
  priate itmap hea

  priate !Container components


 
public int D97G+!9T5
  public int T!T76G57!%5T

  public oar() :

  components < ne. Container()


  ac3Color < Color"lac3
  9oubleuere < true
  this"Client4ie < ne. 4ie(+!9T5, 57!%5T)

  tr/ :
  ot < ne. itmap(=ot"png=)
  apple < ne. itmap(=apple"png=)
  hea < ne. itmap(=hea"png=)

  ? catch (7;ception e) :
  Console"+riteine(e"Message)
  7nironment"7;it(1)
  ?

init%ame()
  ?

  priate oi DnTic3(ob*ect sener, 7ent6rgs e) :

  i (in%ame) :
  chec36pple()
 

  chec3Collision()
  moe()
  ?
  this"eresh()
  ?

  priate oi init%ame() :

  ots < @

  or (int  < 0  U ots ) :


  ; < >0 -  H 10
  / < >0
  ?

  locate6pple()
  Ke/&p < ne. Ke/7ent5anler(DnKe/&p)

  timer < ne. Timer(this"components)


  timer"7nable < true
  timer"!nteral < 100
  timer"Tic3 < ne. 4/stem"7ent5anler(this"DnTic3)

  'aint < ne. 'aint7ent5anler(this"Dn'aint)

  ?

  priate oi Dn'aint(ob*ect sener, 'aint7ent6rgs e) :

  %raphics g < e"%raphics

  i (in%ame) :

  g"9ra.!mage(apple, appleG;, appleG/)

  or (int  < 0  U ots ) :


  i ( << 0) :
  g"9ra.!mage(hea, ;, /)
  ? else :
  g"9ra.!mage(ot, ;, /)
?
  ?

  ? else :
  gameDer(g)
  ?
  ?

  priate oi gameDer(%raphics g) :

  4tring msg < =%ame Der=


  4tringormat ormat < ne. 4tringormat()
  ormat"6lignment < 4tring6lignment"Center
  ormat"ine6lignment < 4tring6lignment"Center
 
g"9ra.4tring(msg, ont, rushes"+hite, Clientectangle, ormat)
  timer"4top()
  ?
 

  priate oi chec36pple() :

  i ((;0 << appleG;) EE (/0 << appleG/)) :


  ots
  locate6pple()
  ?
  ?

  priate oi moe() :

  or (int  < ots   0 --) :


  ; < ;( - 1)
  / < /( - 1)
  ?

  i (let) :
  ;0 -< 9DTG4!Y7
  ?

  i (right) :
  ;0 < 9DTG4!Y7
  ?

  i (up) :
  /0 -< 9DTG4!Y7
  ?

  i (o.n) :
  /0 < 9DTG4!Y7
  ?
  ?

  priate oi chec3Collision() :

  or (int  < ots   0 --) :


  i ((  J) EE (;0 << ;) EE (/0 << /)) :
  in%ame < alse
  ?
  ?

  i (/0  57!%5T - 9DTG4!Y7 - T!T76G57!%5T - D97G+!9T5) :


  in%ame < alse
  ?

  i (/0 U 0) :
  in%ame < alse
  ?

  i (;0  +!9T5 - 9DTG4!Y7 - 2 H D97G+!9T5) :


  in%ame < alse
  ?

  i (;0 U 0) :
  in%ame < alse
  ?
  ?

  priate oi locate6pple() :


  anom ran < ne. anom()
  int r < (int)(ran"Ne;t(6N9G'D4))
  appleG; < ((r H 9DTG4!Y7))
  r < (int)(ran"Ne;t(6N9G'D4))
  appleG/ < ((r H 9DTG4!Y7))
 

  ?

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

  int 3e/ < (int) e"Ke/Coe

  i ((3e/ << (int) Ke/s"et) EE (Xright)) :


  let < true
  up < alse
  o.n < alse
  ?

  i ((3e/ << (int) Ke/s"ight) EE (Xlet)) :


  right < true
  up < alse
  o.n < alse
  ?

  i ((3e/ << (int) Ke/s"&p) EE (Xo.n)) :


  up < true
  right < alse
  let < alse
  ?

  i ((3e/ << (int) Ke/s"9o.n) EE (Xup)) :


  o.n < true
  right < alse
  let < alse
  ?
  ?
?

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$ 5;;  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 int ; < ne. int6G9DT4
 priate int / < ne. int6G9DT4

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  < ots   0 --) :
  ; < ;( - 1)
  / < /( - 1)
 ?

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


i (let) :
  ;0 -< 9DTG4!Y7
 ?

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  < ots   0 --) :

  i ((  J) EE (;0 << ;) EE (/0 << /)) :


  in%ame < alse
  ?
 ?

Finis te game+ i# te snake its one o# its -oints ,it te ead$
i (/0  57!%5T - 9DTG4!Y7 - T!T76G57!%5T - D97G+!9T5) :
  in%ame < alse
 ?

Finis te game+ i# te snake its te bottom o# te 3oard$
4na3e"cs

using 4/stem
using 4/stem"9ra.ing
using 4/stem"+ino.s"orms

class 4na3e  orm :

  public 4na3e() :

  Te;t < =4na3e=


  9oubleuere < true
  ormorer4t/le < ormorer4t/le"i;e4ingle
 
int borer+ith < (this"+ith - this"Client4ie"+ith)  2
  int titlear5eight < this"5eight - this"Client4ie"5eight - borer+ith

  oar boar < ne. oar()


  boar"D97G+!9T5 < borer+ith
  boar"T!T76G57!%5T < titlear5eight

  Controls"6(boar)
  CenterTo4creen()

  ?
?

class M6pplication :
  public static oi Main() :
  6pplication"un(ne. 4na3e())
  ?
?

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