visual c-sharp programming basics

19 312 0
visual c-sharp programming basics

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

;" ! !"#$%&'#$($)*&+,#-)(#.&,,#,/+*"#%).#$)#0+1/#+22,&3+$&)"'#4)*#5&"6).'#&"#789#:)(#.&,,#,/+*"#%).# $)#('/#;&'(+,#<$(6&)#$)#=(&,6#'&02,/#+22,&3+$&)"'>#%).#$)#('/#0)'$#)4#$%/#5&"6).'#?)*0'# 3)"$*),'>#+"6#'/@/*+,#$&2'#)"#%).#$)#2(=,&'%#-)(*#+22,&3+$&)"9# Made"by"Davide" Vitelaru " ! ;&'(+,#78#A*)B*+00&"B#C+'&3'# # %$$2DEE6+@&6/@&$/,+*(93)0E# 2" VISUAL"C#"PROGRAMMING"BASICS# ! VISUAL"C#"PROGRAMMING"BASICS" 3# ! Genera l " Requirements" "#!$#%%#&!'()*!'+'#,)-%!.#+!&)%%!/001!'(0!$#%%#&)/2!)'03*4! 56/#&)/2!'(0!7-*)8*!#$!-'!%0-*'!#/0!9,#2,-33)/2!%-/2+-20!:"#!;/#&!&(-'!<-,)-7%0*=! -,, *=!$+/8')#/*=!0'8>!-,0?! 5@!8#39+'0,!,+//)/2!A)/1#&*!BCDE)*'-DF! 5G)8,#*#$'!E)*+-%!HI!JK9,0**!:H%)8;!$#,!1#&/%#-1?! L#+!8-/!-%*#!+*0!G)8,#*#$'!E)*+-%!M'+1)#!C,#$0**)#/-%=!7+'!'()*!)*!-!8#330,8)-%!<0,*)#/!#$!'(0!E)*+-%!HI! JK9,0**=!-/1!)'!(-*!3#,0!$0-'+,0*N!A0!&)%%!/#'!+*0!3#*'!#$!'(03!)/!'()*!'+'#,)-%!'(#+2(N! If"you"ar e"interested"in"some"specific"part"of"this"t utorial,"c heck"out"the"tabl e"of"contents"on " the"last"page"because"you"might"find"what"you"are " l ooking"for." Quick"S t art"–"Your"first"a pplicatio n" O/!'()*!8(-9'0,=!.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,#90,')0*!&)%%!-990-,!)/!'(0! RC,#90,')0*T!&)/1#&N!M8,#%%!1#&/!-/1!2#!'#!R"0K'TN!"#!)'*!,)2('!.#+!&)%%!*00!RZ+''#/]TN!H(-/20!'(-'!'#! R@11T=!-/1!9,0**!0/'0,N! 6" VISUAL"C#"PROGRAMMING"BASICS# ! ! L#+,!7+''#/!/#&!(-*!R@11T!&,)''0/!#/!)'N!E0,.!2##1=!'()*!& !.#+!8-/!01)'!0<0,.!)'03W*!9,#90,')0*=!0<0/! '(0!'0K'!7#K0*WN! @%*#=!.#+!3)2('!/#')80!'(-'!'(0!$#,3W*!/-30!)*!RU#,3]TN!",.!'#!1#!*#30'()/2!-7#+'!)'N! How$To:$Click$on$an$empty$space$on$the$form,$change$the$form’s$text$property$to$“Calculator”.$ Step"3"–"Debugging"the"application " V$!8#+,*0=!&0!-1101!'(0!8#/',#%*!#/!#+,!$#,3=!7+'!'(0!7+''#/!1#0*/W'!1#!-/.'()/2!*)/80!&0!1)1/W'!R'0%%T!)'! 1#!'#!-/.'()/2N!L#+!8-/!*00!.#+,!9,#2,-3!,+//)/2!7.!9,0**)/2!'(0!RY07+2T!7+''#/=!'(0!2,00/!-,,#&!)/!'(0! '##%7-,!: ?N!A(0/!.#+!8%)8;!'(0!107+2!7+''#/=!.#+!-99%)8-')#/!&)%%!*'-,'N!O'!*(#+%1!%##;!*#30'()/2!%);0! '()*4! ! L#+!9,#7-7%.!',)01!'#!8%)8;!'(0!7+''#/!-%,0-1.=!-/1!/#')801!(#&!&0%%!(0!)*!1#)/2!()*!Q#7N!Y07+22)/2!)*!'(0! 70*'!30'(#1!'#!'0*'!.#+,!-99%)8-')#/!70$#,0!9+7%)*()/2!)'N!J<0,.!')30!.#+!3-;0!-!8(-/20!'#!)'=!.#+!107+2!)'! '#!*00!)$!)'!&#,;01N!V/80!.#+!$)/)*(!'(0!-99%)8-')#/=!.#+!7+)%1!'(0!0/'),0!9,#Q08'!'+,/)/2!0<0,.'()/2!)/'#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!)*!-!%-,20! VISUAL"C#"PROGRAMMING"BASICS" 7# ! -3#+/'!#$!8#10!-%,0-1.!&,)''0/!$#,!.#+=!*#!.#+!8-/!7+)%1!9,#2,-3*!&)'(!-!9,0''.!+*0,!)/'0,$-80!-/1!#'(0,! '()/2*N!O'!(0%9*!.#+!<0,.!3+8(!708-+*0!)'!,01+80*!'(0!-3#+/'!#$!'()/2*!'(-'!.#+!/001!'#!%0-,/!'#!8,0-'0!'(0! 0/'),0!+*0,!)/'0,$-80N!A0!*(#+%1!-%%!'(-/;!G)8,#*#$'!$#,!)'4! using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;! ! Z.!R)39#,'!*#30!'()/2*T!$,#3!'(0!NSJ"!U,-30&#,;=!O!30-/'!)39#,')/2!*#30!8%-**0*N!"(#*0!8%-**0*!8-/!70! '(0!$#,3*=!'(0!+*0,!8#/',#%*=!-/1!-%%!'(0!#'(0,!'()/2*!'(-'!(0%901!+*!7.!/#&!8,0-')/2!'(0!9,#2,-3N!L#+!&)%%! %0-,/!'(0!30-/)/2!#$!'(03!%-'0,N! U#,!/#&=!%0'W*!*00!'(0!,0*'!#$!'(0!8#104! namespace Calculator { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { } } }! ! "(0!Rpublic Form1()T!*'-'030/'!)*!-!8%-**!'(-'!)*!0K08+'01!&(0/!&0!*'-,'!'(0!9,#2,-3a!-8'+-%%.=!&(0/!&0! #90/!'(0!$#,3!/-301!RU#,3]N8*T!:RN8*T!)*!$,#3!H!M(-,9?N!O/!8-*0!.#+!1)1!/#'!;/#&=!)/!HI!'(0!8#10!)*! +*+-%%.!9+'!70'&00/!8+,%.!7,-80*!Q+*'!%);0!)/!b-<-!-/1!HccN! "(0!Rprivate void button1_Click(object sender, EventArgs e)T!)*!'(-'!8%-**!'(-'!)*!0K08+'01!&(0/! &0!8%)8;!'(0!7+''#/N!O/*)10!)'=!&0!&)%%!&,)'0!'(0!8#10!'(-'!&)%%!-11!'(0!'&#!<-%+0*!$,#3!'(0!'0K'!7#K0*N! Note:$In$C#,$two$slashes$(//)$represents$the$beginning$of$a$comment.$A$comment$is$a$piece$of$code$that$is$not$ executed$by$the$compiler,$it$is$used$to$help$you$organize$you$code,$and$so$that$other$programmers$will$ understand$what$every$piece$of$code$means.$We$will$use$comments$inside$our$codes$for$better$explanation.$ "#!3-;0!'(-'!7+''#/!-11!'(0!'&#!<-%+0*!-/1!,0'+,/!'(0!*+3!&0!/001=!&0!(-<0!'#!2,-7!'(0!'0K'!8#/'0/'! $,#3!'(0!'&#!'0K'!7#K0*=!'+,/!)'!'#!)/'020,*=!-11!'(03=!-/1!8(-/20!'(0!'0K'!#$!'(0!'(),1!'0K'!7#K!'#!'(0!*+3N! O'!)*!<0,.!*)39%04! double val1, val2; //We declare two double type variables //We assign to the first variable the value of the text box //Since the text box cand hold a string, it must be converted //to a double to assign it to "val1". 8" VISUAL"C#"PROGRAMMING"BASICS# ! //Note that we assign using “=” as an operator val1 = Double.Parse(textBox1.Text); //Double.Parse("string") converts the string put into the brackets //and assigns it to a double //Same thing for the second variable val2 = Double.Parse(textBox2.Text); //Now we are doing the exact oposite, we take the two //double values and we convert their sum to a string //using the .ToString() command textBox3.Text = (val1 + val2).ToString();! ! S#&!'(-'!&0!$)/)*(01!.#+!3)2('!&-/'!'#!107+2!.#+,!9,#Q08'!-/1!*00!)$!)'!&#,;*N! A(-'!&0!1)1!)*!0-*.!'#!0K9%-)/a!&0!108%-,01!'&#!<-,)-7%0*!-/1!-**)2/01!'(0!<-%+0*!#$!'(0!'&#!'0K'!7#K0*! -$'0,!&0!8#/<0,'01!'(03!$,#3!*',)/2*!'#!)/'020,*N!"(0/=!&0!8(-/201!'(0!'0K'!#$!'(0!'(),1!'0K'!7#K!)/'#!'(0! *+3!#$!'(0!'&#!<-,)-7%0*=!-/1!&0!8#/<0,'01!)'!'#!-!*',)/2!)/!'(0!*-30!')30N!@/1!&0!1)1!-%%!#$!'()*!-'!'(0!8%)8;! #$!-!7+''#/N! ! Step"5"–"Publishing " y o u " a p p lication" A(-'!.#+!(-<0!'#!1#!)*!'#!8,0-'0!-/!)8#/!$#,!.#+,!-99%)8-')#/=!8(-/20!)'*!9+7%)*()/2!*0'')/2*!-/1!3-;0!-! *0'+9!$#,!)'=!7+'!&0!&)%%!*;)9!'(0*0!*'09*!-*!'(0.!-,0!/#'!,0%-'01!'#!7-*)8!HI!9,#2,-33)/2N!! Note:$Check$out$the$“Advanced$Visual$C#$Programming”$tutorial$once$you$finish$this$one.$$ Underst anding"Visual"C#"" \,0-'=!/#&!'(-'!.#+!3-10!.#+,!$),*'!-99%)8-')#/!.#+!8-/!2#!0<0/!10090,!)/'#!HI!-/1!+/10,*'-/1!(#&!3#*'! '()/2*!&#,;=!-/1!*#30!0K',-!'()/2*!'(-'!&)%%!3-;0!.#+,!-99%)8-')#/!&#,;!70''0,N! Control"names" U),*'!#$!-%%=!#/0!)39#,'-/'!'()/2!'(-'!.#+!(-<0!'#!;/#&!)*!'(-'!0<0,.!)'03!#/!.#+,!$#,3!(-*!-!/-30N!@/1!O! -3!/#'!'-%;)/2!-7#+'!R"0K'!Z#KT!#,!RZ+''#/T=!7+'!-7#+'!'(0),!RS-30T!9,#90,'.N! VISUAL"C#"PROGRAMMING"BASICS" 9# ! \#!7-8;!'#!.#+,!$#,3*!10*)2/0,!-/1!8%)8;!#/!'(0!$),*'!'0K'!7#KN!O/!'(0!9,#90,'.!&)/1#&=!.#+!&)%%!*00!'(-'!)'*! /-30!9,#90,'.!)*!R'0K'Z#K]TN!O/!#+,!9,0<)#+*!8#10=!&0!'##;!'(0!<-%+0!$,#3!'()*!'0K'!7#K!7.!'(0!$#%%#&)/2! 30'(#14! val1 = Double.Parse(textBox1.Text);! ! P#&!1#0*!'()*!&#,;^!X0'W*!$#,20'!-7#+'!'(0!8#/<0,*)#/!$#,!-!$0&!*08#/1*=!-/1!*00!&(-'!&0!-8'+-%%.! -**)2/01!'#!'(0!<-,)-7%0!:8#/*)10,)/2!'(-'!'(0!'0K'!7#K!-%,0-1.!(#%1*!-!1#+7%0!<-%+0?N! val1 = textBox1.Text;! ! A0!-**)2/01!'(0!*908)$)8!9,#90,'.!#$!-!8#/',#%!'#!-!<-,)-7%0N!"(0!8#,,08'!*./'-K!)*4! variable = control.property; ! "()*!)*!(#&!)'!-%& *!&#,;*N!V$!8#+,*0=!&0!8-/!1#!'(0!0K-8'!#99#*)'0!-/1!-**)2/!'#!'(0!8#/',#%W*!9,#90,'.!-! 80,'-)/!<-,)-7%0!Q+*'!%);0!&0!1)1!0-,%)0,4! textBox3.Text = (val1 + val2).ToString();! ! "(0!/-30*!-,0!+*01!'#!1)*')/2+)*(!70'&00/!'(0!8#/',#%*!#/!'(0!$#,3N!! L#+!8-/!1#!'(0!*-30!'()/2!&)'(!7+''#/*=!Q+*'!8%)8;!#/!-!7+''#/=!-/1!8(-/20!)'*!/-30!9,#90,'.N!O'!)*! ,08#330/101!'(-'!.#+!,0/-30!.#+,!7+''#/*!*#!'(-'!.#+!;/#&!&(-'!0-8(!#$!'(03!1#0*N! Events"and"classe s" A(0/!.#+!1#+7%0!8%)8;!-!7+''#/=!.#+!-,0!-+'#3-')8-%%.!,01),08'01!'#!.#+,!8#10!&(0,0!-!/0&!$+/8')#/! -990-,*N!"(-'!$+/8')#/!&)%%!70!0K08+'01!&(0/!.#+!8%)8;!'(0!7+''#/N!L#+!8-/!,+/!-/.!#'(0,!$+/8')#/!&(0/! .#+!8%)8;!'(0!7+''#/=!-%%!.#+!(-<0!'#!1#!8(-/20!'(0!0<0/'!'(-'!#88+,*!&(0/!.#+!8%)8;!)'N!"#!1#!'(-'=!2#!'#!)'*! 9,#90,'.!&)/1#&=!-/1!8%)8;!#/!'(0!RJ<0/'*T!7+''#/!: ?N!V/!'(0!%0$'!*)10!#$!'(0!'-7%0!'(-'!Q+*'!-990-,01! 70%%#&!.#+!&)%%!*00!'(0!0<0/'*=!-/1!#/!'(0!,)2('!*)10!.#+!&)%%!*00!&(-'!(-990/*!:A(-'!$+/8')#/!)*! 0K08+'01?!&(0/!'(-'!0<0/'!#88+,*N! !@*!.#+!8-/!*00=!&(0/!&0!Click!'(0!7+''#/=!'(0!button1_Click!$+/8')#/!)*!0K08+'01N!"(-'!$+/8')#/!(-*!700/! -+'#3-')8-%%.!&,)''0/!$#,!.#+!&(0/!.#+!1#+7%058%)8;!'(0!7+''#/N!V$!8#+,*0=!'(0!*-30!$+/8')#/!8-/!70! 0K08+'01!/#!3-''0,!&(-'!0<0/'!#88+,*=!$#,!0K-39%0=!2#!'#!'(0!MouseHover!0<0/'=!-/1!'.90!button1_Click! '#!)'*!%0$'N!U,#3!/#&=!.#+!1#/W'!(-<0!'#!8%)8;!'(0!7+''#/=!-%%!.#+!(-<0!'#!1#!)*!9%-80!'(0!8+,*#,!-7#<0!)'!-/1! '(0!button1_Click!$+/8')#/!&)%%!,+/=!-11)/2!'(0!'&#!/+370,*N! L#+!8-/!-%*#!108%-,0!-!/0&!$+/8')#/!'#!1#!'(0!Q#7=!-/1!8-%%!)'!&(0/!.#+!8%)8;!'(0!7+''#/N!".90!*#30&(0,0! 70%#&!'(0!button1_Click3$+/8')#/4! double AddNumbers(double val1, double val2) { }! ! "()*!)*!(#&!&0!108%-,0!-!/0&!$+/8')#/!)/!HIN!"(0!8#,,08'!*./'-K!)*4! 10" VISUAL"C#"PROGRAMMING"BASICS# ! value_returened function_name(parameter_type parameter_name) { code }! ! "(0!AddNumbers3$+/8')#/!&)%%!,0'+,/!'(0!*+3!#$!'(0!'&#!/+370,*N!".90!)/*)10!'(0!'&#!8+,%.!7,-80*4! return val1 + val2;! ! RreturnT!)*!+*01!'#!*908)$.!&(-'!<-%+0!'(0!$+/8')#/!&)%%!,0'+,/!&(0/!)'!)*!8-%%01N!"#!8-%%!'()*!$+/8')#/=!10%0'0! '(0!8#10!)/!'(0!button1_Click3$+/8')#/!-/1!'.904! private void button1_Click(object sender, EventArgs e) { double val1 = double.Parse(textBox1.Text); double val2 = double.Parse(textBox2.Text); textBox3.Text = AddNumbers(val1, val2).ToString(); }! ! @*!.#+!8-/!*00=!&0!8-/!-**)2/!-!<-%+0!'#!-!<-,)-7%0!&(0/!&0!108%-,0!)'N!A0!+*01!'(0!AddNumbers!$+/8')#/! '#!-11!'(0!'&#!/+370,*N! ! How3does3this3work?3 3 ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! Form13 Z+''#/!)*!8%)8;01! ! d! d! e! button1_Click3f!U+/8')#/!)*! 0K08+'014! <-%]! <-%d! ! AddNumbers3:<-%]=!<-%d?! return3<-%]c<-%d! ! [...]... the
internet,
how
you
will
learn
to
use
them
is
the
biggest
problem.
 Setup
