You are on page 1of 128

C#

Microsoft

www.microsoft.ru
www.dreamspark.ru

-

www.intuit.ru

.

www.lbz.ru

2010

004.438.045C#(075.3)
32.973.26-018.1C#.721-1
73

73

.
C# : / . . . . .
.: - ; . ,
2009. 128 .: ., . ( ).
ISBN 978-5-9963-0236-9 (.)
( 1216 ) - , Windows.
Microsoft C# ( -) . -
, .
004.438.045C#(075.3)
32.973.26-018.1C#.721-1

-
, ,

Microsoft .

:
.
(499) 157-1902, (499) 157-5272,
e-mail: Binom@lbz.ru, http://www.Lbz.ru

ISBN 978-5-9963-0236-9 (.)

-
, 2010
. , 2010

3

, , 7 . 4 . ? 7 ? , : , ,
.
,
, . , .
, .
, , , , ,
, , - . : ,
, If then,
, 12 , . .
, , , , , .
, .
, .
. , . , .
, , . . , ,
, , .
, - , , . , .
C#, Visual Studio .Net, SQL Server , Microsoft.
.
. , , .
, ,
, . , .
, , , , ,
XML-, , , , .
:

( 1216 ) - , Windows.
Microsoft C# ( -)
. -
, .


(Martin Dreyer) , , . .

1.


, , , :
! ! .
, !

, ,
. ? : , ? ? , .
, , : -
, !
? :
, , , , .
: ,
. - ,
. - : ,
. , - . , , , .


, , ,
. , ,
. ,
, .

, : !
C# :
1. Microsoft .NET Framework 3.5. , , ,
, . Microsoft .NET
Framework 3.5
http://msdn.microsoft.com/ru-ru/netframework/aa569263.aspx.
2. , .Net Framework . ,
. Microsoft
Visual C# 2008 Express Edition,
:
http://www.microsoft.com/rus/express/vcsharp.
Visual C# 2008 Express,
.NET Framework, 1
.
3. ,
.
http://intuit.ru/software/school/cs2kids/examples.zip,
,
.

Microsoft Visual C# Express Edition


C# (, )
(.. , )
C#, .NET Framework.
Visual C# Express C#.
Windows,
. Visual C# Express , , .
, Visual C# Express
Edition.
Visual C# Express , , C#, - . , , C#, .
C# , .
Visual C# Express
: http://www.microsoft.com/rus/express/vcsharp.

() Visual C# Express
Visual C# Express, Microsoft Visual C# Express Edition.
(), .
File (), New project ( ) . , Windows.

.
. , Program.cs.
, . : , . !

using System;
class PleaseSayUra
{
static void Main()
{
Console.WriteLine(!);
Console.ReadLine();
}
}
, . ( F5).

, !.

X
.
, .
, C#
. (;)
:

,
, , . , .

,
( , ) , .
. (Solution) Examples, Examples.zip http://intuit.ru/software/school/cs2kids/examples.zip. ,
Examples, . databases
, . Example1, , Example15 C# .
, .
Examples Examples.sln,
. :
, . . :
, : , ,
10, Example10.
, (Set as StartUp Project).
. : F5, ,
Ctrl+F5 . : Debug .
, :

10

C#
II . . .
:

C#
.
:

class Animal
{
}

, Visual C# Express, , , , .
, .

, C#.

11

2.


, . , ,
.
: .
: ? !. , , , , , .

, : , :

, , .
.
, , . :
, (, ),
. ,
.
, , . , , , .
, ,
, .

12

, ,
.
, - ,
. , , ?, , ,
.

.
, . , ?
, ,
-. , , -

13

. , . .

, : , , , !
. . . .
! ,
. - . , . ! - , , , , .

14

2
- . , :
, . - . , , , .
, , , , . :
! 12 ( !) . , , . , , .
, : , , , . .

, , , .
, . , , (
, , ). .
, ! , . ( , .)

. -, . -, ,
, . -, ,
.

15

, , , ...
. . .
, .
: , .
. ( ), , ?
: , , , . , : , -, .
, : :
- .
.
, ,
( ).
,
, .
.

, , , , , . : , ?. : , ...
, -.

16

, .
:
name (, , 12);
earShape (, );
likeToWatchTV (, ,
).
, . -
, ,
.
.
. ,
: , , , . , , .
, . , , .

C#
, , , ( ) . ,
, .
, . (, - )
, C# .
, C# ,
, , . .
, , :
C# ;
C# .
C# , - .
: . ,
.
,
.
: , , . , . ,

.

17

. , .
, , , ,
. , ,
, .
, . . , , , . .
, . . , ,
, .
, , . , , , . , , . ,
, , , , .
. ,
. , , , .
, , ,
. , ,
, , .
- .
C#.

18

. .
, . . : .
, ,
.

: .
, ,
.
C# :

class Animal
{
}
:
;
Animal;
{ }.
Animal , . , , MyPhotoProgram MyGradeCalculator. , , .
.

19

C#
.
class,
,

, ,
.

class Animal
{
}

, ( ). .
, .
Animal :
, ;
, ;
;
.


, . , - . , , , .
:

Animal Barsik;

20

, , . : Animal Barsik. , Barsik,


, .
:

Barsik = new Animal();


, , . :

21

(Animal Barsik) , Animal. .


. , , . ,
, Animal Barsik.
(Barsik = new Animal();) : Barsik? .
, . Animal , Barsik, .
Animal, - ,
, .
, .
, , 2 3
Animal.
// . - fiddlestick , fiddlesticks ,
. , , , .
:

,
.
,
.

class Animal
{
}


.
,
,
,
.
, .

Animal percyThePorcupine;
//

.

.

,
.
percyThePorcupine
Animal, ,

percyThePorcupine =
new Animal();

22

percyThePorcupine Animal.
,

Animal. .
, , , , ( , ),
. , .


, :
(Kind of animal).
(Height).
(Length).
(Number of legs).
(Color).
(Has a tail).
(Is a mammal).
:
Kind of animal = Cat ().
Height = 50 cm ( !).
Length = 110 cm ( , !).
Number of legs = 4.
Color = Black ().
Has tail = true ().
Is mammal = true ().

23

C# . (
, .)
, ,
. : , .
.
height, length number of legs . , .

: numberOfBananas (), numberOfPeople () numberEachOneGets (). , .


. C# .
, .
kindOfAnimal color .
. cat black, .

24

2
, ( ) . , 17- , ! ,
, , , , .
hasTail
(true/false), , (yes/no).
.

, . numberOfLegs
, ,
, , 500 ,
.

String ()
,
, :

string kindOfAnimal;
, , , . ,
, :

25


, , , C#.
integer. , , , , .

, , :

int numberOfLegs;
:

26

Boolean ( )
, true ()
false () , C# true/false . ,
(Boolean), .
, hasTail true/false, :

bool hasTail;
:

, , . .


, ,
Animal, , , . :

class Animal
{
...
}

27

class Animal
{
string kindOfAnimal;
string name;
int numberOfLegs;
int height;
int length;
string color;
bool hasTail;
bool isMammal;
bool spellingCorrect;
}
, , . TAB ,
, .


, , Animal
. ,
Animal ( ),
, .

Animal Barsik;
Barsik = new Animal();
Barsik.kindOfAnimal = Cat;
Barsik.name = ;
Barsik.numberOfLegs = 4;
Barsik.height = 50;
Barsik.length = 110;
Barsik.color = Black;
Barsik.hasTail = true;
Barsik.isMammal = true;
, () .
(;), .
, Barsik , ,
Barsik X :

28

Animal X;
X = new Animal();
X.kindOfAnimal = Cat;
X.height = 50;
X.length = 110;
X.color = Black;
X.hasTail = true;
X.isMammal = true;
. , (
, 12), , . , , .
. , (). , ,
(), . , ,
, .
:


,

.

class SchoolKid
{
string firstName;
string lastName;
int age;
bool isWideAvake;
}

. , :

string

,

,
, , ..

int

Petr ()
Eight-legged Octopus (
)
Girls are too clever to fall out of their
prams ( ,
)
328
28000
-520

,
.
.
integer
, True

False
(true), (false).
boolean.

bool

29

string lastname;
. , .
, , , VsegdaNacheku . : .
,
. , , schoolKid (), :

schoolKid.firstName = Petr;
schoolKid.age = 13;
(.).
, schoolKid.firstName = Petr : Petr firstName () schoolKid.

