Mono C# Winforms Tutorial

Embed Size (px)

Citation preview

  • 5/28/2018 Mono C# Winforms Tutorial

    1/75

    Mono C# Winforms tutorial

    Introduction

    First steps

    Layout management

    Menus & toolbarsControls

    Advanced Controls

    Dialogs

    Drag & Drop

    Painting

    Snake

    Tis is Mono C! "in#orms tutorial$ Mono "in#orms tutorial is #or beginner programmers$

    %ou may ave a look at teT'! tutorial

    or te sister tutorial IronPyton Mono "in#orms tutorial

    Introduction to Mono WinformsTe #irst part o# te Mono "in#orms tutorial introduces te Mono plat#orm and te "in#orms

    library$

    About this tutorial

    Tis is Mono C! "in#orms tutorial$ Mono "in#orms tutorial is #or beginner programmers$ Te goal

    o# tis tutorial is to teac readers basics o# (I programming in Mono "in#orms$ Te tutorial iscreated and tested on Linu)$ *everteless+ it can be used on oter operating systems as ,ell$ Most

    e)amples sould run ,itout modi#ication$ Images used in tis tutorial can be do,nloadedere$

    Mono

    Te Mono Pro-ect is an open development initiative sponsored by Novellto develop an open

    source+ (*I. version o# te Microso#t $*/T development plat#orm$ It is a $*/T compatible set o#

    tools+ ,ic include C! compiler+ Common Language 0untime+ AD1$*/T+ ASP$*/T and

    "in#orms libraries$

    Mono can be divided into tree groups2

    Core components

    nome development stack

    Microso#t compatibility stack

    Te core components are te C! language and te Common language runtime$ Te nome

    development stack includes te T'! library and various database connectivity libraries$ Finally

    te Microso#t compatibility stack includes te AD1$*/T+ ASP$*/T and te "in#orms libraries$

    Mono is multiplat#orm programming plat#orm$ It can be run on Linu)+ 3SD+ Mac 1S .+ Solaris and

    "indo,s operating systems$ It is a multilanguage e##ort$ For no,+ only C! language is #ully

    supported$ Languages like 4isual 3asic or IronPyton are not yet #inised$

    http://zetcode.com/tutorials/monowinformstutorial/introductionhttp://zetcode.com/tutorials/monowinformstutorial/firststepshttp://zetcode.com/tutorials/monowinformstutorial/layouthttp://zetcode.com/tutorials/monowinformstutorial/menustoolbarshttp://zetcode.com/tutorials/monowinformstutorial/controlshttp://zetcode.com/tutorials/monowinformstutorial/advancedcontrolshttp://zetcode.com/tutorials/monowinformstutorial/dialogshttp://zetcode.com/tutorials/monowinformstutorial/dragdrophttp://zetcode.com/tutorials/monowinformstutorial/paintinghttp://zetcode.com/tutorials/monowinformstutorial/snakehttp://zetcode.com/tutorials/gtksharptutorialhttp://zetcode.com/tutorials/gtksharptutorialhttp://zetcode.com/tutorials/ironpythontutorialhttp://zetcode.com/tutorials/monowinformstutorial/images/images.tgzhttp://zetcode.com/tutorials/monowinformstutorial/images/images.tgzhttp://zetcode.com/tutorials/monowinformstutorial/images/images.tgzhttp://zetcode.com/tutorials/monowinformstutorial/firststepshttp://zetcode.com/tutorials/monowinformstutorial/layouthttp://zetcode.com/tutorials/monowinformstutorial/menustoolbarshttp://zetcode.com/tutorials/monowinformstutorial/controlshttp://zetcode.com/tutorials/monowinformstutorial/advancedcontrolshttp://zetcode.com/tutorials/monowinformstutorial/dialogshttp://zetcode.com/tutorials/monowinformstutorial/dragdrophttp://zetcode.com/tutorials/monowinformstutorial/paintinghttp://zetcode.com/tutorials/monowinformstutorial/snakehttp://zetcode.com/tutorials/gtksharptutorialhttp://zetcode.com/tutorials/ironpythontutorialhttp://zetcode.com/tutorials/monowinformstutorial/images/images.tgzhttp://zetcode.com/tutorials/monowinformstutorial/introduction
  • 5/28/2018 Mono C# Winforms Tutorial

    2/75

    Winforms

    "indo,s Forms is a grapical user inter#ace application programming inter#ace 5API6 included as a

    part o# Microso#t7s $*/T Frame,ork$ As o# 89 May :;;

  • 5/28/2018 Mono C# Winforms Tutorial

    3/75

    First steps in Mono WinformsIn tis part o# te Mono "in#orms tutorial+ ,e introduce some basic programs in "in#orms

    programing library$

    SimpleTis is a simple "in#orms application$

    simple"cs

    using 4/stem"+ino.s"ormsusing 4/stem"9ra.ing

    public class 4imple orm: public 4imple() : Te;t < =4imple= 4ie < ne. 4ie(2>0, 200) CenterTo4creen() ?

    static public oi Main() : 6pplication"un(ne. 4imple()) ??

    Tis code e)ample so,s a small ,indo, on te screen$

    using 4/stem"+ino.s"orms

    using 4/stem"9ra.ing

    =ere ,e use te usingdirective+ ,ic allo,s us to use a type #rom an appropriate namespace

    ,itout a #ully >uali#ied name$ For e)ample+ ,e can no, ,rite Form instead o#

    System$"indo,s$Forms$Form$

    public class 4imple orm:"""?

    In "in#orms+ any ,indo, or a dialog is a Form$ Tis control is a basic container+ ,ose purpose is

    to display oter cild controls$ 1ur class+ Simple+ inerits #rom a #orm$ Tis ,ay it becomes a #ormitsel#$

    Te;t < =4imple=4ie < ne. 4ie(2>0, 200)

    Textand Sieare properties o# a #orm$ Canging tese properties+ ,e modi#y our #orm control$ Te

    #irst line displays te)t ?Simple? in te titlebar o# te #orm control$ Te second line sets te si@e o#

    te #orm to :;):;; p)$

    CenterTo4creen()

    Tis metod centers our application on te screen$static public oi Main()

    : 6pplication"un(ne. 4imple())

  • 5/28/2018 Mono C# Winforms Tutorial

    4/75

    ?

    "en compiled and run+ te Main metod is e)ecuted #irst$ Te code instantiates te Simple class

    and runs it$

    $ gmcs -r4/stem"+ino.s"orms"ll -r4/stem"9ra.ing"ll simple"cs

    =ere is o, ,e compile te source code$ I# ,e didn7t make any mistakes+ ,e sould avesimple!exe#ile in our current ,orking directory$

    Figure2 Simple

    Icon

    Mono means monkey in Spanis$ I# ,e do not provide an icon #or our application+ ,e ave a ead

    o# a monkey by de#ault$ Te ne)t e)ample so,s+ o, to cange tis$

    icon"cs

    using 4/stem"+ino.s"ormsusing 4/stem"9ra.ingusing 4/stem

    public class Morm orm: public Morm() : Te;t < =!con= 4ie < ne. 4ie(2>0, 200)

    tr/ : !con < ne. !con(=.eb"ico=) ? catch (7;ception e) : Console"+riteine(e"Message) 7nironment"7;it(1) ?

    CenterTo4creen() ?

    static public oi Main() : 6pplication"un(ne. Morm()) ?

    ?

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

  • 5/28/2018 Mono C# Winforms Tutorial

    5/75

    tat represents te #orm in te taskbar as ,ell as te icon tat is displayed #or te control bo) o# te

    #orm$

    tr/ : !con < ne. !con(=.eb"ico=)? catch (7;ception e) : Console"+riteine(e"Message) 7nironment"7;it(1)?

    It is a good practice to put all input output ,ork bet,een te tryBcatc key,ords$ Te ,eb$ico #ile

    must be available in te current ,orking directory$ Tis is te directory #rom ,ere ,e e)ecute

    5$Bicon$e)e6 our application$

    Figure2 Icon

    TooltipsA tooltip is a small rectangular popup ,indo, tat displays a brie# description o# a control7s

    purpose ,en te user rests te pointer on te control$

    tooltips"cs

    using 4/stemusing 4/stem"9ra.ingusing 4/stem"+ino.s"orms

    class Morm orm :

    public Morm() : Te;t < =Tooltips= 4ie < ne. 4ie(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() ??

  • 5/28/2018 Mono C# Winforms Tutorial

    6/75

    class M6pplication : static oi Main() : 6pplication"un(ne. Morm()) ??

    1ur code e)ample creates a tooltip #or t,o controls$ Te "uttoncontrol and te Formcontrol$ToolTip btnTlp < ne. ToolTip()

    =ere ,e create te ToolTipcontrol$ Tis 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 te creation o# te "uttoncontrol$ Te Textproperty is a label #or te button$ Te ocation

    property places te button on te #orm at )9;+ y E; p) coordinates$ Finally+ te $arentproperty

    determines te container+ ,ere te button ,ill reside$

    Figure2 Tooltips

    "utton

    1ur last code e)ample so,s a button control in action$

    button"cs

    using 4/stemusing 4/stem"9ra.ingusing 4/stem"+ino.s"orms

  • 5/28/2018 Mono C# Winforms Tutorial

    7/75

    class Morm orm : public Morm() : Te;t < =utton= 4ie < ne. 4ie(2>0, 200)

    utton button < ne. utton()

    button"ocation < ne. 'oint(@0, 20) button"Te;t < =Buit= button"Clic3 < ne. 7ent5anler(DnClic3) button"Mouse7nter < ne. 7ent5anler(Dn7nter)

    Controls"6(button) CenterTo4creen() ?

    oi DnClic3(ob*ect sener, 7ent6rgs e) : Close() ?

    oi Dn7nter(ob*ect sener, 7ent6rgs e) :

    Console"+riteine(=utton 7ntere=) ?

    ?

    class M6pplication : public static oi Main() : 6pplication"un(ne. Morm()) ??

    All (I programming is event driven programming$ In our e)ample+ ,e so, a button control on a

    #orm container$ Te button ,ill listen to t,o events$ Te Clic%and te Mouse&nterevents$button"Clic3 < ne. 7ent5anler(DnClic3)

    Tis code line plugs an event andler to te Clic%event$ "en ,e click on te button+ te

    'nClic%()metod is called$

    button"Mouse7nter < ne. 7ent5anler(Dn7nter)

    "en ,e enter te button area ,it te mouse pointer+ te Mouse&nterevent is triggerd$ In tis

    case+ our code calls te 'n&nter()metod$

    oi DnClic3(ob*ect sener, 7ent6rgs e) :

    Close()?

    Te metod closes te application$

    oi Dn7nter(ob*ect sener, 7ent6rgs e) : Console"+riteine(=utton 7ntere=)?

    "en ,e enter te button control area ,it te mouse pointer+ ?3utton /ntered? te)t is displayed in

    te terminal$

    Tis part o# te Mono "in#orms tutorial so,ed some introductory code e)amples to get you

    started ,it te "in#orms programming library$

  • 5/28/2018 Mono C# Winforms Tutorial

    8/75

    a*out management in Mono WinformsTe Mono "in#orms tutorial continues ,it te layout management o# controls$ A#ter ,e ave

    placed controls on teir parent containers+ ,e ave to ensure teir proper layuot$

    AnchorTe Anchorproperty o# a control determines o, it is resi@ed ,it its parent$ Ancor is a term

    #rom te marine ,orld$ "en an ancor is dropped into te ,ater+ te sip is #i)ed in certain place$

    Same applies #or te "in#orms controls$

    /ac control in "in#orms can ave one o# tese AnchorSt*lesvalues2

    Top

    Le#t

    0igt

    3ottom

    *otice+ tat controls are not restricted to one value$ Tey can take any combinatino o# tese values

    using te operator$

    "asic Anchor example

    Te #ollo,ing e)ample so,s a very basic e)ample+ demonstrating te Anchorproperty$

    anchor"cs

    using 4/stemusing 4/stem"9ra.ingusing 4/stem"+ino.s"orms

    class Morm orm :

    public Morm() : Te;t < =6nchor= 4ie < ne. 4ie(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() :

    Morm m < ne. Morm() 6pplication"un(m) ??

  • 5/28/2018 Mono C# Winforms Tutorial

    9/75

    Tis is a very basic code e)ample+ tat clearly so,s ,at te Anchorproperty is all about$ "e

    ave t,o buttons on te #orm$ Te #irst button as te de#ault AnchorSt*lesvalues+ ,ic are

    AncorStyles$Top AncorStyles$Le#t$ Te second button as e)plicitely set te

    AncorStyles$0igt$

    btn2"6nchor < 6nchor4t/les"ight

    "e e)plicitely set te Anchorproperty o# te second button to AncorStyles$0igt value$

    *o, ave a look at te #ollo,ing t,o images$ Te le#t one so,s te application at te beginning$

    Te rigt one so,s te same application a#ter resi@ement$ Te #irst button keeps its distance #rom

    te le#t and top borders o# te #orm$ Te second button keeps its distance #rom te rigt border o#

    te #orm$ 3ut it does not keep any distance in te vertical direction$

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

    +oc%

    Te +oc%property allo,s us to stick a control to a certain edge o# te parent #orm or control$

    Te #ollo,ing are possible +oc%St*levalues$

    Top Le#t

    0igt

    3ottom

    Fill

    *one

    &ditor s%eleton

    Te #ollo,ing code e)ample so,s te +oc%property in action$

    eitor"cs

    using 4/stemusing 4/stem"9ra.ing

  • 5/28/2018 Mono C# Winforms Tutorial

    10/75

    using 4/stem"+ino.s"orms

    class Morm orm :

    public Morm() : Te;t < =7itor= 4ie < ne. 4ie(210, 180)

    MainMenu mainMenu < ne. MainMenu() Menu!tem ile < mainMenu"Menu!tems"6(=Eile=) ile"Menu!tems"6(ne. Menu!tem(=7E;it=, ne. 7ent5anler(this"Dn7;it), 4hortcut"CtrlF))

    Menu < mainMenu

    Te;to; tb < ne. Te;to;() tb"'arent < this tb"9oc3 < 9oc34t/le"ill tb"Multiline < true

    4tatusar sb < ne. 4tatusar() sb"'arent < this sb"Te;t < =ea/=

    CenterTo4creen() ?

    oi Dn7;it(ob*ect sener, 7ent6rgs e) : Close() ??

    class M6pplication : public static oi Main() : 6pplication"un(ne. Morm()) ??

    "e so, a menubar and a statusbar$ And te remaining area is taken by te Text"oxcontrol$

    Te;to; tb < ne. Te;to;()tb"'arent < this

    =ere ,e create te Text"oxcontrol$ Formcontainer is set to be te parent #or te te)t bo)$

    tb"9oc3 < 9oc34t/le"ill

    Tis code line makes te Text"oxcontrol take up te remaining space inside te #orm container$

  • 5/28/2018 Mono C# Winforms Tutorial

    11/75

    Figure2 /ditor skeleton

    Anchored buttons

    Te ne)t e)ample so,s t,o buttons placed in te bottom rigt corner o# te #orm$

    anchorebuttons"cs

    using 4/stemusing 4/stem"9ra.ingusing 4/stem"+ino.s"orms

    class Morm orm :

    priate int +!9T5 < 2>0 priate int 57!%5T < 1>0 priate int &TTDN4G4'6C7 < 1> priate int '6N7G4'6C7 < 8 priate int CD47G4'6C7 < 10

    public Morm() : Te;t < =6nchor= 4ie < ne. 4ie(+!9T5, 57!%5T)

    utton o3 < ne. utton()

    int '6N7G57!%5T < o3"5eight '6N7G4'6C7

    'anel panel < ne. 'anel() panel"5eight < '6N7G57!%5T panel"9oc3 < 9oc34t/le"ottom panel"'arent < this

    int ; < o3"+ith H 2 &TTDN4G4'6C7 int / < ('6N7G57!%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()

  • 5/28/2018 Mono C# Winforms Tutorial

    12/75

    ; < close"+ith

    close"Te;t < =Close= close"'arent < panel close"ocation < ne. 'oint(+!9T5-;-CD47G4'6C7, /) close"6nchor < 6nchor4t/les"ight

    CenterTo4creen() ??

    class M6pplication : public static oi Main() : 6pplication"un(ne. Morm()) ??

    Te e)ample displayes 1'+ Close buttons in te bottom rigt corner o# te ,indo,+ as it is common

    in dialog ,indo,s$

    priate int +!9T5 < 2>0priate int 57!%5T < 1>0

    Te WI+T,and ,&I-,Tvariables determine te ,idt and eigt o# te application ,indo,$

    priate int &TTDN4G4'6C7 < 1>priate int '6N7G4'6C7 < 8priate int CD47G4'6C7 < 10

    Te ".TT'NS/S$AC&is te space bet,een te 1' and te Close button$ Te $AN&/S$AC&

    is te space bet,een te panel and te bottom o# te #orm$ Finally+ te C'S&/S$AC&variable

    sets te space bet,een te Close button and te rigt border o# te #orm$

    int '6N7G57!%5T < o3"5eight '6N7G4'6C7

    =ere ,e compute te eigt o# te panel$ Te eigt o# te panel is based on te eigt o# te 1'

    button$ And ,e add some additional space+ so tat te buttons are not too close to te border$

    'anel panel < ne. 'anel()panel"5eight < '6N7G57!%5Tpanel"9oc3 < 9oc34t/le"ottompanel"'arent < this

    =ere ,e create and manage te $anelcontrol$ In tis e)ample+ it is used as a container #or our

    buttons$ It is glued to te bottom border o# te #orm$ And te buttons are placed ,itin te panel$o3"Te;t < =D3=

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

    Te 1' button7s parent is set to te panel ,idget$ Te location is computed$ And te Anchor

    property is set to te rigt$ Te oter button is created similarly$

  • 5/28/2018 Mono C# Winforms Tutorial

    13/75

    Figure2 Ancored buttons

    $la*er s%eleton

    Te last e)ample o# tis part o# te Mono "in#orms tutorial so,s a more comple) e)ample$ It is a

    skeleton o# a music player$

    pla/er"cs

    using 4/stemusing 4/stem"9ra.ingusing 4/stem"+ino.s"orms

    class Morm orm :

    public Morm() : Te;t < ='la/er= 4ie < ne. 4ie(@>0, 280)

    MainMenu mainMenu < ne. MainMenu() Menu!tem ile < mainMenu"Menu!tems"6(=Eile=) 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 aourites < mainMenu"Menu!tems"6(=Eaourites=) Menu!tem help < mainMenu"Menu!tems"6(=E5elp=) ile"Menu!tems"6(ne. Menu!tem(=7E;it=, ne. 7ent5anler(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"4ie < ne. 4ie(2>, 2>) pause"!mage < ne. itmap(=pause"png=)

  • 5/28/2018 Mono C# Winforms Tutorial

    14/75

    utton pla/ < ne. utton() pla/"lat4t/le < lat4t/le"'opup pla/"'arent < button'anel pla/"ocation < ne. 'oint(@>, 10) pla/"4ie < ne. 4ie(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"4ie < ne. 4ie(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"4ie < ne. 4ie(2>, 2>) bac3.ar"!mage < ne. itmap(=bac3.ar"png=)

    Trac3ar tb < ne. Trac3ar() tb"'arent < button'anel tb"Tic34t/le < Tic34t/le"None tb"4ie < ne. 4ie(1>0, 2>) tb"ocation < ne. 'oint(200, 10) tb"6nchor < 6nchor4t/les"ight

    utton auio < ne. utton() auio"lat4t/le < lat4t/le"'opup auio"'arent < button'anel auio"4ie < ne. 4ie(2>, 2>) auio"!mage < ne. itmap(=auio"png=) auio"ocation < ne. 'oint(1A0, 10)

    auio"6nchor < 6nchor4t/les"ight

    4tatusar sb < ne. 4tatusar() sb"'arent < this sb"Te;t < =ea/=

    CenterTo4creen() ?

    oi Dn7;it(ob*ect sener, 7ent6rgs e) : Close() ??

    class M6pplication : public static oi Main() : 6pplication"un(ne. Morm()) ??

    Tis is a more comple) e)ample so,ing bot +oc%and Anchorproperties in action$

    MainMenu mainMenu < ne. MainMenu()Menu!tem ile < mainMenu"Menu!tems"6(=Eile=)"""

    Menu < mainMenu

    =ere ,e create te menubar$

  • 5/28/2018 Mono C# Winforms Tutorial

    15/75

    'anel panel < ne. 'anel()panel"'arent < thispanel"ac3Color < Color"lac3panel"9oc3 < 9oc34t/le"ill

    Tis is te black panel+ ,ic takes all te remaining space+ le#t by te menubar+ statusbar and te

    control panel$

    'anel button'anel < ne. 'anel()button'anel"'arent < thisbutton'anel"5eight < J0button'anel"9oc3 < 9oc34t/le"ottom

    Tis is te control panel$ Its parent is te #orm container$ It is glued to te bottom o# te #orm$ Its

    eigt is G;p)$ Inside tis control panel+ ,e place all te buttons and te trackar$

    utton pause < ne. utton()pause"lat4t/le < lat4t/le"'opuppause"'arent < button'anelpause"ocation < ne. 'oint(>, 10)

    pause"4ie < ne. 4ie(2>, 2>)pause"!mage < ne. itmap(=pause"png=)

    Te pause button is one o# te #our buttons+ tat as te de#ault Anchorproperty value$ Te style o#

    te button is set to #lat+ because it looks better$ "e put a bitmap on te button$

    tb"6nchor < 6nchor4t/les"ight"""auio"6nchor < 6nchor4t/les"ight

    Te last t,o controls are ancored to te rigt$

    Figure2 Player skeleton

    Tis part o# te Mono "in#orms tutorial ,as about te layout management o# controls$ "e

    practised various possibilities tat te "in#orms library o##ers$

  • 5/28/2018 Mono C# Winforms Tutorial

    16/75

    Menus and toolbars in Mono WinformsIn tis part o# te Mono "in#orms tutorial+ ,e ,ill talk about menus and toolbars$

    A menubar is one o# te most visible parts o# te (I application$ It is a group o# commands

    located in various menus$ "ile in console applications you ad to remember all tose arcane

    commands+ ere ,e ave most o# te commands grouped into logical parts$ Tere are acceptedstandards tat #urter reduce te amount o# time spending to learn a ne, application$

    Simple menu

    In te #irst e)ample+ ,e create a simple menu$

    simplemenu"cs

    using 4/stemusing 4/stem"9ra.ingusing 4/stem"+ino.s"orms

    class Morm orm : public Morm() : Te;t < =4imple menu=

    Menu4trip ms < ne. Menu4trip() ms"'arent < this

    Tool4tripMenu!tem ile < ne. Tool4tripMenu!tem(=Eile=)Tool4tripMenu!tem e;it < ne. Tool4tripMenu!tem(=E7;it=, null,

    ne. 7ent5anler(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 4ie < ne. 4ie(2>0, 200)

    CenterTo4creen() ?

    oi Dn7;it(ob*ect sener, 7ent6rgs e) : Close() ??

    class M6pplication :

    public static oi Main() : 6pplication"un(ne. Morm()) ??

    In our e)ample+ ,e ave a menubar and one menu$ Inside a menu tere is one menu item$ I# ,e

    select te menu item+ application is closed$

    *otice te ,ays o, ,e can close te application$ "e can close it by using te CtrlH. sorcut or

    by pressing Alt+ F+ / keys$

    Menu4trip ms < ne. Menu4trip()

    MenuStripcreates a menu system #or our #orm$ "e add ToolStripMenuItemob-ects to te

    MenuStrip tat represent te individual menu commands in te menu structure$ /ac

    ToolStripMenuItemcan be a command #or your application or a parent menu #or oter submenu

  • 5/28/2018 Mono C# Winforms Tutorial

    17/75

    items$

    Tool4tripMenu!tem ile < ne. Tool4tripMenu!tem(=Eile=)

    =ere ,e create a menu$

    Tool4tripMenu!tem e;it < ne. Tool4tripMenu!tem(=E7;it=, null, ne. 7ent5anler(Dn7;it))

    Tis line creates te e)it menu item$

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

    "e provide a sortcut #or te e)it menu item$

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

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

    ms"!tems"6(ile)

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

    MainMenu4trip < ms

    Te MenuStripis plugged into te #orm$

    Figure2 Simple menu

    Submenu/ac menu item can also ave a submenu$ Tis ,ay ,e can group similar commnads into groups$

    For e)ample ,e can place commands tat ideBso, various toolbars like personal bar+ address bar+

    status bar or navigation bar into a submenu called toolbars$

    submenu"cs

    using 4/stemusing 4/stem"9ra.ingusing 4/stem"+ino.s"orms

    class Morm orm :

    public Morm() : Te;t < =4ubmenu=

  • 5/28/2018 Mono C# Winforms Tutorial

    18/75

    Menu4trip ms < ne. Menu4trip() ms"'arent < this

    Tool4tripMenu!tem ile < ne. Tool4tripMenu!tem(=Eile=)Tool4tripMenu!tem e;it < ne. Tool4tripMenu!tem(=E7;it=, null,

    ne. 7ent5anler(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.see 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 4ie < ne. 4ie(@80, 200)

    CenterTo4creen() ?

    oi Dn7;it(ob*ect sener, 7ent6rgs e) :

    Close() ??

    class M6pplication : public static oi Main() : 6pplication"un(ne. Morm()) ??

    In tis e)ample+ ,e create one submenu$ Te submenu Import as tree menu items$

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

    import"Te;t < =!mport=

    A ToolStripMenuItemcan 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.see list"""= import"9rop9o.n!tems"6(temp)

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

  • 5/28/2018 Mono C# Winforms Tutorial

    19/75

    Figure2 Submenu

    Chec% menu item

    Te ne)t code e)ample demonstrates+ o, to create a cecked menu item$

    chec3menuitem"cs

    using 4/stemusing 4/stem"9ra.ingusing 4/stem"+ino.s"orms

    class Morm orm :

    priate 4tatusar sb priate Menu!tem ie.4tatusar

    public Morm() : Te;t < =Chec3 menu item=

    sb < ne. 4tatusar() sb"'arent < this sb"Te;t < =ea/=

    MainMenu mainMenu < ne. MainMenu()

    Menu!tem ile < mainMenu"Menu!tems"6(=Eile=)ile"Menu!tems"6(ne. Menu!tem(=7E;it=,

    ne. 7ent5anler(Dn7;it), 4hortcut"CtrlF))

    Menu!tem ie. < mainMenu"Menu!tems"6(=EIie.=) ie.4tatusar < ne. Menu!tem(=Iie. 4tatusar=) ie.4tatusar"Chec3e < true ie.4tatusar"Clic3 < ne. 7ent5anler(Toggle4tatusar) ie."Menu!tems"6(ie.4tatusar)

    Menu < mainMenu 4ie < ne. 4ie(2>0, 200)

    CenterTo4creen() ?

    oi Dn7;it(ob*ect sener, 7ent6rgs e) :

    Close() ?

    oi Toggle4tatusar(ob*ect sener, 7ent6rgs e) :

  • 5/28/2018 Mono C# Winforms Tutorial

    20/75

    bool chec3 < ie.4tatusar"Chec3e i (chec3) : sb"Iisible < alse ie.4tatusar"Chec3e < alse ? else : sb"Iisible < true ie.4tatusar"Chec3e < true ? ?

    ?

    class M6pplication : public static oi Main() : 6pplication"un(ne. Morm()) ??

    "e ave t,o menus$ File and 4ie,$ Te 4ie, menu as a menu item tat toggles te visibility o#

    te statusbar$

    MainMenu mainMenu < ne. MainMenu()

    In tis e)ample+ ,e use te MainMenucontrol$ To create a menubar+ ,e can use eiter MainMenu

    or MenuStripcontrols$ Te latter as some additional #unctionality$

    ie.4tatusar"Chec3e < true

    Tis menu item is cecked by de#ault+ because te statusbar is visible #rom te start o# te

    application$

    bool chec3 < ie.4tatusar"Chec3ei (chec3) :

    sb"Iisible < alse ie.4tatusar"Chec3e < alse? else : sb"Iisible < true ie.4tatusar"Chec3e < true?

    "e determine+ i# te menu item is cecked$ "e so, and ide te statusbar and te ceck tick

    depending on te chec%value$

    Figure2 Ceck menu item

  • 5/28/2018 Mono C# Winforms Tutorial

    21/75

    Images0 separator

    "e ,ill #urter enance our kno,ledge o# te MenuStripcontrol$ "e ,ill create a menu item ,it

    an image and so, o, to separate tem ,it a separator$

    menustrip"cs

    using 4/stemusing 4/stem"9ra.ingusing 4/stem"+ino.s"orms

    class Morm orm :

    public Morm() :Te;t < =Menu4trip=4ie < ne. 4ie(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"romile(=ne."png=)

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

    Tool4tripMenu!tem subm2 < ne. Tool4tripMenu!tem(=Dpen=)subm2"!mage < !mage"romile(=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"romile(=e;it"png=)

    titem1"9rop9o.n!tems"6(subm@)

    subm@"Clic3 < Dn7;itControls"6(menu4trip)

    MainMenu4trip < menu4trip

    CenterTo4creen() ?

    public static oi Main() :6pplication"un(ne. Morm())

    ?

    oi Dn7;it(ob*ect sener, 7ent6rgs e) : Close() ??

    "e ave t,o menus in our code e)ample$ File and Tools$ In te File ,e ave tree menu items ,it

    images$ "e ave also one separator$ In tis e)ample+ te png images must be located in te current

    ,orking directory$Tool4tripMenu!tem subm1 < ne. Tool4tripMenu!tem(=Ne.=)subm1"!mage < !mage"romile(=ne."png=)

  • 5/28/2018 Mono C# Winforms Tutorial

    22/75

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

    =ere ,e create te #irst menu item$ To add an image to te item+ ,e set te Imageproperty to our

    image$ "e create an Image #rom te speci#ied #ile using te static FromFile()metod$

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

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

    Figure2 Images and separator

    Tool"ar

    Menus group all commands tat ,e can use in an application$ Toolbars provide a >uick access to

    te most #re>uently used commands$ Tool"arcontrol is used to display Tool"ar"uttoncontrols$

    "e can assign images to te buttons by creating an Imageist$ "e tan assign te image list to teImageistproperty o# te toolbar and assign te image inde) value to te ImageIndexproperty #or

    eac Tool"ar"utton$

    toolbar"cs

    using 4/stemusing 4/stem"9ra.ingusing 4/stem"+ino.s"orms

    public class Morm orm: priate !mageist toolar!cons

    priate Toolarutton sae priate Toolarutton e;it priate Toolar toolar

    public Morm() : 4ie < ne. 4ie(2>0, 200) Te;t < =4imple toolbar=

    toolar < ne. Toolar() toolar"'arent < this toolar!cons < ne. !mageist() sae < ne. Toolarutton() e;it < ne. Toolarutton()

    sae"!mage!ne; < 0 sae"Tag < =4ae= e;it"!mage!ne; < 1

  • 5/28/2018 Mono C# Winforms Tutorial

    23/75

    e;it"Tag < =7;it=

    toolar"!mageist < toolar!cons toolar"4ho.ToolTips < true toolar"uttons"6ange(ne. Toolarutton :sae, e;it?) toolar"uttonClic3 < ne. ToolaruttonClic37ent5anler(DnClic3e)

    toolar!cons"!mages"6(ne. !con(=ne."ico=)) toolar!cons"!mages"6(ne. !con(=e;it"ico=))

    CenterTo4creen() ?

    static oi Main():

    6pplication"un(ne. Morm()) ?oi DnClic3e(ob*ect sener, ToolaruttonClic37ent6rgs e):

    i (e"utton"Tag"7Ouals(=7;it=))

    Close() ??

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

    toolar < ne. Toolar()

    =ere ,e create te Tool"arcontrol$

    toolar!cons < ne. !mageist()

    An image list is created$sae < ne. Toolarutton()

    e;it < ne. Toolarutton()

    Tese are t,o toolbar buttons$

    sae"!mage!ne; < 0

    "e determine ,ic icon #rom te image list ,ill be used #or te save toolbar button$

    toolar"uttons"6ange(ne. Toolarutton :sae, e;it?)

    Te Tool"ar"uttoncontrols are added to te toolbar$

    toolar!cons"!mages"6(ne. !con(=ne."ico=))toolar!cons"!mages"6(ne. !con(=e;it"ico=))

    Icons are added to te image list$

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

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

  • 5/28/2018 Mono C# Winforms Tutorial

    24/75

    Figure2 Tool3ar

    Tis part o# te "in#orms tutorial ,as about menus and toolbars$

    "asic Controls in Mono WinformsTis part o# te 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+ ceck bo)es+ sliders+ list bo)es etc$ /veryting a programmer needs #or

    is -ob$ In tis section o# te tutorial+ ,e ,ill describe several use#ul controls$

    abel Control

    abelis a simple control #or displaying te)t or images$ It does not receive #ocus$

    label"cs

    using 4/stemusing 4/stem"9ra.ingusing 4/stem"+ino.s"orms

    class Morm orm :

    string te;t < P=4ometimes ! eel !Qe got toun a.a/ !Qe got to%et a.a/rom the pain that /ou rie into the heart o meThe loe .e share4eems to go no.here

    !Qe lost m/ lights! toss an turn ! canQt sleep at night

    Dnce ! ran to /ou (! ran)No. !Qll run rom /ouThis tainte loe /ouQe gien! gie /ou all a bo/ coul gie /ouTa3e m/ tears an thatQs not nearl/ allTainte loeTainte loe=

    public Morm() : Te;t < =Tainte oe=

    ont ont < ne. ont(=4eri=, 10)

    abel l/rics < ne. abel()

  • 5/28/2018 Mono C# Winforms Tutorial

    25/75

    l/rics"'arent < this l/rics"Te;t < te;t l/rics"ont < ont l/rics"ocation < ne. 'oint(10, 10) l/rics"4ie < ne. 4ie (2R0, 2R0)

    CenterTo4creen()

    ??

    class M6pplication : public static oi Main() : 6pplication"un(ne. Morm()) ??

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

    abel l/rics < ne. abel()

    abelcontrol is created$

    string te;t < P=4ometimes ! eel !Qe got """

    Te caracter is used to denote a multiline string$

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

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

    Figure2 Label

  • 5/28/2018 Mono C# Winforms Tutorial

    26/75

    Chec%"ox

    Chec%"oxis a control tat as t,o states$ 1n and 1##$ It is a bo) ,it a label or an image$ I# te

    Ceck3o) is cecked+ it is represented by a tick in a bo)$ A Ceck3o) can be used to so,Bide

    splasscreen at startup+ toggle visibility o# a toolbar etc$

    chec3bo;"cs

    using 4/stemusing 4/stem"9ra.ingusing 4/stem"+ino.s"orms

    class Morm orm :

    priate Chec3o; cb

    public Morm() : Te;t < =Chec3o;= 4ie < ne. 4ie(220, 1A0)

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

    cb"Chec3eChange < ne. 7ent5anler(DnChange)

    CenterTo4creen() ?

    oi DnChange(ob*ect sener, 7ent6rgs e) : i (cb"Chec3e) :

    Te;t < =Chec3o;= ? else : Te;t < == ? ??

    class M6pplication : public static oi Main() : 6pplication"un(ne. Morm()) ??

    1ur code e)ample so,s or ides te title o# te ,indo, depending on it7s state$

    cb < ne. Chec3o;()

    Chec%"oxcontrol is created$

    cb"Te;t < =4ho. Title=cb"Chec3e < true

    "en te application starts+ ,e so, te title$ And ,e set te Chec%"oxcontrol to cecked state$

    cb"Chec3eChange < ne. 7ent5anler(DnChange)

    "en ,e click on te Chec%"oxcontrol+ te Chec%edChangedevent is triggered$

    i (cb"Chec3e) : Te;t < =Chec3o;=

  • 5/28/2018 Mono C# Winforms Tutorial

    27/75

    ? else : Te;t < ==?

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

    Figure2 Ceck3o)

    Trac%"ar

    Trac%"aris a component tat lets te user grapically select a value by sliding a knob ,itin a

    bounded interval$ 1ur e)ample ,ill so, a volume control$

    trac3bar"cs

    using 4/stemusing 4/stem"9ra.ingusing 4/stem"+ino.s"orms

    class Morm orm :

    'ictureo; pb Trac3ar tb itmap mute, min, me, ma;

    public Morm() : Te;t < =Trac3ar= 4ie < ne. 4ie(2S0, 1R0)

    tb < ne. Trac3ar() tb"'arent < this

    tb"4ie < ne. 4ie(1S0, @0) tb"ocation < ne. 'oint(20, J0) tb"Tic34t/le < Tic34t/le"None

    tb"IalueChange < ne. 7ent5anler(DnChange)

    oa!mages()

    pb < ne. 'ictureo;() pb"'arent < this pb"ocation < ne. 'oint(210, >0) pb"!mage < mute

    CenterTo4creen()

    ?

  • 5/28/2018 Mono C# Winforms Tutorial

    28/75

    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 sener, 7ent6rgs e) : int al < tb"Ialue

    i (al

  • 5/28/2018 Mono C# Winforms Tutorial

    29/75

    ? else : pb"!mage < ma;?

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

    control$

    Figure2 Track3ar

    Combo"ox

    Combo"oxis a control tat combines a button or editable #ield and a dropdo,n list$ Te user can

    select a value #rom te dropdo,n list+ ,ic appears at te user7s re>uest$ I# you make te combo

    bo) editable+ ten te combo bo) includes an editable #ield into ,ic te user can type a value$

    combobo;"cs

    using 4/stemusing 4/stem"9ra.ingusing 4/stem"+ino.s"orms

    class Morm orm :

    priate Comboo; cb priate abel label

    public Morm() : Te;t < =Comboo;= 4ie < ne. 4ie(2J0, 2J0)

    cb < ne. Comboo;() cb"'arent < this

    cb"ocation < ne. 'oint(>0, @0)

    cb"!tems"6ange(ne. ob*ect :=&buntu=, =Manria=, =e 5at=, =eora=, =%entoo=?)

    cb"4electionChangeCommitte < ne. 7ent5anler(DnChange)

    label < ne. abel() label"ocation < ne. 'oint(>0, 1J0) label"'arent < this

    label"Te;t < ="""=

    CenterTo4creen() ?

  • 5/28/2018 Mono C# Winforms Tutorial

    30/75

    oi DnChange(ob*ect sener, 7ent6rgs e) : Comboo; combo < (Comboo;) sener label"Te;t < combo"Te;t ??

    class M6pplication : public static oi Main() : 6pplication"un(ne. Morm()) ??

    1ur code programming e)ample so,s a combobo) ,it #ive items$ Te selected item is so,n in

    a label control$

    cb < ne. Comboo;()

    Combo"oxcontrol is created$

    cb"!tems"6ange(ne. ob*ect :=&buntu=, =Manria=, =e 5at=, =eora=, =%entoo=?)

    Te Combo"oxcontrol is #illed ,it items$

    cb"4electionChangeCommitte < ne. 7ent5anler(DnChange)

    I# ,e select an item #rom te combobo)+ te SelectionChangeCommittedevent is triggered$

    oi DnChange(ob*ect sener, 7ent6rgs e) : Comboo; combo < (Comboo;) sener label"Te;t < combo"Te;t?

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

    Figure2 Combo3o)

  • 5/28/2018 Mono C# Winforms Tutorial

    31/75

    MonthCalendar

    In te ne)t e)ample+ ,e ,ill so, a MonthCalendarcontrol$ Te MontCalendar control allo,s

    te user to select a date using a visual display$

    monthcalenar"cs

    using 4/stemusing 4/stem"9ra.ingusing 4/stem"+ino.s"orms

    class Morm orm :

    priate MonthCalenar calenar priate abel ate

    public Morm() : Te;t < =Month Calenar= 4ie < ne. 4ie(2J0, 2J0)

    calenar < ne. MonthCalenar() calenar"'arent < this calenar"ocation < ne. 'oint(20, 20) calenar"9ate4electe < ne. 9ateange7ent5anler(Dn4electe)

    ate < ne. abel() ate"ocation < ne. 'oint(J0, 1A0) ate"'arent < this 9ateTime t < calenar"4election4tart ate"Te;t < t"Month == t"9a/ == t"Vear

    CenterTo4creen() ?

    oi Dn4electe(ob*ect sener, 7ent6rgs e) : 9ateTime t < calenar"4election4tart ate"Te;t < t"Month == t"9a/ == t"Vear ??

    class M6pplication : public static oi Main() : 6pplication"un(ne. Morm()) ??

    In te e)ample+ ,e so, a MonthCalendarand a abel$

    priate MonthCalenar calenar priate abel ate

    "e ave t,o controls$ A MonthCalendarand a abel$ Te latter so,s te currently selected date$

    oi Dn4electe(ob*ect sener, 7ent6rgs e) : 9ateTime t < calenar"4election4tart ate"Te;t < t"Month == t"9a/ == t"Vear ?

    "en ,e select a date #rom te MonthCalendar+ te 'nSelected()metod is called$ TeSelectionStartproperty gets te start date o# te selected range o# dates$

  • 5/28/2018 Mono C# Winforms Tutorial

    32/75

    Figure2 MontCalendar

    Text"ox

    Te Text"oxcontrol is used to display or accept some te)t$ Te te)t can be single or multiline$ Tis

    control is also capable o# pass,ord masking$

    te;tbo;"cs

    using 4/stemusing 4/stem"9ra.ingusing 4/stem"+ino.s"orms

    class Morm orm :

    priate abel te;t

    public Morm() : Te;t < =Te;to;= 4ie < ne. 4ie(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"6uto4ie < true

    Te;to; tbo; < ne. Te;to;() tbo;"'arent < this tbo;"ocation < ne. 'oint(S0, 100) tbo;"Ke/&p < ne. Ke/7ent5anler(DnKe/&p)

    ?

    oi DnKe/&p(ob*ect sener, Ke/7ent6rgs e) : Te;to; tb < (Te;to;) sener this"te;t"Te;t < tb"Te;t ??

    class M6pplication : public static oi Main() :

    6pplication"un(ne. Morm()) ??

  • 5/28/2018 Mono C# Winforms Tutorial

    33/75

    Tis e)ample so,s a te)t bo) and a label$ Te te)t tat ,e key in te te)t bo) is displayed

    immediately in te label control$

    te;t < ne. abel()"""te;t"6uto4ie < true

    Te abelcontrol is created$ Te AutoSieproperty ensures+ tat te Label gro,s to so, te te)t$Te;to; tbo; < ne. Te;to;()

    """tbo;"Ke/&p < ne. Ke/7ent5anler(DnKe/&p)

    "e plug in te 1e*.pevent$ "en ,e release te key+ 'n1e*.p()metod is called$

    oi DnKe/&p(ob*ect sener, Ke/7ent6rgs e) : Te;to; tb < (Te;to;) sener this"te;t"Te;t < tb"Te;t?

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

    Figure2 Te)t3o)

    "e ave #inised capter o# te Mono "in#orms tutorial+ dedicated to basic controls$

    Advanced Controls in Mono WinformsIn tis part o# te Mono "in#orms tutorial+ ,e introduce some more advanced controls$ *amely te

    ist"ox+ te ist2ie3and te Tree2ie3control$

    ist"ox Control

    Te ist"oxcontrol is used to display a list o# items$ (sers can select one or more items by

    clicking on tem$

    listbo;"cs

    using 4/stemusing 4/stem"9ra.ingusing 4/stem"+ino.s"orms

    class Morm orm :

    priate 4tatusar sb

  • 5/28/2018 Mono C# Winforms Tutorial

    34/75

    public Morm() : Te;t < =isto;= 4ie < ne. 4ie(210, 210)

    isto; lb < ne. isto;() lb"'arent < this lb"!tems"6(=Wessica=) lb"!tems"6(=achel=) lb"!tems"6(=6ngelina=) lb"!tems"6(=6m/=) lb"!tems"6(=Wennier=) lb"!tems"6(=4carlett=)

    lb"9oc3 < 9oc34t/le"ill lb"4electe!ne;Change < ne. 7ent5anler(DnChange)

    sb < ne. 4tatusar() sb"'arent < this

    CenterTo4creen() ?

    oi DnChange(ob*ect sener, 7ent6rgs e) : isto; lb < (isto;) sener sb"Te;t < lb"4electe!tem"To4tring() ??

    class M6pplication : public static oi Main() : 6pplication"un(ne. Morm()) ??

    1ur e)ample so,s a listbo) ,it si) names$ Te selected item is so,n in te statusbar$

    isto; lb < ne. isto;()lb"'arent < this

    ist"oxcontrol is created$

    lb"!tems"6(=Wessica=)

    Tis is o, ,e add a ne, item to te ist"oxcontrol$ Te control as te Itemsproperty$ Te

    property is a re#erence to te list o# items in a listbo)$ (sing tis re#erence+ ,e can add+ remove or

    get count o# items o# te listbo)$lb"4electe!ne;Change < ne. 7ent5anler(DnChange)

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

    isto; lb < (isto;) senersb"Te;t < lb"4electe!tem"To4tring()

    Inside te 'nChange()metod+ ,e get te re#erence to te listbo) and set te selected te)t to te

    statusbar$

  • 5/28/2018 Mono C# Winforms Tutorial

    35/75

    Figure2 List3o)

    ist2ie3

    ist2ie3control is used to display collections o# items$ It is a more sopisticated control tan te

    ist"oxcontrol$ It can display data in various vie,s is mostly used to display data in multicolumn

    vie,s$

    listie."cs

    using 4/stemusing 4/stem"9ra.ingusing 4/stem"+ino.s"ormsusing 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 Morm orm :

    priate 4tatusar sb

    public Morm() : Te;t < =istIie.= 4ie < ne. 4ie(@>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))

  • 5/28/2018 Mono C# Winforms Tutorial

    36/75

    Column5eaer name < ne. Column5eaer() name"Te;t < =Name= name"+ith < -1 Column5eaer /ear < ne. Column5eaer() /ear"Te;t < =Vear=

    4uspena/out()

    istIie. l < ne. istIie.() l"'arent < this l"ullo.4elect < true l"%riines < true l"6llo.Columneorer < true l"4orting < 4ortDrer"6scening l"Columns"6ange(ne. Column5eaer :name, /ear?) l"ColumnClic3 < ne. ColumnClic37ent5anler(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. 7ent5anler(DnChange)

    sb < ne. 4tatusar() sb"'arent < this l"Iie. < Iie."9etails

    esumea/out()

    CenterTo4creen() ?

    oi DnChange(ob*ect sener, 7ent6rgs e) : istIie. l < (istIie.) sener string name < l"4electe!tems0"4ub!tems0"Te;t string born < l"4electe!tems0"4ub!tems1"Te;t sb"Te;t < name =, = born ?

    oi ColumnClic3(ob*ect sener, ColumnClic37ent6rgs e) :

    istIie. l < (istIie.) sener

    i (l"4orting

  • 5/28/2018 Mono C# Winforms Tutorial

    37/75

    In our e)ample+ ,e ave a listvie, ,it t,o columns$ In te #irst column+ ,e display te name o#

    te actress$ In te second one teir date o# birt$ Te data is store in a istcollection$ 3y selecting a

    ro,+ te data in a ro, is displayed in te statusbar$ Also+ by clicking on te column eader+ te data

    is sorted$

    public class 6ctress:

    """?

    "e use te Actressclass 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$

    Column5eaer name < ne. Column5eaer()name"Te;t < =Name=name"+ith < -1

    For eac column in a listvie,+ ,e create a Column,eader$ 3y setting te Widthto 8+ te ,idt o#

    te column is e>ual to te longest item in te column$

    istIie. l < ne. istIie.()l"'arent < this

    ist2ie3control is created$

    l"ullo.4elect < true

    l"%riines < truel"6llo.Columneorer < truel"4orting < 4ortDrer"6scening

    =ere ,e set #our properties o# te control$ Tis code lines enable #ull ro, selection+ so, grid lines+

    allo, column reordering by dragging te columns and sort te data in ascending order$

    l"Columns"6ange(ne. Column5eaer :name, /ear?)

    =ere ,e add t,o Column,eaders to te ist2ie3control$

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

    Tis cycle populates te listvie, control$ /ac ro, is added to te listvie, as a ist2ie3Item

    class$

    l"Iie. < Iie."9etails

    Te ist2ie3control can ave di##erent vie,s$ Di##erent vie,s display data di##erently$

    istIie. l < (istIie.) sener

    string name < l"4electe!tems0"4ub!tems0"Te;tstring born < l"4electe!tems0"4ub!tems1"Te;tsb"Te;t < name =, = born

  • 5/28/2018 Mono C# Winforms Tutorial

    38/75

    Inside te 'nChanged()metod+ ,e get te data #rom te selected ro, and so, it on te

    statusbar$

    i (l"4orting

  • 5/28/2018 Mono C# Winforms Tutorial

    39/75

    chil@"Te;t < =Waa=

    root"Noes"6ange(ne. TreeNoe :chil1, chil2, chil@?)

    t"'arent < this t"Noes"6(root) t"9oc3 < 9oc34t/le"ill t"6ter4elect < ne. TreeIie.7ent5anler(6ter4elect)

    sb < ne. 4tatusar() sb"'arent < this

    CenterTo4creen() ?

    oi 6ter4elect(ob*ect sener, TreeIie.7ent6rgs e) : sb"Te;t < e"Noe"Te;t ??

    class M6pplication : public static oi Main() : 6pplication"un(ne. Morm()) ??

    Tis is a very simple demonstration o# te Tree2ie3control$ "e ave one root item and tree

    cildren$

    TreeIie. t < ne. TreeIie.()

    "e create te Tree2ie3control$

    TreeNoe root < ne. TreeNoe()root"Te;t < =anguages="""t"Noes"6(root)

    =ere ,e create a root node$

    TreeNoe chil1 < ne. TreeNoe()chil1"Te;t < ='/thon=

    Cild node is created in a similar ,ay$

    root"Noes"6ange(ne. TreeNoe :chil1, chil2, chil@?)

    Cild nodes are plugged into te Nodesproperty o# te root node$

  • 5/28/2018 Mono C# Winforms Tutorial

    40/75

    Figure2 Tree4ie,

    +irectoriesTe #ollo,ing code e)ample ,ill e)amine te Tree2ie3control more indept$

    irectories"cs

    using 4/stemusing 4/stem"9ra.ingusing 4/stem"+ino.s"ormsusing 4/stem"!D

    public class Morm orm:

    priate TreeIie. t priate utton e;pan priate utton e;pan6ll priate utton collapse priate utton collapse6ll priate 4tatusar sb

    priate const string 5DM7G9! < =homerons3i*=

    public Morm() : 4ie < ne. 4ie(J00, J00) Te;t < =9irectories=

    t < ne. TreeIie.()

    4uspena/out()

    t"'arent < this t"ocation < ne. 'oint(10,10) t"4ie < ne. 4ie(Client4ie"+ith - 20, 5eight - 200) t"6nchor < 6nchor4t/les"Top L 6nchor4t/les"et L

    6nchor4t/les"ight

    t"ullo.4elect < alse

    t"4ho.ines < truet"4ho.'lusMinus < truet"4crollable < truet"6ter4elect < ne. TreeIie.7ent5anler(6ter4elect)

  • 5/28/2018 Mono C# Winforms Tutorial

    41/75

    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"et L 6nchor4t/les"Top e;pan"Clic3 < ne. 7ent5anler(Dn7;pan)

    e;pan6ll < ne. utton() e;pan6ll"'arent < this e;pan6ll"ocation < ne. 'oint(20, e;pan"ottom >) e;pan6ll"Te;t < =7;pan 6ll= e;pan6ll"6nchor < 6nchor4t/les"et L 6nchor4t/les"Top e;pan6ll"Clic3 < ne. 7ent5anler(Dn7;pan6ll)

    collapse < ne. utton() collapse"'arent < this collapse"ocation < ne. 'oint(e;pan6ll"ight >, e;pan"Top ) collapse"Te;t < =Collapse= collapse"6nchor < 6nchor4t/les"et L 6nchor4t/les"Top collapse"Clic3 < ne. 7ent5anler(DnCollapse)

    collapse6ll < ne. utton() collapse6ll"'arent < this collapse6ll"ocation < ne. 'oint(collapse"et, collapse"ottom >) collapse6ll"Te;t < =Collapse 6ll= collapse6ll"6nchor < 6nchor4t/les"et L 6nchor4t/les"Top collapse6ll"Clic3 < ne. 7ent5anler(DnCollapse6ll)

    sb < ne. 4tatusar() sb"'arent < this

    4ho.9irectories(t"Noes, 5DM7G9!)

    esumea/out()

    CenterTo4creen() ?

    oi 6ter4elect(ob*ect sener, TreeIie.7ent6rgs e) : sb"Te;t < e"Noe"Te;t ?

    oi 4ho.9irectories(TreeNoeCollection trNoe, string path) : 9irector/!no ir!no < ne. 9irector/!no(path) i (ir!no X< null) : 9irector/!no sub9irs < ir!no"%et9irectories() TreeNoe tr < ne. TreeNoe(ir!no"Name)

    i (sub9irs"ength 0) : oreach (9irector/!no r in sub9irs) :

    i (Xr"Name"4tarts+ith(="=))

    4ho.9irectories(tr"Noes, r"ullName)? ? trNoe"6(tr)

  • 5/28/2018 Mono C# Winforms Tutorial

    42/75

    ? ?

    oi Dn7;pan(ob*ect sener, 7ent6rgs e) : t"4electeNoe"7;pan() ?

    oi Dn7;pan6ll(ob*ect sener, 7ent6rgs e) : t"7;pan6ll() ?

    oi DnCollapse(ob*ect sener, 7ent6rgs e) : t"4electeNoe"Collapse() ?

    oi DnCollapse6ll(ob*ect sener, 7ent6rgs e) :

    t"Collapse6ll() ?

    static oi Main():

    6pplication"un(ne. Morm()) ?

    ?

    1ur code e)ample so,s te directories o# te speci#ied ome directory in a Tree2ie3control$ Te

    application starts ,it some delay+ because it reads te directory structure o# te ome directory

    #irst$ "e ave also #our buttons on te #orm$ Te buttons e)pand and collapse nodesprogramatically$

    t"4crollable < true

    "e make te treevie, control scrollable+ because te control so,s lots o# directories$

    4ho.9irectories(t"Noes, 5DM7G9!)

    Te Sho3+irectories()metod #ills te nodes o# te trevie, control ,it directories available in

    te speci#ied ome directory$

    i (sub9irs"ength 0)

    :"""?

    "e ceck i# tere are any subdirectories$

    oreach (9irector/!no r in sub9irs):

    i (Xr"Name"4tarts+ith(="=)) 4ho.9irectories(tr"Noes, r"ullName)?

    "e loop troug all directories$ For tis+ ,e use te recursion algoritm$ "e also skip te idden

    directories$ Tey begin ,it a dot on (ni) systems$

    trNoe"6(tr)

  • 5/28/2018 Mono C# Winforms Tutorial

    43/75

    Tis code line actually adds te directory to te treevie, control$

    oi Dn7;pan(ob*ect sener, 7ent6rgs e) : t"4electeNoe"7;pan() ?

    All #our buttons ave events plugged to a metod$ =ere is a metod #or te /)pand button$ It callste &xpand()metod o# te currently selected node$

    Figure2 Directories

    In tis part o# te Mono "in#orms tutorial+ ,e covered several advanced controls available in

    "in#orms library$

    +ialogsIn tis part o# te Mono "in#orms tutorial+ ,e ,ill talk about dialogs$

    Dialog ,indo,s or dialogs are an indispensable part o# most modern (I applications$ A dialog is

    de#ined as a conversation bet,een t,o or more persons$ In a computer application a dialog is a

    ,indo, ,ic is used to ?talk? to te application$ A dialog is used to input data+ modi#y data+

    cange te application settings etc$ Dialogs are important means o# communication bet,een a user

    and a computer program$

    Tere are essentially t,o types o# dialogs$ Prede#ined dialogs and custom dialogs$

    Folder"ro3ser+ialog

    Tis dialog prompts te user to select a #older$

    olerbro.serialog"cs

  • 5/28/2018 Mono C# Winforms Tutorial

    44/75

    using 4/stemusing 4/stem"!Dusing 4/stem"9ra.ingusing 4/stem"+ino.s"orms

    class Morm orm :

    priate Toolar toolbar priate Toolarutton open priate 4tatusar statusbar

    public Morm() : Te;t < =olerro.ser9ialog=

    toolbar < ne. Toolar() open < ne. Toolarutton()

    statusbar < ne. 4tatusar() statusbar"'arent < this

    toolbar"uttons"6(open)

    toolbar"uttonClic3 < ne. ToolaruttonClic37ent5anler(DnClic3e)

    Controls"6(toolbar)

    CenterTo4creen() ?

    oi DnClic3e(ob*ect sener, ToolaruttonClic37ent6rgs e) : olerro.ser9ialog ialog < ne. olerro.ser9ialog()

    i (ialog"4ho.9ialog(this)

  • 5/28/2018 Mono C# Winforms Tutorial

    45/75

    Figure2 Folder3ro,serDialog

    Color+ialog

    Tis dialog displays available colors along ,it controls tat enable te user to de#ine custom

    colors$

    colorialog"cs

    using 4/stemusing 4/stem"9ra.ingusing 4/stem"+ino.s"orms

    class Morm orm :

    priate Toolar toolbar priate Toolarutton open priate Color color

    priate int rect+ith < 100 priate int rect5eight < 100 priate ectangle r

    public Morm() : Te;t < =Color9ialog=

    color < Color"lue

    toolbar < ne. Toolar() open < ne. Toolarutton()

    toolbar"uttons"6(open) toolbar"uttonClic3 < ne. ToolaruttonClic37ent5anler(DnClic3e)

    ocateect()

    4et4t/le (Control4t/les"esieera., true) Controls"6(toolbar) 'aint < ne. 'aint7ent5anler(Dn'aint)

  • 5/28/2018 Mono C# Winforms Tutorial

    46/75

    CenterTo4creen() ?

    oi Dn'aint(ob*ect sener, 'aint7ent6rgs e) : %raphics g < e"%raphics ocateect()

    4olirush brush < ne. 4olirush(color)

    g"illectangle(brush, r) ?

    oi DnClic3e(ob*ect sener, ToolaruttonClic37ent6rgs e) : Color9ialog ialog < ne. Color9ialog() i (ialog"4ho.9ialog(this)

  • 5/28/2018 Mono C# Winforms Tutorial

    47/75

    Figure2 ColorDialog

    Font+ialog

    Te Font+ialogis used to select #onts$

    ontialog"cs

    using 4/stemusing 4/stem"9ra.ing

    using 4/stem"+ino.s"orms

    class Morm orm :

    priate abel te;t priate Toolar toolbar priate Toolarutton open

    public Morm() : Te;t < =ont9ialog=

    te;t < ne. abel() te;t"'arent < this

    te;t"Te;t < =+inorms tutorial=

    ocateTe;t()

    toolbar < ne. Toolar() toolbar"'arent < this open < ne. Toolarutton()

    toolbar"uttons"6(open) toolbar"uttonClic3 < ne. ToolaruttonClic37ent5anler(DnClic3e)

    te;t"6uto4ie < true esie < ne. 7ent5anler(Dnesie)

    CenterTo4creen() ?

  • 5/28/2018 Mono C# Winforms Tutorial

    48/75

    oi Dnesie(ob*ect sener, 7ent6rgs e): ocateTe;t() ?

    oi DnClic3e(ob*ect sener, ToolaruttonClic37ent6rgs e) : ont9ialog ialog < ne. ont9ialog() i (ialog"4ho.9ialog(this)

  • 5/28/2018 Mono C# Winforms Tutorial

    49/75

    'pen+ialog

    Tis dialog is used to open #iles$

    openialog"cs

    using 4/stem

    using 4/stem"!Dusing 4/stem"9ra.ingusing 4/stem"+ino.s"orms

    class Morm orm :

    priate Toolar toolbar priate Toolarutton open priate Te;to; te;tbo;

    public Morm() : Te;t < =Dpenile9ialog=

    toolbar < ne. Toolar() open < ne. Toolarutton()

    te;tbo; < ne. Te;to;() te;tbo;"Multiline < true te;tbo;"4crollars < 4crollars"oth te;tbo;"+or+rap < alse te;tbo;"'arent < this te;tbo;"9oc3 < 9oc34t/le"ill

    toolbar"uttons"6(open) toolbar"uttonClic3 < ne. ToolaruttonClic37ent5anler(DnClic3e)

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

    CenterTo4creen() ?

    oi DnClic3e(ob*ect sener, ToolaruttonClic37ent6rgs e) : Dpenile9ialog ialog < ne. Dpenile9ialog() ialog"ilter < =C# iles (H"cs)LH"cs=

    i (ialog"4ho.9ialog(this)

  • 5/28/2018 Mono C# Winforms Tutorial

    50/75

    Te 'pen+ialogis created$

    ialog"ilter < =C# iles (H"cs)LH"cs=

    "e set te Filterproperty to C! source #iles$ 1nly C! #iles can be cosen ,it tis dialog instance$

    i (ialog"4ho.9ialog(this)

  • 5/28/2018 Mono C# Winforms Tutorial

    51/75

    googlecolorbg ?FFFFFF?O

    googlecolorlink ?9D

  • 5/28/2018 Mono C# Winforms Tutorial

    52/75

    button"Mouse9o.n < ne. Mouse7ent5anler(DnMouse9o.n) button"Mouse&p < ne. Mouse7ent5anler(DnMouse&p) button"MouseMoe < ne. Mouse7ent5anler(DnMouseMoe)

    CenterTo4creen() ?

    public static oi Main():

    6pplication"un(ne. Morm()) ?

    priate oi DnMouse9o.n(ob*ect sener, Mouse7ent6rgs e):

    is9ragging < true olF < e"F olV < e"V ?

    priate oi DnMouseMoe(ob*ect sener, Mouse7ent6rgs e)

    : i (is9ragging)

    : button"Top < button"Top (e"V - olV)

    button"et < button"et (e"F - olF)?

    ?

    priate oi DnMouse&p(ob*ect sener, Mouse7ent6rgs e):

    is9ragging < alse ??

    Te code e)ample puts a regular button control on te #orm container$ 3y clicking on te button

    sur#ace and simultaneously dragging it ,it a mouse ,e can relocate te button$

    priate bool is9ragging < alse priate int olF, olV

    Tese are te supporting variables #or our e)ample$ Te is+raggingvariable tells us+ ,eter ,e

    are in te process o# dragging an ob-ect$ Te old5and old6variables store te )+ y coordinates -ust

    be#ore te dragging process begins$

    button"Mouse9o.n < ne. Mouse7ent5anler(DnMouse9o.n)

    button"Mouse&p < ne. Mouse7ent5anler(DnMouse&p) button"MouseMoe < ne. Mouse7ent5anler(DnMouseMoe)

    "e plug in tree di##erent mouse andlers #or our button$ Tey implement tree di##erent stages o#

    te drag & drop process$ Te process begins+ ,en ,e click on te button$ Tis is andled by te

    'nMouse+o3n()metod$ Te second part is te movement$ Tis is ,en ,e move te ob-ect to a

    ne, position$ It is andled in te 'nMouseMove()metod$ Te #inal part is ,en te process

    stops$ It appens ,en ,e release te mouse button$ Te appropriate task is delegated to te

    'nMouse.p()metod$

    priate oi DnMouse9o.n(ob*ect sener, Mouse7ent6rgs e):

    is9ragging < true olF < e"F olV < e"V ?

  • 5/28/2018 Mono C# Winforms Tutorial

    53/75

    Te 'nMouse+o3n()metod implements te #irst part o# te process$ It sets tree necessary

    variables$

    priate oi DnMouseMoe(ob*ect sener, Mouse7ent6rgs e):

    i (is9ragging):

    button"Top < button"Top (e"V - olV)button"et < button"et (e"F - olF)

    ? ?

    In te 'nMouseMove()metod+ ,e relocate te button$ "e calculate te di##erence bet,een te

    stored )+ y coordinates and te ne, coordinates o# te mouse pointer$ Te di##erence is added to te

    Topand eftproperties o# te button+ tus moving it to a ne, position$

    Figure2 Dragging a button

    +ragging Text

    In te previous e)ample+ ,e did drag & drop on te control$ *e)t ,e ,ill do a drag & drop

    operation on te te)tual data$ =ere ,e ,ill use te drag & drop protocol provided by te "in#orms

    library$

    Drag & drop operation is a standardi@ed communication protocol in "in#orms$ "e ave t,o basic

    ob-ects$ Te drag sourceand te drop target$

    ragte;t"cs

    using 4/stemusing 4/stem"9ra.ingusing 4/stem"+ino.s"orms

    public class Morm orm:

    priate Te;to; te;to; priate utton button

    public Morm() : !nitorm()

    CenterTo4creen() ?

    priate oi DnMouse9o.n(ob*ect sener, Mouse7ent6rgs e)

  • 5/28/2018 Mono C# Winforms Tutorial

    54/75

    : Te;to; t;t < (Te;to;) sener t;t"9o9rag9rop(t;t"Te;t, 9rag9rop7ects"Cop/) ?

    priate oi Dn9rag7nter(ob*ect sener, 9rag7ent6rgs e) : e"7ect < 9rag9rop7ects"Cop/ ?

    priate oi Dn9rag9rop(ob*ect sener, 9rag7ent6rgs e) : utton button < (utton) sener button"Te;t < (string) e"9ata"%et9ata(9ataormats"Te;t) ?

    priate oi !nitorm() : Te;t < =9rag E rop= button < ne. utton()

    te;to; < ne. Te;to;() 4uspena/out()

    button"6llo.9rop < true button"ocation < ne. 'oint(1>0, >0) te;to;"ocation < ne. 'oint(1>, >0)

    button"9rag9rop < ne. 9rag7ent5anler(Dn9rag9rop) button"9rag7nter < ne. 9rag7ent5anler(Dn9rag7nter) te;to;"Mouse9o.n < ne. Mouse7ent5anler(DnMouse9o.n)

    Client4ie < ne. 4ie(2>0, 200) Controls"6(button)

    Controls"6(te;to;) esumea/out() ?

    public static oi Main(string args) : 6pplication"un(ne. Morm()) ?

    ?

    "e ave t,o controls on te #orm$ A button and a te)t bo)$ "e ,ill drag te)t #rom te te)t bo) and

    drop it on te button$!nitorm()

    Setting up o# te #orm is delegated to te InitForm()metod$ Tis is usually done in larger

    applications$

    4uspena/out()"""esumea/out()

    "e lay out our controls bet,een tese t,o metod calls$ Tis is an optimali@ation$ It sould

    eliminate #licker$

    button"6llo.9rop < true

    "e set te Allo3+ropproperty to true$ Dropping is not enabled by de#ault$

  • 5/28/2018 Mono C# Winforms Tutorial

    55/75

    button"9rag9rop < ne. 9rag7ent5anler(Dn9rag9rop)button"9rag7nter < ne. 9rag7ent5anler(Dn9rag7nter)te;to;"Mouse9o.n < ne. Mouse7ent5anler(DnMouse9o.n)

    Again+ te drag & drop process is divided into tree steps$ "e ave tree metods #or eac

    particular step$

    priate oi DnMouse9o.n(ob*ect sener, Mouse7ent6rgs e): Te;to; t;t < (Te;to;) sener t;t"9o9rag9rop(t;t"Te;t, 9rag9rop7ects"Cop/)?

    In te 'nMouse+o3n()metod ,e initiali@e te drap & drop process$ "e initiate te process ,it

    te +o+rag+rop()metod$ Te +rag+rop&ffects!Cop*parameter speci#ies te type o# te

    operation$ /sentially+ ,e can eiter copy te te)t or move it during te drag & drop operation$

    priate oi Dn9rag7nter(ob*ect sener, 9rag7ent6rgs e): e"7ect < 9rag9rop7ects"Cop/

    ?

    Te +rag&nterevent is launced ,en te mouse pointer enters te area o# te drop target control$

    Te &ffectproperty must be set$ Te DragDrop/##ects o# te drag source and drop target must be

    e>ual$ 1ter,ise te operation ,ill not ,ork$

    priate oi Dn9rag9rop(ob*ect sener, 9rag7ent6rgs e): utton button < (utton) sener button"Te;t < (string) e"9ata"%et9ata(9ataormats"Te;t)?

    Finally ,e ave te 'n+rag+rop()metod$ =ere ,e get te data #rom te event ob-ect and set it tote button Textproperty$

    Figure2 Drag & drop o# te)t

    +ragging Image

    In our last e)ample+ ,e ,ill drag & drop image on te #orm$

    ragimage"cs

    using 4/stemusing 4/stem"9ra.ing

  • 5/28/2018 Mono C# Winforms Tutorial

    56/75

    using 4/stem"+ino.s"orms

    public class !mage9rag9rop orm: priate bool is9ragging priate int olF, olV

    priate ectangle ropect priate 'ictureo; pico; priate itmap image priate rush brush

    public !mage9rag9rop() : Client4ie < ne. 4ie(@>0, 2>0) Te;t < =9ragging !mage= 'aint < ne. 'aint7ent5anler(Dn'aint)

    is9ragging < alse ropect < ne. ectangle(10, 10, 200, 1S0) brush < rushes"%ra/

    pico; < ne. 'ictureo;() loa!mage()

    pico;"'arent < this pico;"ocation < ne. 'oint(100, >0) pico;"4ie < ne. 4ie(image"+ith, image"5eight) pico;"!mage < image pico;"Cursor < Cursors"5an

    pico;"Mouse9o.n < ne. Mouse7ent5anler(DnMouse9o.n) pico;"Mouse&p < ne. Mouse7ent5anler(DnMouse&p) pico;"MouseMoe < ne. Mouse7ent5anler(DnMouseMoe)

    CenterTo4creen() ?

    oi loa!mage() : tr/ : image < ne. itmap(=image"*pg=) ? catch : Console"+riteine(=7rror reaing image=) 7nironment"7;it(1) ? ?

    public static oi Main():

    6pplication"un(ne. !mage9rag9rop()) ?

    priate oi DnMouse9o.n(ob*ect sener, Mouse7ent6rgs e):

    is9ragging < true olF < e"F olV < e"V ?

    priate oi DnMouseMoe(ob*ect sener, Mouse7ent6rgs e)

    : i (is9ragging):

    pico;"Top < pico;"Top (e"V - olV)

  • 5/28/2018 Mono C# Winforms Tutorial

    57/75

    pico;"et < pico;"et (e"F - olF) ? ?

    priate oi DnMouse&p(ob*ect sener, Mouse7ent6rgs e):

    is9ragging < alse

    i(ropect"Contains(pico;"ouns)) : brush < rushes"%ol ? else : brush < rushes"%ra/ ?

    eresh() ?

    priate oi Dn'aint(ob*ect sener, 'aint7ent6rgs e) : %raphics g < e"%raphics g"illectangle(brush, ropect)

    ??

    In our e)ample ,e ave a $icture"oxand ,e dra, a gray rectangle$ I# ,e drop te picture inside

    te rectangle+ te color o# te rectangle canges to gold$

    brush < rushes"%ra/

    Te brushvariable olds te brus o# te rectangle$ It is a gray color by de#ault$

    oi loa!mage() : tr/ : image < ne. itmap(=image"*pg=) ? catch : Console"+riteine(=7rror reaing image=) 7nironment"7;it(1) ? ?

    Te loadImage()loads a bitmap #or te $icture"oxcontrol$

    i (ropect"Contains(pico;"ouns)) : brush < rushes"%ol? else : brush < rushes"%ra/?

    In te 'nMouse.p()metod+ ,e determine te brus o# te rectangle$ I# te bounds o# te picture

    bo) are inside te rectangle+ te brus is o# gold colorO gray oter,ise$

    eresh()

    "e must call te 7efresh()metod to activate te ne, brus color$

  • 5/28/2018 Mono C# Winforms Tutorial

    58/75

    Figure2 Drag & drop image

    Tis capter ,as dedicated to drag & drop operations using te Mono "in#orms library$

    $ainting in Mono WinformsJK

    googleadclient ?pubE;NE;E8889:?O

    googlead,idt 8:;O

    googleadeigt N;;O

    googlead#ormat ?8:;)N;;as?O

    googleadtype ?te)t?O

    BB:;;E;E8:2 kanal8googleadcannel ?E89:

  • 5/28/2018 Mono C# Winforms Tutorial

    59/75

    custom control #rom scratc$ To do te painting+ ,e use te painting API provided by te "in#orms

    library$ Te painting is done ,itin a metod+ tat ,e plug into te $aintevent$

    Te S*stem!+ra3ingnamespace provides access to -+I8basic grapics #unctionality$ More

    advanced #unctionality is provided in te System$Dra,ing$Dra,ing:D+ System$Dra,ing$Imaging+

    and System$Dra,ing$Te)t namespaces$ Te -raphicsclass provides metods #or dra,ing on te

    #orm$

    ines

    1ur #irst e)ample ,ill dra, lines on te Formcontrol$

    lines"cs

    using 4/stemusing 4/stem"9ra.ingusing 4/stem"9ra.ing"9ra.ing29using 4/stem"+ino.s"orms

    class Morm orm :

    public Morm() : Te;t < =ines= 4ie < ne. 4ie(280, 2A0) esieera. < true

    'aint < ne. 'aint7ent5anler(Dn'aint) CenterTo4creen() ?

    oi Dn'aint(ob*ect sener, 'aint7ent6rgs 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. Morm()) ??

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

  • 5/28/2018 Mono C# Winforms Tutorial

    60/75

    esieera. < true

    "en ,e resi@e te #orm+ it is automatically redra,n$ Tis is not te de#ault beavior$

    'aint < ne. 'aint7ent5anler(Dn'aint)

    Paint events are delivered to te 'n$aint()metod$

    oi Dn'aint(ob*ect sener, 'aint7ent6rgs e): """?

    Tis is te signature o# te 'n$aint()metod$

    %raphics g < e"%raphics

    In order to paint on te #orm+ ,e must get te -raphicsob-ect$ Painting on a #orm is actually

    calling various metods o# te -raphicsob-ect$

    'en pen < ne. 'en(Color"lac3, 1)

    pen"9ash4t/le < 9ash4t/le"9otg"9ra.ine(pen, 20, J0, 2>0, J0)

    "e create a $enob-ect$ Tis ob-ect is used to dra, outlines o# sapes$ Tan ,e set a dotted

    +ashSt*le$ Finally ,e dra, te line ,it te +ra3ine()metod$ Te #irst parameter is te pen

    ob-ect$ Te ne)t #our values are ) and y values o# starting and ending points o# te line$

    pen"9ash'attern < ne. loat :S, 8, 1, 1, 1, 1, 1, 1 ?

    Tere are several builtin +ashSt*levalues$ "e can create our o,n style by using te +ash$attern

    property$ It may look di##icult at te #irst sigt$ 3ut te pattern is simply an array o# #ill and emptyvalues$

    g"9ispose()

    "e kno, tat C! language uses garbage collection$ So ,y are ,e e)plicitely releasing resourcesQ

    Tis is to increase e##iciency$ "e are elping te garbage collector$

    Figure2 Lines

  • 5/28/2018 Mono C# Winforms Tutorial

    61/75

    Colors

    A color in "in#orms library represents an A03 5alpa+ red+ green+ blue6 color$ It is a combination

    o# Alpa+ 0ed+ reen+ and 3lue 5036 intensity values$ Tere are also prede#ined color names+ tat

    ,e can use in painting$ ttp2BB@etcode$comBtutorialsBmono,in#ormstutorialBimages

    colors"cs

    using 4/stemusing 4/stem"9ra.ingusing 4/stem"9ra.ing"9ra.ing29using 4/stem"+ino.s"orms

    class Morm orm :

    public Morm() : Te;t < =Colors= 4ie < ne. 4ie(@S0, @00)httpetcoe"comtutorialsmono.inormstutorialimages 'aint < ne. 'aint7ent5anler(Dn'aint)

    CenterTo4creen() ?

    oi Dn'aint(ob*ect sener, 'aint7ent6rgs e) :

    %raphics g < e"%raphics

    g"illectangle(rushes"4ienna, 10, 1>, R0, S0) g"illectangle(rushes"%reen, 1@0, 1>, R0, S0) g"illectangle(rushes"Maroon, 2>0, 1>, R0, S0) g"illectangle(rushes"Chocolate, 10, 10>, R0, S0)

    g"illectangle(rushes"%ra/, 1@0, 10>, R0, S0) g"illectangle(rushes"Coral, 2>0, 10>, R0, S0) g"illectangle(rushes"ro.n, 10, 1R>, R0, S0) g"illectangle(rushes"Teal, 1@0, 1R>, R0, S0) g"illectangle(rushes"%olenro, 2>0, 1R>, R0, S0)

    g"9ispose()httpetcoe"comtutorialsmono.inormstutorialimages

    ??

    class M6pplication : public static oi Main() : 6pplication"un(ne. Morm()) ??

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

    g"illectangle(rushes"4ienna, 10, 1>, R0, S0)

    Te Fill7ectagle()metod #ills a speci#ied rectangle ,it a brus$ A brus can be a color or a

    pattern$ Tere are some prede#ined colors available$ "e can get tem #rom te "rushes

    enumeration$ Te last #our values are te )+ y values o# te tople#t point and te ,idt and eigt o#

    te rectangle$

  • 5/28/2018 Mono C# Winforms Tutorial

    62/75

    Figure2 Colors

    ,atches

    Te ,atch"rushob-ect is used to #ill te interiors o# te sapes$ Tere are several builtin patterns+

    tat ,e can use$

    hatches"cs

    using 4/stemusing 4/stem"9ra.ingusing 4/stem"9ra.ing"9ra.ing29

    using 4/stem"+ino.s"orms

    class Morm orm :

    public Morm() : Te;t < =5atches= 4ie < ne. 4ie(@S0, @00)

    'aint < ne. 'aint7ent5anler(Dn'aint)

    CenterTo4creen() ?

    oi Dn'aint(ob*ect sener, 'aint7ent6rgs e) :

    %raphics g < e"%raphics

    5atchrush hb < ne. 5atchrush(5atch4t/le"Cross, Color"lac3,this"ac3Color) g"illectangle(hb, 10, 1>, R0, S0)

    hb < ne. 5atchrush(5atch4t/le"'ercent0>, Color"lac3, this"ac3Color) g"illectangle(hb, 1@0, 1>, R0, S0)

    hb < ne. 5atchrush(5atch4t/le"4oli9iamon, Color"lac3,this"ac3Color)

    g"illectangle(hb, 2>0, 1>, R0, S0)

    hb < ne. 5atchrush(5atch4t/le"9iagonalric3, Color"lac3,this"ac3Color)

  • 5/28/2018 Mono C# Winforms Tutorial

    63/75

    g"illectangle(hb, 10, 10>, R0, S0)

    hb < ne. 5atchrush(5atch4t/le"9iot, Color"lac3, this"ac3Color) g"illectangle(hb, 1@0, 10>, R0, S0)

    hb < ne. 5atchrush(5atch4t/le"+ae, Color"lac3, this"ac3Color) g"illectangle(hb, 2>0, 10>, R0, S0)

    hb < ne. 5atchrush(5atch4t/le"YigYag, Color"lac3, this"ac3Color) g"illectangle(hb, 10, 1R>, R0, S0)

    hb < ne. 5atchrush(5atch4t/le"4phere, Color"lac3, this"ac3Color) g"illectangle(hb, 1@0, 1R>, R0, S0)

    hb < ne. 5atchrush(5atch4t/le"4hingle, Color"lac3, this"ac3Color) g"illectangle(hb, 2>0, 1R>, R0, S0)

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

    class M6pplication : public static oi Main() : 6pplication"un(ne. Morm()) ??

    Tis time ,e #ill nine rectangles ,it nine di##erent patterns+ called atces$

    5atchrush hb < ne. 5atchrush(5atch4t/le"Cross, Color"lac3, this"ac3Color)

    =ere ,e create a ,atch"rushob-ect$ Te parameters are te atc style and te #oreground and te

    background colors$ Te background color is set to te color o# te #orm+ so tat it looks like ,eave dra,n onto te #orm$

    g"illectangle(hb, 10, 1>, R0, S0)

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

    Figure2 =atces

  • 5/28/2018 Mono C# Winforms Tutorial

    64/75

    -radients

    In computer grapics+ gradient is a smoot blending o# sades #rom ligt to dark or #rom one color

    to anoter$ In :D dra,ing programs and paint programs+ gradients are used to create color#ul

    backgrounds and special e##ects as ,ell as to simulate ligts and sado,s$ 5ans,ers$com6

    graients"cs

    using 4/stemusing 4/stem"9ra.ingusing 4/stem"9ra.ing"9ra.ing29using 4/stem"+ino.s"orms

    class Morm orm :

    public Morm() : Te;t < =%raients= 4ie < ne. 4ie(@>0, @>0)

    'aint < ne. 'aint7ent5anler(Dn'aint)

    CenterTo4creen() ?

    oi Dn'aint(ob*ect sener, 'aint7ent6rgs e) :

    %raphics g < e"%raphics

    'oint pt1 < ne. 'oint(>, >) 'oint pt2 < ne. 'oint(2>, 2>) rush lg < ne. inear%raientrush(pt1, pt2, Color"e, Color"lac3) g"illectangle(lg, 20, 20, @00, J0)

    pt1 < ne. 'oint(>, 2>) pt2 < ne. 'oint(20, 2) lg < ne. inear%raientrush(pt1, pt2, Color"Vello., Color"lac3) g"illectangle(lg, 20, 80, @00, J0)

    pt1 < ne. 'oint(>, 2>) pt2 < ne. 'oint(2, 2) lg < ne. inear%raientrush(pt1, pt2, Color"%reen, Color"lac3) g"illectangle(lg, 20, 1J0, @00, J0)

    pt1 < ne. 'oint(2>, 2>) pt2 < ne. 'oint(1>, 2>) lg < ne. inear%raientrush(pt1, pt2, Color"lue, Color"lac3) g"illectangle(lg, 20, 200, @00, J0)

    pt1 < ne. 'oint(0, 10) pt2 < ne. 'oint(0, 20) lg < ne. inear%raientrush(pt1, pt2, Color"Drange, Color"lac3) g"illectangle(lg, 20, 2S0, @00, J0)

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

    class M6pplication : public static oi Main() : 6pplication"un(ne. Morm()) ??

  • 5/28/2018 Mono C# Winforms Tutorial

    65/75

    "e dra, #ive rectangles ,ic are #illed ,it di##erent linear gradients$

    ttp2BB@etcode$comBtutorialsBmono,in#ormstutorialBimages

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

    Tese t,o are te controlling points o# te linear gradient brus$

    rush lg < ne. inear%raientrush(pt1, pt2, Color"e, Color"lac3)

    "e create te inear-radient"rushob-ect$ "e use t,o controlling points and t,o blending

    colors$

    Figure2 radients

    +ra3ing string

    To dra, string on te "in#orms Form+ ,e use te +ra3String()metod$

    l/rics"cs

    using 4/stemusing 4/stem"9ra.ingusing 4/stem"9ra.ing"9ra.ing29using 4/stem"+ino.s"orms

    class Morm orm :

    public Morm() :

    Te;t < =Vou 3no. !Qm No %oo= 4ie < ne. 4ie(@80, J>0)

    'aint < ne. 'aint7ent5anler(Dn'aint)

    CenterTo4creen() ?

  • 5/28/2018 Mono C# Winforms Tutorial

    66/75

    oi Dn'aint(ob*ect sener, 'aint7ent6rgs e) :

    %raphics g < e"%raphics

    ont t < ne. ont(='urisa=, 10) 4olirush br < ne. 4olirush(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 sleees 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 toa/Z=, t, br, pt)

    pt < ne. 'oint(20"0, 110"0)g"9ra.4tring(=6n snie 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. Morm()) ??

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

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

    "e use te Purisa #ont+ o# 8; pts eigt$

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

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

  • 5/28/2018 Mono C# Winforms Tutorial

    67/75

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

    Te +ra3String()metod takes te #ollo,ing parameters2 te)t to dra,+ #ont+ brus and te $ointF

    ob-ect$

    Figure2 Lyrics

    +ra3ing image

    In our last e)ample ,e ,ill dra, an image on te Formcontrol$

    reroc3"cs

    using 4/stem

    using 4/stem"9ra.ingusing 4/stem"+ino.s"orms

    class Morm orm :

    priate itmap castle

    public Morm() : Te;t < =e oc3= loa!mage() Client4ie < ne. 4ie(castle"+ith, castle"5eight)

    'aint < ne. 'aint7ent5anler(Dn'aint)

    CenterTo4creen() ?

    oi loa!mage() :

  • 5/28/2018 Mono C# Winforms Tutorial

    68/75

    tr/ :castle < ne. itmap(=reroc3"png=)

    ? catch (7;ception e) : Console"+riteine(e"Message) 7nironment"7;it(1) ? ?

    oi Dn'aint(ob*ect sener, 'aint7ent6rgs e) :

    %raphics g < e"%raphics

    ectangle r < ne. ectangle(1, 1, castle"+ith, castle"5eight) g"9ra.!mage(castle, r) ??

    class M6pplication : public static oi Main() : 6pplication"un(ne. Morm()) ?

    ?

    Tis code e)ample dra,s an image o# a castle on te #orm$

    tr/ :castle < ne. itmap(=reroc3"png=)

    ? catch (7;ception e) : Console"+riteine(e"Message) 7nironment"7;it(1)?

    "e load an image o# a castle$

    ectangle r < ne. ectangle(1, 1, castle"+ith, castle"5eight)

    "e determine te rectangle tat ,e ,ill dra,$

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

    Tis line actually dra,s te image$

    Figure2 Image

  • 5/28/2018 Mono C# Winforms Tutorial

    69/75

    In tis capter+ ,e did some painting in Mono "in#orms library$

    Sna%e in Mono WinformsJK

    googleadclient ?pubE;NE;E8889:?Ogooglead,idt 8:;O

    googleadeigt N;;O

    googlead#ormat ?8:;)N;;as?O

    googleadtype ?te)t?O

    BB:;;E;E8:2 kanal8

    googleadcannel ?E89:

  • 5/28/2018 Mono C# Winforms Tutorial

    70/75

    using 4/stem"Collectionsusing 4/stem"ComponentMoelusing 4/stem"9ra.ingusing 4/stem"9atausing 4/stem"+ino.s"orms

    public class oar &serControl :

    priate const int +!9T5 < @00 priate const int 57!%5T < @00 priate const int 9DTG4!Y7 < 10 priate const int 6G9DT4 < R00 priate const int 6N9G'D4 < 2A

    priate int ; < ne. int6G9DT4 priate int / < ne. int6G9DT4

    priate int ots priate int appleG;

    priate int appleG/

    priate bool let < alse priate bool right < true priate bool up < alse priate bool o.n < alse priate bool in%ame < true

    priate Timer timer

    priate itmap ot priate itmap apple priate itmap hea

    priate !Container components

    public int D97G+!9T5 public int T!T76G57!%5T

    public oar() :

    components < ne. Container() ac3Color < Color"lac3 9oubleuere < true this"Client4ie < ne. 4ie(+!9T5, 57!%5T)

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

    ? catch (7;ception e) : Console"+riteine(e"Message) 7nironment"7;it(1) ?

    init%ame() ?

    priate oi DnTic3(ob*ect sener, 7ent6rgs e) :

    i (in%ame) : chec36pple()

  • 5/28/2018 Mono C# Winforms Tutorial

    71/75

    chec3Collision() moe() ? this"eresh() ?

    priate oi init%ame() :

    ots < @

    or (int < 0 U ots ) : ; < >0 - H 10 / < >0 ?

    locate6pple() Ke/&p < ne. Ke/7ent5anler(DnKe/&p)

    timer < ne. Timer(this"components)

    timer"7nable < true timer"!nteral < 100 timer"Tic3 < ne. 4/stem"7ent5anler(this"DnTic3)

    'aint < ne. 'aint7ent5anler(this"Dn'aint)

    ?

    priate oi Dn'aint(ob*ect sener, 'aint7ent6rgs e) :

    %raphics g < e"%raphics

    i (in%ame) :

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

    or (int < 0 U ots ) : i (

  • 5/28/2018 Mono C# Winforms Tutorial

    72/75

    priate oi chec36pple() :

    i ((;0

  • 5/28/2018 Mono C# Winforms Tutorial

    73/75

    ?

    priate oi DnKe/&p(ob*ect sener, Ke/7ent6rgs e) :

    int 3e/ < (int) e"Ke/Coe

    i ((3e/

  • 5/28/2018 Mono C# Winforms Tutorial

    74/75

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

    or (int < ots 0 --) :

    i (( J) EE (;0

  • 5/28/2018 Mono C# Winforms Tutorial

    75/75

    Figure2 SnakeTis ,as te Snake game programmed using te Mono "in#orms library$