Projects
 In
case
you
have Visual Studio
Professional
installed,
under
“Other
Project
Types”
in
“Setup
and
 Deployment”
you
have
this:
 
 The
setup
project
is
the
easiest
way
to
create
a
decent
and
customizable
setup
for
your
project.
 
 
 
 
 VISUAL C# PROGRAMMING BASICS 15
 
 Visual C#
Syntax
 This
chapter
will
show
you
some
basic
C#
code
for
doing
different
operations.
... We
typed
“public”
before
declaring
the
function
so
we
can
use
it
outside
this
class.
 Now,
go
back
to
“Form1.cs”
and
declare
the
class
right
on
top
of
the
Main
function:
 Calculator Calc = new Calculator();
 
 12
 
 VISUAL C# PROGRAMMING BASICS The
correct
syntax
is:

 ClassName WhatNameYouWantToUse = new ClassName();
 
 Now
that
we
declared
the
class,
we
need
to
use
the
function
from
it
named
“AddNumbers”.
To
do
that,
 change
the
following
line
of
the
button1_Click
function:
... button2_Click(object sender, EventArgs e) { double val1 = double.Parse(textBox1.Text); double val2 = double.Parse(textBox2.Text); textBox3.Text = Calc.MultiplyNumbers(val1, val2).ToString(); }
 
 
 VISUAL C# PROGRAMMING BASICS 13
 Solutions