,
C# ,
private, protected public. .

class Animal
{
string kindOfAnimal;
string name;
int numberOfLegs;
int height;
int length;
string color;
bool hasTail;
bool isMammal;
bool spellingCorrect;
}
class MyTestClass
{
Animal myAnimal;
}
. , MyTestClass
myAnimal, Animal.
, , ,
. (private) , ,

30

. private, protected public .

, . , . (private)
, . , . ,
, - (protected). ,
, (public) , .
, :

class Animal
{
public string kindOfAnimal;
public string name;
public int numberOfLegs;
public int height;
public int length;
public string color;
bool hasTail;
protected bool isMammal;
private bool spellingCorrect;
}
: , ? private ,
private.
hasTail (private).

31

:
Private .
Public .
Protected - . Animal, , , Mammal (), Animal.
public
private. protected ,
Visual C# Express.
, Animal
Zoo.

class Animal
{
public string kindOfAnimal;
public string name;
public int numberOfLegs;
public int height;
public int length;
public string color;
bool hasTail;
protected bool isMammal;
private bool spellingCorrect;
}
class Zoo
{
Animal a = new Animal ();
// , Zoo
// Animal
a.kindOfAnimal = Kangaroo;
// , Zoo
//
a.isMammal = false;
//
a.spellingCorrect = true; //
}

, . , , - :
;
;

32

-;
;
.
, . , , , , , . ,
, , .

, ,.

, , , , , , .

33

C# . .
. , .
:

void SayHello()
{
}
, .
- .

void SayHello()
{
Console.WriteLine(Hello);
}
( TAB .)
, Hello.
, .

VOID?
void , , . .
: . Void , .

34

void , , , , , , . SayHello , Hello


. .
, , . .


, , , , .
, , ,
, .
, . :

SayHello();
, , ,
,
SayHello. , , .
, , .
:


. .
,