and
Projects
 It
is
time
to
learn
how
the
solution
explorer
works.

 File
types
 All
you
have
to
care
about
in
the
solution
explorer
are
the
forms,
 classes
and
references.

... The
“Windows
Forms
Application”
project
is
what
we
previously
made.
It
is
just
a
simple
application
with
 a
user
interface.
On
the
other
hand,
the
“Console
Application”
is
a
simple
C#
application
project
without
 the
user
interface,
it
works
inside
the
console.
 14
 
 VISUAL C# PROGRAMMING BASICS The
“Class
Library”
is
actually
a
project
full
of
classes.
Once
you
compile
the
project,
you
will
end
up
with
a
 “.DLL”
file
(Dynamic
Link
Library).
You
can
add
that
file
as
a
reference
to
your
project
so
you
can
use
the
.. .VISUAL C# PROGRAMMING BASICS 11
 
 Note
how
the
button1_Click
function
passes
the
values
to
the
AddNumbers
and
then
assigns
them
to
the
 third
text
box.
 Debug
your
application
and
you
will
notice
that
it
will
work.
The
best
part
in
using
function
to
do
your
job
... to end the loop }
 
 Decisions
 //if then condition int i = 4; bool ok=false; if (i / 2 == 2) { ok = true; } else { i++; } string String = "1234"; //Notice that C# is case-sensitive 16
 
 VISUAL C# PROGRAMMING BASICS //This is the switch-case command //it works like multiple if's switch (String) { //This happens when no other value works default: break; //In CASE String is "12345", this is what happens... StreamReader OtherName = File.OpenText(FilePath); string Something = OtherName.ReadLine(); //Reading a line string AllContent = OtherName.ReadToEnd(); //Reading everything to end OtherName.Close(); VISUAL C# PROGRAMMING BASICS 17
 
 Windows
Forms
Controls
 In
this
chapter
you
will
learn
how
to
use
most
of
the
controls
that
you
can
find
in
your
toolbox.
We
are
still
 going
to
work
on
the
calculator
project
until
we
reach
some
control
that
we
can’t
use
in
such
a
project.
... Drag
a
check
box
on
your
form.
Then
double‐click
on
it
to
open
the
function
that
occurs
when
it’s
check
is
 changed
and
type:
 private void checkBox1_CheckedChanged(object sender, EventArgs e) { if (checkBox1.Checked == true) { button1.Enabled = true; } else 18
 
 VISUAL C# PROGRAMMING BASICS { button1.Enabled = false; }
 } 
 This
will
disable
the
button
if
it
is
unchecked
and
enable
it
if
it
is
checked.
As
you
can
see,
the
“.checked”
 property
is
a
Boolean
type
variable.
... Fortunately,
if
you
add
more
to
the
form,
only
one
of
them
can
be
checked
at
once.
 If
you
want
more
radio
buttons
but
for
something
different,
just
make
a
new
panel
and
put
them
in
there
 so
that
they
won’t
un‐check
when
you
click
the
other
buttons.
 
 VISUAL C# PROGRAMMING BASICS 19
 
 Contents
 General
Requirements 3
 Quick