, .
, (//),
.
, .
+ .
, .

.
, Person
.

class Person
{
//
public string firstName;
public string lastName;
//
public void ShowFullName()
{
Console.WriteLine(Name is
+ firstName + +
lastName);
}
}
Person Petr;
Petr = new Person();
Petr.firstName = Petr;


ShowFullName,
Person
.

35
Petr.lastName = Ivanov;
Petr.ShowFullName();

?
, . , , .
, , :

Hello Jo
Hello Sam
Hello You
:

void WriteHelloJo()
{
Console.WriteLine(Hello Jo);
}
void WriteHelloSam()
{
Console.WriteLine(Hello Sam);
}
void WriteHelloYou()
{
Console.WriteLine(Hello You);
}
:

WriteHelloJo();
WriteHelloSam();
WriteHelloYou();
. WriteHello, , , ?

36

2
:

void WriteHello(string someName)


{
Console.WriteLine(Hello + someName);
}
:

WriteHello(Jo);
WriteHello(Sam);
WriteHello(You);
, , .
, .
// . , .
.

void WriteHello(string someName)


{
Console.WriteLine(Hello + someName);
}

37

:
- .
Hello.
(string someName) .
.
,
, : 2 5, 7 3.
, , : , , ,
.
, , ,
. , :

void WriteHello(string x)
{
Console.WriteLine(Hello + x);
}
:

void WriteHello(string someName)


{
Console.WriteLine(Hello + someBodysName);
}
: someName someBodysName
.
, , , :

void WriteHello(string firstName, string lastName)


{
Console.WriteLine(Hello + firstName + + lastName);
}
:

WriteHello(Petr, Ivanov);
Hello Petr Ivanov.

, ,
: 5 .

38

? , , , . .
, . . - , , , , , , .
:

,
.

class Person
{
//
string firstName;
string lastName;
//
public void LuckyNumber(
int numberOfTeeth, int age)
{
Console.WriteLine(
+
numberOfTeeth * age);
}
}

,
.
,
LuckyNumber - Person Petr;
.
Petr= new Person();

Petr.LuckyNumber(24, 14);

// . :
Console.WriteLine, , ?. , (, C#)

.
void-.
, : . .
, , void.
. ,
, .
, , . , , , C#:
, , , number of legs = 4;
, , , , number of legs = 2;

39

, , , , number of legs = 1;
, - , number of legs = 0.

int NumberOfLegs(string animalName)


{
if (animalName == ) //
{
// 4
return 4;
}
else if (animalName == ) //,
{
// 2
return 2;
}
else if (animalName == )//,
{
// 1
return 1;
}
else // ( )
{
// 0
return 0;
}
}
. :

int i;
// i .
i = NumberOfLegs();
// i = 2, , NumberOfLegs
Console.WriteLine( + i);
i = NumberOfLegs();
// i = 0. , !
Console.WriteLine( + i);
: 2,
0. , , .
:

int NumberOfLegs(string animalName)


{
...
}

40

2
:

void NumberOfLegs(string animalName)


{
...
}
:

string NumberOfLegs(string animalName)


{
...
}
, ,
(void), , . Integer, int .
, . , void .

void JustWriteSomething(string someThing)


{
Console.WriteLine(someThing);
}
: , , return . , .
:

class Person
{


.
void, ,
, , .

//
string firstName;
string lastName;
//
int LuckyNumber(int numberOfTeeth,
int age)
{
return (numberOfTeeth * age);
}

}
- Person Anna;
, - Anna = new Person();
.
int num = Anna.LuckyNumber(24, 14);


, ,

.

41

Console.WriteLine(
: + num);

- Console.WriteLine(
WriteLine.
: + Anna.LuckyNumber(24, 14));
, ,
. private (), . ,
public .

public void JustWriteSomething(string someThing)


{
Console.WriteLine(someThing);
}

. ,
. , . .
. :
(private) (public) Animal
.

class Animal
{
//

42

2
public string kindOfAnimal;
public string name;
public int numberOfLegs;
public int height;
public int length;
public string color;
bool hasTail;
protected bool isMammal;
private bool spellingCorrect;
//
// , ,
public string GetFoodInfo()
{
// , ,

}
//
private void SpellingCorrect()
{
// ,

}
// ,
protected bool IsValidAnimalType()
{
//

}
class Zoo
{
Animal a = new Animal ();
a.name = Kangaroo;
string food;
bool animalExists;
// , Zoo
// Animal
food = a.GetFoodInfo(); //
// , Zoo

43

//
a.spellingCorrect();
//
animalExists = a.IsValidAnimalType();
//
}
, .
( ) , -
.

() .
.
// . , .
, ,
. , .
.

class Person
{
//
string firstName;
string lastName;
// - Person
public Person()
{
firstName = Johnny;
lastName = Rocket;
}
}
- -: new.
:
. ,
, Animal.
, :

Person p = new Person();


Console.WriteLine(p.lastName);
Rocket. new Person(),
Person -

44

. p, Rocket
lastName.
.
, , ,
.

-, .
- , -.
, .

.
:

class Person
{
//
string firstName;
string lastName;
// -
public Person()
{
firstName = Johnny;
lastName = Rocket;

45

}
// -
public Person(string f, string l)
{
this.firstName = f;
this.lastName = l;
}
}
, .
, :

Person p = new Person();


p.lastName Rocket.
:

Person p = new Person(Petr, Ivanov);


p.lastName Ivanov.
this , , : , -.
// . this
. , this. . ,
f, firstName, , this , , , , .

, . , ( ,
). : , .
, :
, ;
;
;
.
, , ,
. ( ?) ,

46

- , ,
.
, , , , . ,
mrButton, .
. :
Visual C# Express.
Windows: File () Create
project ( ) Windows Application (
Windows Forms).
Visual C# Express , .
( )
Form1.cs.
Program.cs
.
,
Program.cs , (
.

using System;
using System.Windows.Forms;
class MyButtonClass: Form
{
private Button mrButton;
// -
public MyButtonClass()
{
mrButton = new Button();
mrButton.Text = ;
this.Controls.Add(mrButton);
}

47

//
static void Main()
{
Application.Run(new MyButtonClass());
}
}
F5 ( ). , , .
, .
. ,
, .

.
, .
, . , :

using System;
using System.Windows.Forms;
class MyButtonClass: Form
{
private Button mrButton;
// -
public MyButtonClass()
{
mrButton = new Button();
mrButton.Text = !;
this.Controls.Add(mrButton);
}
//
static void Main()
{
Application.Run(new MyButtonClass());
}
// -
void MyButtonClickEventHandler(object sender, EventArgs e)
{
mrButton.Text = !;
}
}

48

?
( X ,
).
F5 .
. ?!
, ,
. void , . MyButtonClickEventHandler.
, , , . , , (object sender, EventArgs
e)? , . Click, , . .
, .

. object EventArgs.
sender e,
. ,
, , . , x y.
// . :
. . ,
, .

void MyButtonClickEventHandler(object x, EventArgs y)


{
mrButton.Text = !;
}
, . .
: , .


, ? , , , -

49

:
.
, ,. , .
. .

using System;
using System.Windows.Forms;
class MyButtonClass : Form
{
private Button mrButton;
// -
public MyButtonClass()
{
mrButton = new Button();
mrButton.Text = ;
mrButton.Click += new System.EventHandler(MyButtonClickEventHandler);
this.Controls.Add(mrButton);
}
//
static void Main()
{
Application.Run(new MyButtonClass());
}
// -
void MyButtonClickEventHandler(object sender, EventArgs e)
{
mrButton.Text = !;
}
}
:
mrButton Click , MyButtonClickEventHandler.
, !.
, ,
F5 . , . , ( Example5).

50

2
// . , .
.
, , .

public MyButtonClass()
{
mrButton = new Button();
mrButton.Text = ;
mrButton.Top = 100;
mrButton.Left = 100;
mrButton.Height = 50;
mrButton.Width = 70;
mrButton.Click += new System.EventHandler(MyButtonClickEventHandler);
this.Controls.Add(mrButton);
}
. , ,
MouseEventArgs, , , .

public void TheMouseIsDown(object sender, MouseEventArgs e)


{
if (e.Button = MouseButtons.Left)
this.Text = ;
}
, . : , TheMouseIsDown, , :

this.MouseDown += new MouseEventHandler(TheMouseIsDown);

, , , .

51

public void TheMouseWasClicked(object sender, MouseEventArgs e)


{
//
if (e.Button == MouseButtons.Left)
//
this.Width = this.Width + 100;
else if (e.Button == MouseButtons.Right)
//
this.Width = this.Width 100;
}
, :

public void TheMouseMoved(object sender, MouseEventArgs e)


{
//
System.Drawing.Graphics g = this.CreateGraphics();
//
System.Drawing.Pen redPen = new System.Drawing.Pen(Color.Red, 3);
// .
// .
// X Y
// .
g.DrawEllipse(redPen, e.X, e.Y, 40, 40);
//
g.Dispose();
}
, :

52


, , , , .. .
, ? ,
. , , .

, :
, 17;
, 82,
, .
:
, 17, . ;
, 82, . -
- , ? ,
! :
, 17, . , ;
, 82, . -, .
:
, . , , 17;
, . -, , 82.
. , , -,
82.
( , . -, . )
.


?
, Microsoft Point,
, Point, ,
, -.

53

// . Point ,
.
, ,
Point. ?
, . .NET . , :

Microsoft.Drawing.Point
Anna.PictureStuff.Point
(Point),
, . ,

Point Anna, Point -:

Anna.PictureStuff.Point annaPoint = new Anna.PictureStuff.Point();


Point Microsoft :

Microsoft.Drawing.Point microsoftPoint = new Microsoft.Drawing.Point();


Point,
.


.
:

class Animal
{
...
}
:

namespace Anna
{
class Animal
{
...
}
}

54

2
, , :

namespace Anna.FunStuff
{
class Animal
{
...
}
}
. . Animal Anna.FunStuff.
, , , . ,
, , .
:




.
.

namespace Charles.SchoolUtilities
{
class Animal
{
}
}


Charles.SchoolUtilities.Animal cat;
,
cat = new Charles.SchoolUtilities.Animal();
.


,
using
.

.

// ,
//
using Charles.SchoolUtilities;
// Animal
// Charles.SchoolUtilities
Animal cat = new Animal();

, , ,
.

55

class MyFancyClass : Form


{
...
}
:

class MyFancyClass
{
...
}
, ,
.


, , .
, .

, . ,
,
.


, - .

56

. Animal Bird. Bird Animal.

class Animal
{
//
public string kindOfAnimal;
public string name;
public int numberOfLegs;
public int height;
public int length;
public string color;
bool hasTail;
protected bool isMammal;
private bool spellingCorrect;

}
// Bird Animal
class Bird : Animal
{
public string featherColor;

}
, ,
. Bird
Animal . , , featherColor .
, :

class Bird : Animal


{
...
}
: Bird, Animal. , Bird Animal.
Bird ,
Bird, Animal, ,
:

Bird b = new Bird();


b.kindOfAnimal = ;

57

b.isMammal = false;
b.featherColor = -;
// . , . ,
: Bird, (protected)
isMammal , Bird Animal; , .
: ,
. ( Bird)
( Animal). Bird
Animal, Car, , Animal,
.
Animal Bird,
, .
, .


, , ,
, , , . , ,
.
System.Windows.Forms.Form.
:

class MyFancyClass : Form


{
...
}
: , Form.


!
, , , , .

58

public class Musician



.

{
public string name;
public int age;
public bool canSing;
}

Guitarist ,
, .
Musician, ,
, Guitarist

Musician.
Guitarist,
,
.

public class Guitarist : Musician


{
public string guitarType;
public string guitarBrand;
}
Guitarist g = new Guitarist();
g.name = Jimbo van Helsdingen;
g.ageInYears = 28;
g.canSing = false;
g.guitarType = Acoustic;
g.guitarBrand = Gibson;

.NET Framework

59

3. .NET Framework

.NET Framework?
C# .NET Framework . , .
.NET Framework? , Microsoft, C# .. .NET Framework .
1. -, ,
, C#.
.
2. -, , (
).
, C# ( .NET)
, .

.NET framework , , . ,
, , .
, , ,

60

. , ,
.
: , , , . , ,
http://intuit.ru/software/school/cs2kids/examples.zip.


: : ! , ?
.NET Framework, ! ?
:
.NET Framework SDK (Software Development Kit
), Visual C# Express.
Visual C# Express. ,
, , . . , Console.
Console.

. , , . C# -

.NET Framework

61

( )
. , , Console.WriteLine(.
Visual C# Express 19 WriteLine.
11- (. ).


, ,
. -

62

, , . ,
.
. ,
.

, , , .
. , , -
, :
. , ? .NET
.

.
.
, - , , ,
, , .
:
1. Console.ReadLine , (
).
2. Console.WriteLine ( )
.
3. Console.Write .

.NET Framework

63

1
! .
1

using System;
class PleaseSayUra
{
static void Main()
{
//
Console.Write(!);
//
Console.ReadLine();
}
}

2
:
;
, ;
,
.
2

using System;
class WriteAFewLines
{
static void Main()
{
Console.WriteLine( : );
//
string favouriteWord = Console.ReadLine();

64

3
Console.WriteLine( ? );
//
// ( )
int numberOfTimes = Convert.ToInt32(Console.ReadLine());
//
for (int i = 0; i < numberOfTimes; i++)
{
Console.WriteLine(favouriteWord);
}
//
Console.ReadLine();

}
}

Windows Forms
, Windows, System.Windows.Forms. , , , , . , .
, , ( Label) ( PictureBox), , , .
.
, ,
. (, , . .). -

.NET Framework

65

,
, .

: ,
, , . ., . .
.NET Framework , . , .

System.Windows.Forms
, :
Label ().
Button ().
ListBox ().
CheckBox ().
RadioButton ().
MessageBox ( ).
Menu ().
TabControl ( ).
Toolbar ( ).
TreeView ().
DataGrid ( ).
PictureBox ().
RichTextBox ( RTF).

66

Windows Forms Visual C# Express


, , . , C# Express
( Form1.cs Program.cs) C#, , .
:
Form1.cs.
Program.cs , .
,
, .

3
Windows Forms.
.
3

using System.Windows.Forms;
class SimpleWindowsForm : Form
{
// -
public SimpleWindowsForm()
{
//
this.Text = ;
}
static void Main()
{
//
//
//
Application.Run(new SimpleWindowsForm());
}
}

.NET Framework

67

4
,
.
4

using System.Windows.Forms;
class SimpleWindowsFormWithButton : Form
{
Button button1;
// -
public SimpleWindowsFormWithButton()
{
//
this.Text = ;
//
// : !,
// !
button1 = new Button();
button1.Text = !;
button1.Top = 100;
button1.Left = 100;
button1.Height = 50;
button1.Width = 70;
this.Controls.Add(button1);
}
static void Main()

68

{
//
Application.Run(new SimpleWindowsFormWithButton());
}
}

5
, . .
, -
. . ( ) -
, , , . ,
, .
5

using System;
using System.Windows.Forms;
using System.Drawing;
class FormWithWorkingButton : Form
{
Button mrButton;
// -
public FormWithWorkingButton()
{
//
this.Text = !;

.NET Framework

69

//
mrButton = new Button();
mrButton.Text = ;
mrButton.Top = 100;
mrButton.Left = 100;
mrButton.Height = 50;
mrButton.Width = 70;
mrButton.Click += new System.EventHandler(mrButton_Click);
this.Controls.Add(mrButton);
}
static void Main()
{
//
Application.Run(new FormWithWorkingButton());
}
// ,
void mrButton_Click(object sender, EventArgs e)
{
//
mrButton.Text = !;
}
}

6
: .
, . 4- : Button, ListBox, MessageBox
PictureBox.

70

: System.Windows.Forms System.Drawing. , PictureBox,


Drawing.
6

using System.Windows.Forms;
using System.Drawing;
class MyForm : Form
{
// ListBox :
//
ListBox listBox1;
// -
public MyForm()
{
//
this.Size = new Size(400, 400);
// PictureBox, ,
//
PictureBox pictureBox1 = new PictureBox();
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
Bitmap image1 = new Bitmap (..//..//images//Zakat.jpg);
pictureBox1.ClientSize = new Size(this.Width, 150);
pictureBox1.Image = (Image)image1;
this.Controls.Add(pictureBox1);
// Button,
Button button1 = new System.Windows.Forms.Button();
button1.Location = new Point(150, 160);
button1.Size = new Size(100, 30);
button1.Text = ;
button1.Click += new System.EventHandler(button1_Click);
this.Controls.Add(button1);
// ListBox,
listBox1 = new System.Windows.Forms.ListBox();
listBox1.Location = new System.Drawing.Point(20, 200);
listBox1.Size = new Size(100, 100);
listBox1.Items.Add();
listBox1.Items.Add( );
listBox1.Items.Add();
listBox1.Items.Add();

.NET Framework
listBox1.Items.Add();
listBox1.SelectedIndex = 2;
this.Controls.Add(listBox1);
}
// ,
void button1_Click(object sender, System.EventArgs e)
{
//
MessageBox.Show(this, + listBox1.SelectedItem,
, MessageBoxButtons.OK);
}
static void Main()
{
//
Application.Run(new MyForm());
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// MyForm
//
this.BackColor = System.Drawing.SystemColors.Control;
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = MyForm;
this.ResumeLayout(false);
}
}

71

72

7
, . , . , , ,
.
, .
,
TreeView. .
, . ,
, . .
, , .
, , , CheckBox, , .
: PictureBox
DataGridView, System.Drawing, System.Data System.Xml.
7

using
using
using
using
using

System;
System.Windows.Forms;
System.Drawing;
System.Data;
System.Xml;

class FormWithManyControls : Form


{
TreeView treeView1;
Panel panel1;
CheckBox checkBox1, checkBox2;
RadioButton radioButton1, radioButton2;
ListBox listBox1;
// -
public FormWithManyControls()
{
//
this.Text = , !;
this.Height = 800; this.Width = 900;

.NET Framework
// TreeView
treeView1 = new TreeView();
treeView1.BackColor = Color.BurlyWood;
treeView1.Dock = DockStyle.Left;
treeView1.AfterSelect +=
new System.Windows.Forms.TreeViewEventHandler(treeView1_AfterSelect);
TreeNode tn = new TreeNode();
tn.Expand();
tn.Nodes.Add(new
tn.Nodes.Add(new
tn.Nodes.Add(new
tn.Nodes.Add(new
tn.Nodes.Add(new
tn.Nodes.Add(new
tn.Nodes.Add(new
tn.Nodes.Add(new
tn.Nodes.Add(new
tn.Nodes.Add(new
tn.Nodes.Add(new
tn.Nodes.Add(new
tn.Nodes.Add(new

TreeNode([]));
TreeNode(Label));
TreeNode(Button));
TreeNode(CheckBox));
TreeNode(RadioButton));
TreeNode(ListBox));
TreeNode(TextBox));
TreeNode(TabControl));
TreeNode(DataGridView));
TreeNode(MainMenu));
TreeNode(ToolBar));
TreeNode(PictureBox));
TreeNode(RichTextBox));

treeView1.Nodes.Add(tn);
this.Controls.Add(treeView1);
//
panel1 = new Panel();
panel1.Dock = DockStyle.Right;
panel1.BorderStyle = BorderStyle.Fixed3D;
panel1.Width = this.Width treeView1.Width;
this.Controls.Add(panel1);
}
// ,
// TreeView
private void treeView1_AfterSelect
(object sender, System.Windows.Forms.TreeViewEventArgs e)
{

73

74

3
//
if (e.Node.Text == [])
{
//
panel1.Controls.Clear();
}
else if (e.Node.Text == Button)
{
//
Button button1 = new Button();
button1.Text = !;
button1.Location = new Point(300, 20);
button1.Width = 120;
button1.Height = 40;
button1.Click += new EventHandler(button1_Click);
panel1.Controls.Add(button1);
}
else if (e.Node.Text == Label)
{
//
Label label1 = new Label();
label1.Text =
. !;
label1.Location = new Point(180, 70);
label1.Width = 400;
label1.Click += new EventHandler(label1_Click);
panel1.Controls.Add(label1);
}
else if (e.Node.Text == CheckBox)
{
//
checkBox1 = new CheckBox();
checkBox1.Text = !;
checkBox1.Location = new Point(20, 40);
checkBox1.Width = 150;
checkBox1.CheckedChanged +=
new EventHandler(CheckBox_CheckedChanged);

.NET Framework
panel1.Controls.Add(checkBox1);
checkBox2 = new CheckBox();
checkBox2.Text = !;
checkBox2.Location = new Point(20, 80);
checkBox2.Width = 150;
checkBox2.CheckedChanged +=
new EventHandler(CheckBox_CheckedChanged);
panel1.Controls.Add(checkBox2);
}
else if (e.Node.Text == RadioButton)
{
//
radioButton1 = new RadioButton();
radioButton1.Text = !;
radioButton1.Location = new Point(20, 120);
radioButton1.Width = 150;
radioButton1.Height = 30;
//radioButton1.Size = new Size(20, 100);
radioButton1.CheckedChanged +=
new EventHandler(RadioButton_CheckedChanged);
panel1.Controls.Add(radioButton1);
radioButton2 = new RadioButton();
radioButton2.Text = !;
radioButton2.Location = new Point(20, 160);
radioButton2.Width = 150;
radioButton2.Height = 30;
//radioButton2.Size = new Size(20, 100);
radioButton2.CheckedChanged +=
new EventHandler(RadioButton_CheckedChanged);
panel1.Controls.Add(radioButton2);
}
else if (e.Node.Text == ListBox)
{
//
listBox1 = new ListBox();
listBox1.Items.Add();

75

76

3
listBox1.Items.Add();
listBox1.Items.Add();
listBox1.Items.Add();
listBox1.Location = new Point(20, 250);
listBox1.Width = 100; listBox1.Height = 100;
listBox1.SelectedIndexChanged +=
new EventHandler(listBox1_SelectedIndexChanged);
panel1.Controls.Add(listBox1);
}
else if (e.Node.Text == TextBox)
{
//
TextBox textBox1 = new TextBox();
textBox1.Multiline = true;
textBox1.Text = . ! +
\r\n + !;
textBox1.Location = new Point(180, 100);
textBox1.Width = 400; textBox1.Height = 40;
panel1.Controls.Add(textBox1);
}
else if (e.Node.Text == DataGridView)
{
// , xml
DataSet dataSet1 = new DataSet( DataSet);
dataSet1.ReadXml(..//..//images//marks.xml);
DataGridView dataGridView1 = new DataGridView();
dataGridView1.Width = 250;
dataGridView1.Height = 150;
dataGridView1.Location = new Point(20, 500);
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = dataSet1;
dataGridView1.DataMember = ;
//dataGridView1.DataMember = ;
//dataGridView1.ColumnCount = 2;
panel1.Controls.Add(dataGridView1);
}

.NET Framework

77

else if (e.Node.Text == TabControl)


{
//
//
TabControl tabControl1 = new TabControl();
tabControl1.Location = new Point(190, 150);
tabControl1.Size = new Size(300, 300);

TabPage tabPage1 = new TabPage();


PictureBox pictureBox1 = new PictureBox();
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = new Bitmap(..//..//images//Vadik.jpg);
pictureBox1.Size = new Size(300, 200);
tabPage1.Controls.Add(pictureBox1);
Label labelV = new Label();
labelV.Top = 200;
labelV.Size = new Size(300, 50);
labelV.Text = ! !;
tabPage1.Controls.Add(labelV);
tabControl1.TabPages.Add(tabPage1);
TabPage tabPage2 = new TabPage( );
PictureBox pictureBox2 = new PictureBox();
pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox2.Image = new Bitmap(..//..//images//comp.jpg);
pictureBox2.Size = new Size(300, 200);
tabPage2.Controls.Add(pictureBox2);
Label label = new Label();
label.Top = 200;
label.Size = new Size(300, 50);
label.Text = ! , +
!;
tabPage2.Controls.Add(label);
tabControl1.TabPages.Add(tabPage2);
TabPage tabPage3 = new TabPage();
PictureBox pictureBox3 = new PictureBox();
pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox3.Image = new Bitmap(..//..//images//terra.jpg);
pictureBox3.Size = new Size(300, 200);
tabPage3.Controls.Add(pictureBox3);

78

3
Label labelT = new Label();
labelT.Top = 200;
labelT.Size = new Size(300, 50);
labelT.Text = ! +
!;
tabPage3.Controls.Add(labelT);
tabControl1.TabPages.Add(tabPage3);
panel1.Controls.Add(tabControl1);
}
else if (e.Node.Text == PictureBox)
{
//
PictureBox pictureBox1 = new PictureBox();
pictureBox1.Image = new Bitmap(..//..//images//Zakat.jpg);
pictureBox1.BorderStyle = BorderStyle.Fixed3D;
pictureBox1.Location = new Point(500, 250);
pictureBox1.Size = new Size(250, 200);
panel1.Controls.Add(pictureBox1);
}
else if (e.Node.Text == RichTextBox)
{
//
// XML
RichTextBox richTextBox1 = new RichTextBox();
richTextBox1.LoadFile(..//..//images//marks.xml,
RichTextBoxStreamType.UnicodePlainText);
richTextBox1.WordWrap = false;
richTextBox1.BorderStyle = BorderStyle.Fixed3D;
richTextBox1.BackColor = Color.Beige;
richTextBox1.Size = new Size(250, 150);
richTextBox1.Location = new Point(300, 500);
// panel1.Height richTextBox1.Height 5);
panel1.Controls.Add(richTextBox1);
}
else if (e.Node.Text == MainMenu)
{
// ( )
MainMenu mainMenu1 = new MainMenu();

.NET Framework
MenuItem menuItem1 = new MenuItem(File);
menuItem1.MenuItems.Add(Exit,
new EventHandler(mainMenu1_Exit_Select));
mainMenu1.MenuItems.Add(menuItem1);
MenuItem menuItem2 = new MenuItem(Background);
menuItem2.MenuItems.Add(Choose,
new EventHandler(mainMenu1_ColorOwn_Select));
menuItem2.MenuItems.Add(White,
new EventHandler(mainMenu1_ColorWhite_Select));
mainMenu1.MenuItems.Add(menuItem2);
this.Menu = mainMenu1;
MessageBox.Show( +
OK.);
}
else if (e.Node.Text == ToolBar)
{
//
//
ToolBar toolBar1 = new ToolBar();
toolBar1.Size = new Size(100, 100);
toolBar1.Dock = DockStyle.Right;
ImageList imageList1 = new ImageList();
imageList1.Images.Add(new Bitmap(../../images/new.gif));
imageList1.Images.Add(new Bitmap(../../images/open.gif));
imageList1.Images.Add(new Bitmap(../../images/copy.gif));
toolBar1.ImageList = imageList1;
ToolBarButton toolBarbutton1 = new ToolBarButton(New);
toolBarbutton1.ImageIndex = 0;
toolBar1.Buttons.Add(toolBarbutton1);
ToolBarButton toolBarbutton2 = new ToolBarButton(Open);
toolBarbutton2.ImageIndex = 1;
toolBar1.Buttons.Add(toolBarbutton2);
ToolBarButton toolBarButton3 = new ToolBarButton(Copy);
toolBarButton3.ImageIndex = 2;
toolBar1.Buttons.Add(toolBarButton3);

79

80

3
toolBar1.ButtonClick +=
new ToolBarButtonClickEventHandler(toolBar1_Click);
panel1.Controls.Add(toolBar1);
}
}
/* */
// ,
void label1_Click(object sender, System.EventArgs e)
{
MessageBox.Show
(, Click.
.);
}
// ,
void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show(, - !);
}
// ,
void CheckBox_CheckedChanged(object sender, System.EventArgs e)
{
if (checkBox1.Checked && checkBox2.Checked)
{
MessageBox.Show( !);
}
else if (checkBox1.Checked)
{
MessageBox.Show( !);
}
else if (checkBox2.Checked)
{
MessageBox.Show( . !);
}
else
{
MessageBox.Show( , ?);
}
}

.NET Framework
// ,
void RadioButton_CheckedChanged(object sender, System.EventArgs e)
{
if (radioButton1.Checked)
{
MessageBox.Show( !);
}
else if (radioButton2.Checked)
{
MessageBox.Show( !);
}
}
// ,
void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
switch (listBox1.SelectedItem.ToString())
{
case (): treeView1.BackColor = Color.Green; break;
case (): treeView1.BackColor = Color.Yellow; break;
case (): treeView1.BackColor = Color.Blue; break;
case (): treeView1.BackColor = Color.Gray; break;
}
}
// , White
void mainMenu1_ColorWhite_Select(object sender, System.EventArgs e)
{
treeView1.BackColor = Color.White;
}
// ,
void mainMenu1_ColorOwn_Select(object sender, System.EventArgs e)
{
ColorDialog colorDialog1 = new ColorDialog();
colorDialog1.Color = treeView1.BackColor;
colorDialog1.ShowDialog();
treeView1.BackColor = colorDialog1.Color;
}
// , exit
void mainMenu1_Exit_Select(object sender, System.EventArgs e)
{

81

82

3
if (
MessageBox.Show( , ?,
Exit confirmation, MessageBoxButtons.YesNo)
== DialogResult.Yes
)
{
this.Dispose();
}
}
// , -

void toolBar1_Click
(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
if (e.Button.Text == Open)
{
MessageBox.Show( !);
}
else if (e.Button.Text == New)
{

.NET Framework

83

MessageBox.Show( !);
}
else if (e.Button.Text == Copy)
{
MessageBox.Show( !);
}
}
static void Main()
{
// //
Application.Run(new FormWithManyControls());
}
}

, Drawing, . .
. .
, : , , . . , .
. , , . .NET Framework .

8
, , , . PictureBox.
8

using System;
using System.Windows.Forms;
using System.Drawing;
class PictureDisplayer : Form
{
Bitmap image1;
PictureBox pictureBox1;

84

3
// -
public PictureDisplayer()
{
//
this.Text = ;
this.Size = new Size(302, 240);
//
pictureBox1 = new PictureBox();
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.BorderStyle = BorderStyle.Fixed3D;
pictureBox1.ClientSize = new Size(300, 196);
// PictureBox
image1 = new Bitmap(@../../images/Iskusstvo.jpg);
pictureBox1.Image = (Image)image1;
// PictureBox ( )
this.Controls.Add(pictureBox1);
}
static void Main()
{
//
Application.Run(new PictureDisplayer());
}

.NET Framework

85

9
flip () , :
9

using System;
using System.Windows.Forms;
using System.Drawing;
class PictureFlipper : Form
{
Button button1;
Bitmap image1;
PictureBox pictureBox1;
// -
public PictureFlipper()
{
//
this.Text = ;
this.Size = new Size(302, 240);
//
button1 = new Button();
button1.Text = ;
button1.Location = new Point(100, 150);
button1.Size = new Size(70, 40);
button1.Click += new System.EventHandler(button1_Click);
this.Controls.Add(button1);
// PictureBox
pictureBox1 = new PictureBox();
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.BorderStyle = BorderStyle.Fixed3D;
pictureBox1.ClientSize = new Size(300, 196);
// PictureBox
image1 = new Bitmap(@../../images/Giraf.jpg);

86

3
pictureBox1.Image = (Image)image1;
// PictureBox
this.Controls.Add(pictureBox1);
}
static void Main()
{
//
Application.Run(new PictureFlipper());
}
// ,
void button1_Click(object sender, EventArgs e)
{
// Flip the image along the X axis (horizontally)
image1.RotateFlip(RotateFlipType.RotateNoneFlipX);
// PictureBox
pictureBox1.Image = (Image)image1;
//
this.Text = !;
}

,
.
, ,
.

.NET Framework

87

. , - , .
1. , , ,
, .
Pen ().
, Pen, 3 :

Pen myGreenPen = new Pen(Color.Green, 3);


2. .
Brush (),
.
SolidBrush ( ) :

SolidBrush myBlueBrush = new SolidBrush(Color.Blue);


10
, DrawSomeShapes, , .
10

using System;
using System.Windows.Forms;
using System.Drawing;
class SimpleShapeMaker : Form
{
// -
public SimpleShapeMaker()
{
//
this.BackColor = Color.White;
//
Button button1 = new Button();
button1.Text = !;
button1.Location = new Point(110, 10);
button1.Size = new Size(70, 40);
button1.BackColor = Color.LightGray;
button1.Click += new System.EventHandler(button1_Click);

88

3
this.Controls.Add(button1);
}
// ,
void button1_Click(object o, System.EventArgs e)
{
//
DrawSomeShapes();
}
//
void DrawSomeShapes()
{
//
Graphics g = this.CreateGraphics();
// , 3
Pen redPen = new Pen(Color.Red, 3);
// ,
g.DrawLine(redPen, 140, 170, 140, 230);
g.DrawRectangle(redPen, 50, 60, 50, 60);
g.DrawEllipse(redPen, 150, 100, 100, 60);
//
g.Dispose();
}
static void Main()
{
//
Application.Run(new SimpleShapeMaker());
}

.NET Framework

89

11
,
. , ,
.
, . , ,
.
,
, .
: (red),
(green) (blue)
RGB. ,
RGB, 0 255 . :
red=255, green=0, blue=0 -;
red=255, green=255, blue=0 .
,
X Y ( ). ,
X=0 Y=0.
11

using System;
using System.Windows.Forms;
using System.Drawing;
class FunWithTheMouse : Form
{
// ,
PictureBox pictureBox1;
Label label1;
Point spotClicked;
// -
public FunWithTheMouse()
{
//
this.Size = new Size(640, 480);
// PictureBox
pictureBox1 = new PictureBox();

90

3
pictureBox1.Image = (Image)new Bitmap(@../../images/Dog.bmp);
pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
pictureBox1.Dock = DockStyle.Fill;
this.Controls.Add(pictureBox1);
//
label1 = new Label();
label1.BackColor = Color.Wheat;
label1.Dock = DockStyle.Bottom;
label1.Text =
. +
+
SHIFT , .;
label1.TextAlign = ContentAlignment.MiddleCenter;
this.Controls.Add(label1);
// PictureBox
this.pictureBox1.MouseDown += new MouseEventHandler(MouseButtonIsDown);
this.pictureBox1.MouseUp += new MouseEventHandler(MouseButtonIsUp);
this.pictureBox1.MouseMove += new MouseEventHandler(TheMouseMoved);
}
// ,
public void TheMouseMoved(object sender, MouseEventArgs e)
{
// SHIFT
if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
{
//
System.Drawing.Graphics g =
this.pictureBox1.CreateGraphics();
//
System.Drawing.Pen yellowPen = new
System.Drawing.Pen(Color.Yellow, 3);
// (, )
// X Y
// .
g.DrawEllipse(yellowPen, e.X, e.Y, 40, 40);
//

.NET Framework
g.Dispose();
}
}
// ,
public void MouseButtonIsDown(object sender, MouseEventArgs e)
{
// , .
// ,
spotClicked.X = e.X;//
spotClicked.Y = e.Y;//
}
// ,
public void MouseButtonIsUp(object sender, MouseEventArgs e)
{
/* ! */
// ( ),
// ,
Rectangle r = new Rectangle();
// ,
// .
// .
r.X = spotClicked.X;
r.Y = spotClicked.Y;
//
//
// ( ).
r.Width = e.X spotClicked.X;
r.Height = e.Y spotClicked.Y;
if (e.Button == MouseButtons.Left)
{
/* ,
*/

91

92

3
//
Graphics g = this.pictureBox1.CreateGraphics();
//
Pen redPen = new Pen(Color.Red, 2);
g.DrawRectangle(redPen, r);
}
else
{
// ,
// ,
ChangeLightness(r);
}
}
// ,
//
public void ChangeLightness(Rectangle rect)
{
int newRed, newGreen, newBlue;
Color pixel;
// , PictureBox
System.Drawing.Bitmap picture = new
Bitmap(this.pictureBox1.Image);
// ,
// , .
if ((rect.Width > 150) || (rect.Height > 150))
{
DialogResult result = MessageBox.Show(
! +
!,
Warning, MessageBoxButtons.OKCancel);
// Cancel ()
//

.NET Framework
if (result == DialogResult.Cancel) return;
}
/*
RGB */
// ...
for (int x = rect.X; x < rect.X + rect.Width; x++)
{
// ...
for (int y = rect.Y; y < (rect.Y + rect.Height); y++)
{
//
pixel = picture.GetPixel(x, y);
//
newRed = (int)Math.Round(pixel.R * 2.0, 0);
if (newRed > 255) newRed = 255;
newGreen = (int)Math.Round(pixel.G * 2.0, 0);
if (newGreen > 255) newGreen = 255;
newBlue = (int)Math.Round(pixel.B * 2.0, 0);
if (newBlue > 255) newBlue = 255;
//
picture.SetPixel
(x, y, Color.FromArgb(
(byte)newRed, (byte)newGreen, (byte)newBlue));
}
}
// PictureBox,
//
this.pictureBox1.Image = picture;
}

93

94

3
static void Main()
{
//
Application.Run(new FunWithTheMouse());
}

XML

95

4. XML


.
, . , .
, , , Microsoft Access. Microsoft SQL Server Express Edition, SQL Server,
. SQL Server Express Visual C#
Express, , , .
.NET Framework System.Data. , , .
, .
, .
, .
, .
: PlanetName ( ), DistanceFromSun ( )
Inhabitants ().
Planet
PlanetName

DistanceFromSun
( )

Inhabitants

57909
108200
149600
227940
778400
7208100
1423600
2867000
4488400
5909600

, , , 108 200 , , , .
,
.

96

, . , . . , SQL Server, .
Population
PlanetName

Year

Population

2008
2008
2008
2008
2008
2008
2008
2008
2008
2008
2009
2009
2009
2009
2009
2009
2009
2009
2009
2009

40000
25
6000000000
450000
8326300200
325000
1000000
7849345700
<NULL>
<NULL>
35000
3
6500000000
326800
8451780500
8700
750000
8237456000
<NULL>
<NULL>

, , , 2008 .
25 , , , , .
. , , .

SQL
: Microsoft Access,
Oracle, DB2, Microsoft SQL Server, Informix, mySQL, . C# ,
.
C# , .

XML

97

( , ADO.NET,
.)

, . SQL ( . Structured Query Language ). SQL SQL Server, Microsoft. SQL (), .

, C# , SQL. SQL .
SQL :
SELECT ( );
INSERT ( );
UPDATE ( , ).
.
, SELECT :

SELECT < >


FROM < >
WHERE < >
SELECT:

SELECT *

FROM PLANET

* . Planet .

SELECT PlanetName, Inhabitants

FROM PLANET

98

4
PlanetName Inhabitants

PLANET.
PlanetName

Inhabitants

SELECT PlanetName, InhabitantsFROM PLANET


WHERE PlanetName=Venus
PlanetName Inhabitants PLANET.
, Venus PlanetName.
PlanetName

Inhabitants

SELECT PlanetName, Population


WHERE Population<100000

FROM POPULATION

POPULATION PlanetName Population, Population 100000.


PlanetName

Population

40000
25
<NULL>
<NULL>
35000
3
8700
<NULL>
<NULL>

XML

99

, INSERT :

INSERT INTO < , >


(< , >)
VALUES (<, >)
:

INSERT INTO PLANET (PlanetName, DistanceFromSun, Inhabitants)


VALUES (, 23500000, )
INSERT PLANET . - , :
PlanetName

DistanceFromSun

Inhabitants

57909
108200
149600
227940
778400
7208100
1423600
2867000
4488400
5909600
23500000

UPDATE :

UPDATE < , >


SET < , > = < >
WHERE <>
:

UPDATE PLANET SET PlanetName= , Inhabitants= WHERE PlanetName=


, PlanetName . ,
:
PlanetName

DistanceFromSun

Inhabitants

57909
108200
149600
227940
778400
7208100

100

1423600
2867000
4488400
5909600
23500000


, , PLANET POPULATION : PlanetName, . .
, , :
PLANET
PlanetName

DistanceFromSun

Inhabitants

108200

POPULATION
PlanetName

Year

Population

2008
2009

25
3

SELECT *
FROM PLANET INNER JOIN POPULATION ON PLANET.PlanetName=POPULATION.planetName
WHERE PlanetName=
PLANETS_AND_POPULATION
PlanetName
DistanceFromSun

Inhabitants

Year

Population

2008
2009

25
3

108200
108200


. . ,
. , , Microsoft SQL Server
SqlConnection, Access OleDbConnection.
SQLDataProvider, OLEDbProvider.
, .
, , Planets Microsoft SQL Server Access.
Access 2007 , ,

XML

101

Access 2007 Access 2003. ,


.
Planets databases.
Planets.mdf Planets.ldf, SQL Server, Planets.accdb Access 2007, Planets.mdb Access 2003.
, databases , , , Examples.sln.
SQL Server ,
Microsoft SQL Server Microsoft SQL Server Express Edition.
:
1. Microsoft SQL Server Express Edition;
2. Microsoft SQL Server;
3. Access 2003;
4. Access 2007.
, , Microsoft SQL Server. , , , .
Microsoft Office, , Access, , .
,
SQL Server Express. SQL Server , , .
: http://www.microsoft.com/rus/express/sql/download.

, C#
C# Connection, Command,
DataReader, DataAdapter. Sql OleDb , Planets .

12
SQL-. , ListBox .
12

using System.Windows.Forms;
using System.Data;

102

// SQL Server
using System.Data.SqlClient;
using System.Drawing;
// Access
using System.Data.OleDb;
class SimpleDataAccess : Form
{
public SimpleDataAccess()
{
//
this.Text = . .;
//
Label labelCaption = new Label();
labelCaption.Text = !;
labelCaption.Location = new Point(30, 10);
labelCaption.Width = 200;
labelCaption.Parent = this;
ListBox listPlanets = new ListBox();
listPlanets.Location = new Point(30, 50);
listPlanets.Width = 100;
listPlanets.Parent = this;
//
//
string sql = SELECT * FROM PLANET;
string connectionString;
/*
// 1
// SQL Server Express Edition
// PLANETS
string dbLocation =
(../../../databases/planets.mdf);
connectionString = @data source=.\SQLEXPRESS; +
User Instance=true;Integrated Security=SSPI; +
AttachDBFilename= + dbLocation;
SqlConnection connection1 = new SqlConnection(connectionString);
*/
// 2
// SQL Server 2005

XML
connectionString =
data source = localhost; Initial Catalog = Planets; +
Integrated Security = SSPI;
SqlConnection connection1 = new SqlConnection(connectionString);
//
connection1.Open();
SqlCommand command1 = new SqlCommand(sql, connection1);
SqlDataReader dataReader1 = command1.ExecuteReader();
//
//
while (dataReader1.Read())
{
listPlanets.Items.Add(dataReader1[PlanetName]);
}
//
dataReader1.Close();
connection1.Close();
/*
// 3. Access 2003 *.mdb
connectionString = Provider=Microsoft.Jet.OLEDB.4.0; +
@Data Source= ../../../databases/planets.mdb;

// 4. Access 2007 *.accdb


connectionString = Provider=Microsoft.Ace.OLEDB.12.0; +
@Data Source= ../../../databases/planets.accdb;

OleDbConnection connection = new OleDbConnection(connectionString);


connection.Open();
OleDbCommand command = new OleDbCommand(sql, connection);
OleDbDataReader dataReader = command.ExecuteReader();
//
//
while (dataReader.Read())
{
listPlanets.Items.Add(dataReader[PlanetName]);
}

103

104

4
//
dataReader.Close();
connection.Close();
* */

}
static void Main()
{
//
Application.Run(new SimpleDataAccess());
}
}

13
.
DataGridView.
, ,
DataSet, DataGridView, .
,
.
13

using System.Windows.Forms;
using System.Data;
// SQL Server

XML
using System.Data.SqlClient;
using System.Drawing;
// Access
using System.Data.OleDb;
class DataInGrid : Form
{
public DataInGrid()
{
//
this.Width = 450;
this.Height = 400;
//
this.Text = : +
Grid.;
//
Label labelCaption = new Label();
labelCaption.Text = !;
labelCaption.Location = new Point(60, 10);
labelCaption.Width = 200;
labelCaption.Parent = this;
// DataGridView
DataGridView dataGridView1 = new DataGridView();
dataGridView1.Width = 350;
dataGridView1.Height = 250;
dataGridView1.Location = new Point(20, 50);
dataGridView1.DataMember = Table;
dataGridView1.AutoResizeColumns();
this.Controls.Add(dataGridView1);

//
//
string sql = SELECT * FROM PLANET;
string connectionString;
// DataSet
// DataTable
DataSet dataSet1 = new DataSet();
/*
// 1
// SQL Server Express Edition

105

106

4
// PLANETS
string dbLocation =
(../../../databases/planets.mdf);
connectionString = @data source=.\SQLEXPRESS; +
User Instance=true;Integrated Security=SSPI; +
AttachDBFilename= + dbLocation;
SqlConnection connection1 = new SqlConnection(connectionString);
*/
/*
// 2
// SQL Server 2005
connectionString =
data source = localhost; Initial Catalog = Planets; +
Integrated Security = SSPI;
SqlConnection connection1 = new SqlConnection(connectionString);
//
connection1.Open();
// DataAdapter DataSet
SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter();
// DataAdapter,
//
sqlDataAdapter1.SelectCommand =
new SqlCommand(sql, connection1);
// DataSet
sqlDataAdapter1.Fill(dataSet1);
// DataGridView
DataGridView1.DataSource = dataSet1;
//
connection1.Close();
* */
/*
// 3. Access 2003 *.mdb
connectionString = Provider=Microsoft.Jet.OLEDB.4.0; +
@Data Source= ../../../databases/planets.mdb;
*/

XML
// 4. Access 2007 *.accdb
connectionString = Provider=Microsoft.Ace.OLEDB.12.0; +
@Data Source= ../../../databases/planets.accdb;

OleDbConnection connection = new OleDbConnection(connectionString);


connection.Open();
OleDbDataAdapter dataAdapter = new OleDbDataAdapter();
dataAdapter.SelectCommand = new OleDbCommand(sql, connection);
dataAdapter.Fill(dataSet1);
dataGridView1.DataSource = dataSet1;
//
connection.Close();
}
static void Main()
{
//
Application.Run(new DataInGrid());
}
}

107

108

14
DataGridView, , ,
, . , , DataGridView,
, .
DataAdapter Select, , Insert, Update, Delete, .
( ) UPDATE INSERT. System.Data , CommandBuilder, SQL .
14

using System;
using System.Windows.Forms;
using System.Data;
// SQL Server
using System.Data.SqlClient;
using System.Drawing;
using System.Diagnostics;
class DataInOutGrid: Form
{
SqlDataAdapter dataAdapter;
DataGridView dataGridView;
public DataInOutGrid()
{
//
this.Width = 450;
this.Height = 400;
//
this.Text = : +
Grid.;
//
//,
Label labelCaption = new Label();
labelCaption.Text = !;
labelCaption.Location = new Point(60, 10);
labelCaption.Width = 200;
labelCaption.Parent = this;

XML
// DataGridView
dataGridView = new DataGridView();
dataGridView.Width = 350;
dataGridView.Height = 250;
dataGridView.Location = new Point(20, 50);
dataGridView.AutoResizeColumns();
this.Controls.Add(dataGridView);
//
Button buttonSave = new Button();
buttonSave.Location = new Point(100, 320);
buttonSave.Width = 220;
buttonSave.Text = !;
buttonSave.Click +=
new System.EventHandler(ButtonSave_Click);
buttonSave.Parent = this;
//
//
string sql = SELECT * FROM PLANET;
string connectionString;
// DataTable
DataTable dataTable = new DataTable();
/*
// 1
// SQL Server Express Edition
// PLANETS
string dbLocation =
(../../../databases/planets.mdf);
connectionString = @data source=.\SQLEXPRESS; +
User Instance=true;Integrated Security=SSPI; +
AttachDBFilename= + dbLocation;
SqlConnection connection1 = new SqlConnection(connectionString);
*/
// 2
// SQL Server 2005
connectionString =
data source = localhost; Initial Catalog = Planets; +
Integrated Security = SSPI;
SqlConnection connection = new SqlConnection(connectionString);

109

110

4
//
connection.Open();
//
SqlCommand sqlCommand = new SqlCommand(sql, connection);
//
// DataAdapter DataSet
dataAdapter = new SqlDataAdapter(sqlCommand);
//
// Update
SqlCommandBuilder commandBuilder =
new SqlCommandBuilder(dataAdapter);
// DataTable
dataAdapter.Fill(dataTable);
// DataGridView
dataGridView.DataSource = dataTable;
//
connection.Close();

static void Main()


{
//
Application.Run(new DataInOutGrid());
}
void ButtonSave_Click(object sender, System.EventArgs args)
{
try
{
dataAdapter.Update((DataTable)dataGridView.DataSource);
MessageBox.Show( !,
, MessageBoxButtons.OK);
}
catch(Exception)
{
MessageBox.Show( !,
, MessageBoxButtons.OK);
}
}
}

XML

111

, (
).

, . . , ,
. Access, ,
Example 12 Example 13.

XML-
System.Xml XML- . :
XML-;
XML- ;
XML- .

XML
XML ( . eXtensible Markup Language ) . , XML, . XML , . , XML
.
XML , , , . , . , , ,
XML- .

112

, XML- :

<?xml version=1.0 encoding=utf-8 ?>

<>

<>

< =-->

</>
< =->

</>
</>
<>

< =>

</>
< =>

</>
</>
</>

XML-
<?xml version=1.0 encoding=utf-8 ?>. ,
,
XML

(<>) .
, .

,

,

XML HTML, HTML , . XML


, .
,
XML.

XML- - , . , (<>) (</>).


, .

XML

113

. ,
.
XML- <> . <>, .

<>
<>

</>
</>

. . = . ,
<> , .
<> . -.

<

</>

>

, ,
. , : ; < .
> ... .
. . , <> :

< = = - />
, .

15
XML- .
System.Xml:
XmlDocument. Xml-. ,
.

114

XmlNodeList.
XML-, , , .
XmlNode. XML-.
XML-
xPath. , XML. xPath // :
, .
15

using System;
using System.Windows.Forms;
using System.Drawing;
// XML-
using System.Xml;
// xPath
using System.Xml.XPath;
class XmlRetriever: Form
{
ComboBox comboBox1;
Button button1;
ListBox listBox1;
RichTextBox richTextBox1;
XmlDocument xmlDoc;
// -
public XmlRetriever()
{
//
this.Text = XML-;
this.Size = new Size(400, 400);
// XmlDocument, xml-
xmlDoc = new XmlDocument();
xmlDoc.Load(../../docs/Planets.xml);
// TextBox
richTextBox1 = new RichTextBox();
richTextBox1.Dock = DockStyle.Top;
richTextBox1.AcceptsTab = true;
richTextBox1.Height = 180;
richTextBox1.ReadOnly = true;
richTextBox1.BackColor = Color.Silver;

XML
// XML- TextBox
richTextBox1.Text = xmlDoc.OuterXml;
this.Controls.Add(richTextBox1);
// ComboBox
//
// XPath,
// XML-
comboBox1 = new ComboBox();
comboBox1.Location = new Point(0, 200);
comboBox1.Width = 300;
comboBox1.Items.Add(/);
comboBox1.Items.Add(//);
comboBox1.Items.Add(///);
comboBox1.Items.Add(
///[@=--]);
comboBox1.SelectedIndex = 0;
this.Controls.Add(comboBox1);
//
// Xml-
button1 = new Button();
button1.Text = ;
button1.Location = new Point(100, 230);
button1.Width = 120;
button1.Click += new EventHandler(Button1_Click);
this.Controls.Add(button1);
// ListBox
listBox1 = new ListBox();
listBox1.Dock = DockStyle.Bottom;
listBox1.Location = new Point(10, 10);
this.Controls.Add(listBox1);
}
static void Main()
{
//
Application.Run(new XmlRetriever());
}
// ,
void Button1_Click(object sender, EventArgs e)
{

115

116

4
XmlNodeList xmlNodes;
XmlNode xmlElement;
string elementValue;
// try-catch,
// XPath
// ,
//
try
{
// XML- ,
// XPath, ComboBox
xmlNodes = xmlDoc.SelectNodes(comboBox1.Text);
// ,
// ListBox
listBox1.Items.Clear();
for (int i = 0; i < xmlNodes.Count; i++)
{
xmlElement = xmlNodes[i];
if (xmlElement.HasChildNodes)
{
elementValue = xmlElement.FirstChild.Value.Trim();
listBox1.Items.Add(elementValue);
}
}
}
catch (XPathException ex)
{
const string errorMessage =
XPath! +
\r\n + ! +
\r\n + !;
MessageBox.Show(errorMessage +\r\n + ex.Message);
}

}
}
XML- RichTextBox.
ComboBox xPath
. ListBox Xml-,
XPath. XPath
ComboBox.
xPath, .

XML

117

118

5.

, . ? .
, .
, .

C#.
, .

119


, ,
. Windows
Application, . , Program . , . ,
, , .
.
, , . , Location, Height Width. ,
. ,
, ; , , . , , .
, , , , . , .
Windows-. , , , .

120

.
1. Form1.cs . .
2. Form1.Design.cs , .
.
3. Form1.Designer.cs , ,
, , Visual C# Express
. ,
, .
.
4. Program.cs. Main() Form. Main .
, . Windows Main . .
,
. ,
: .
.
: ,
, Forms1.cs,
.
, Program.cs Form1.cs .


, , ,
. ,
:
1. , Visual C# Express ?
, . ,
.
Visual C# Express, .
. , ,
, ,
.
. .

121

, . , , .
2. , .NET Framework Class Library?
3. ?
4. ?
Visual C# Express .NET Framework Class Library. ,
, .NET Framework SDK. ,
System.Windows.Forms. Class library ( ) ,
,
System.Windows.Forms.

122

5
Systems.Windows.Forms ( , , . .). (, )
(. ). , , .

. , , .
,
.
5. ?
- , . , - , Visual Basic .

123

-:
http://www.microsoft.com/rus/express/vcsharp.
http://blogs.msdn.com/rucoding4fun/
http://msdn.microsoft.com/beginner
http://social.msdn.microsoft.com/Forums/ru-RU/beginnerru/threads
http://www.dreamspark.ru
http://www.techdays.ru
http://www.gotdotnet.ru
http://msdn.microsoft.com/ru-ru/vcsharp/aa336717.aspx (EN)
http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=160&SiteID=1 (EN)
http://www.codeproject.com/?cat=3 (EN)
http://intuit.ru/software/school/cs2kids/

. , - .
!

124

1.



Microsoft Visual C# Express Edition
() Visual C# Express
,
C#

2.

C#


String ()

Boolean ( )


,

VOID?

?

5
5
6
7
9
10

11
11
16
19
22
24
25
26
26
27
29
31
33
34
35
45
47
48
52
52
53
54
55
55
57
57

125
3. .NET Framework
.NET Framework?


Windows Forms
System.Windows.Forms
Windows Forms Visual C# Express

59
59
60
61
64
65
66
83

4. XML

95


SQL


, C#
XML-
XML

15

95
96
100
100
101
111
111
112
113
113

5.

118
119
120

C#

.
.
.
JetStyle
13.10.2009. . 70x90 1/16.
. . . . 8. .
2000 .
.
- , www.intuit.ru
, ., 8, .3.
E-mail: admin@intuit.ru, http://www.intuit.ru

.
, , . 3
: (499) 157-1902, (499) 157-5272
E-mail: Binom@lbz.ru, http://www.Lbz.ru

ISBN 978-5-9963-0236-9