Start
–
Your
first
application 3
 Step
1
–
Creating
the
project 3
 Step
2
–
Designing
the
form... going
to
work
on
the
calculator
project
until
we
reach
some
control
that
we
can’t
use
in
such
a
project.
 Also,
keep
in
mind
that
you
will
learn
to
use
some
easy
control
in
this
chapter,
for
more
difficult
controls,
 see
the
“Advanced Visual C# Programming 
tutorial.
 Forms
 This
is
a
must‐read
item
because
you
will
need
to
know
how
to
make
your
form
look
better.
For
start,
our
 main
form
in
calculator
named
“Form1”
is
resizable,
even
if
it
has
only
4
controls
in
it.
Once
you
resize
it,
it
 . " ! ;&'(+,#78#A*)B*+00&"B#C+'&3'# # %$$2DEE6+@&6/@&$/,+*(93)0E# 2" VISUAL& quot;C#" ;PROGRAMMING& quot ;BASICS# ! VISUAL& quot;C#" ;PROGRAMMING& quot ;BASICS& quot; 3# ! Genera l " Requirements" "#!$#%%#&!'()*!'+'#,)-%!.#+!&)%%!/001!'(0!$#%%#&)/2!)'03*4! 56/#&)/2!'(0!7-*)8*!#$!-'!%0-*'!#/0!9,#2,-33)/2!%-/2+-20!:"#!;/#&!&(-'!<-,)-7%0*=! -,,. !'#!8,0-'0!-!1080/'!-/1!8+*'#3)_-7%0!*0'+9!$#,!.#+,!9,#Q08'N! ! ! ! ! VISUAL& quot;C#" ;PROGRAMMING& quot ;BASICS& quot; 15# ! Visual& quot;C#"Synta x" "()*!8(-9'0,!&)%%!*(#&!.#+!*#30!7-*)8!HI!8#10!$#,!1#)/2!1)$$0,0/'!#90,-')#/*N! Variables"&"Operations" . g"the"form" A(-'!&0!&-/'!'#!8,0-'0!)*!-!*)39%0!8-%8+%-'#,!-99%)8-')#/N!A(-'!)'!&)%%!1#!)*!'#!-11!'&#!/+370,*!)/*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8-/!70!,0*)_01!&)'(!+*)/2!'(0!3#+*0N! H%)8;!#/!'(0!7+''#/!:G-;0!*+,0!.#+!1#/W'!1#+7%0!8%)8;!)'=!#,!)'*!8#10!&)%%!#90/N!O$!'(-'!(-990/*=!8%#*0!'(0!'-7! &)'(!'(0!8#10!$,#3!'(0!'#9!#$!'(0!3)11%059-/0%?N!V/80!8%)8;01=!'(0!7+''#/W*!9,#90,')0*!&)%%!-990-,!)/!'(0! RC,#90,')0*T!&)/1#&N!M8,#%%!1#&/!-/1!2#!'#!R"0K'TN!"#!)'*!,)2('!.#+!&)%%!*00!RZ+''#/]TN!H(-/20!'(-'!'#! R@11T=!-/1!9,0**!0/'0,N! 6" VISUAL& quot;C#" ;PROGRAMMING& quot ;BASICS# ! ! L#+,!7+''#/!/#&!(-*!R@11T!&,)''0/!#/!)'N!E0,.!2##1=!'()*!&

Ngày đăng: 28/04/2014, 15:33

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan