Академический Документы
Профессиональный Документы
Культура Документы
Дрейер М. - C# для школьников (лицей информационных технологий) - 2010 PDF
Дрейер М. - C# для школьников (лицей информационных технологий) - 2010 PDF
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 .: ., . ( ).
( 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
-
, 2010
ISBN 978-5-9963-0236-9 (.) . , 2010
3
, , 7 . -
4 . -
? 7 ? -
, : , ,
.
,
, . , .
, .
, , , , ,
, , - -
. : ,
, If then,
, 12 , -
. .
, , , -
, , .
, .
, .
. , -
. , .
, , . -
. , ,
, , .
, -
- , , -
. , -
.
C#, Visual Studio .Net, SQL Server -
, Microsoft.
-
.
. , , .
, ,
, . -
, .
, , , , ,
XML-, -
, , -
, .
:
4
( 1216 ) --
, -
Windows.
Microsoft C# ( -)
. -
, .
(Martin Dreyer) , , -
. -
-
.
5
1.
, , , :
! ! .
, !
, ,
. ? : -
, ? ? , .
, , : -
, !
? :
, , , -
, .
: ,
. - ,
. - : ,
. , - . , -
, , .
, , ,
. , ,
. ,
, .
6 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,
-
,
.
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.
7
() 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).
8 1
, !.
X
.
, .
, C#
. -
(;)
:
,
, , -
. , .
9
,
( -
, ) , .
. -
(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 1
C#
II . -
. .
:
C# class Animal
. {
: }
, Visual C# Express, -
, , , -
.
, -
.
, -
C#.
11
2.
, -
. , ,
.
: .
: ? !. -
, , , -
, , .
, : , :
, , .
.
, , -
. :
, (, ),
. ,
.
, , -
. , -
, -
, .
, ,
, .
12 2
, ,
.
, - ,
. -
, , ?, , ,
.
.
, . , ?
, ,
-. , , -
13
. , -
. .
, : , -
, , !
. -
. . .
! ,
. - . , -
. ! - , , -
, , .
14 2
- . , :
, -
. - . -
, , -
, .
, , , , -
. :
! 12 ( !) -
. , -
, . , -
, .
, : , , -
, . .
, -
, , .
, . -
, , (
, , ). .
, ! , . ( , -
.)
. -, . -, ,
, . -, ,
.
15
, , , ...
. . .
, .
: , -
.
. ( ), -
, ?
: , , , . -
, : , -
-, .
, : :
- .
.
, ,
( ).
,
, .
.
, , -
, , -
, . -
: , ?. : , ...
, -.
16 2
, .
:
name (, , 12);
earShape (, );
likeToWatchTV (, ,
).
, . -
, ,
.
.
. ,
: , , , -
. , , -
.
, . -
, -
, .
C#
, , , ( -
) . ,
, .
, . (-
, - )
, C# .
, C# ,
, , . .
, , -
:
C# ;
C# .
C# , -
- .
: . ,
.
,
.
: , -
, . , -
. ,
.
17
. , -
.
, , , ,
. , ,
, .
, -
. . , -
, , . .
, . -
. , ,
, .
, , -
. , , -
, . -
, , -
. ,
, , , -
, .
. ,
. , -
, , -
.
, , ,
. , ,
, , -
.
- -
.
C#.
18 2
. .
, . -
. -
: .
, ,
.
: -
.
, ,
.
C# :
class Animal
{
}
:
;
Animal;
{ }.
Animal , -
. -
, , MyPhotoProgram MyGradeCalculator. -
, , .
.
19
:
C# class Animal
. {
class, }
,
, ,
.
, ( -
). .
, .
Animal :
, ;
, ;
;
.
, . , - -
. , , -
, .
:
Animal Barsik;
20 2
, , -
. : -
Animal Barsik. , Barsik,
, .
:
, , -
. :
21
(Animal Barsik) , -
Animal. -
.
. , , -
. ,
, -
Animal Barsik.
(Barsik = new Animal();) : -
Barsik? .
, . -
Animal , Barsik, .
Animal, - ,
, .
, .
, , 2 3
Animal.
// . - fiddlestick , fiddlesticks ,
. , , , .
:
, class Animal
. {
, }
.
, Animal percyThePorcupine;
, //
,
.
, .
.
.
percyThePorcupine =
, new Animal();
.
percyThePorcupine
Animal, ,
22 2
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 2
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 2
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 , Petr ()
Eight-legged Octopus (
, )
, , .. Girls are too clever to fall out of their
prams ( ,
)
int , 328
. 28000
. -520
integer
bool , True
False
(true), (false).
boolean.
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 2
. -
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 2
-;
;
.
, . , -
, , , , . ,
, -
, .
, -
,.
, , -
, , , -
, -
.
33
C# . -
.
. , .
:
void SayHello()
{
}
, .
- .
void SayHello()
{
Console.WriteLine(Hello);
}
( TAB .)
, Hello.
, .
VOID?
void , -
, . .
: -
. Void , .
34 2
void , , -
, , , -
, . -
SayHello , Hello
. .
, , -
. .
, , -
, , .
, , ,
, .
, . -
:
SayHello();
, , ,
,
SayHello. , -
, .
, , .
:
class Person
. . {
, //
public string firstName;
, . public string lastName;
, (//), //
. public void ShowFullName()
, . {
Console.WriteLine(Name is
+ . + firstName + +
, . lastName);
}
. }
, -
. , -
, .
, , :
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
:
WriteHello(Jo);
WriteHello(Sam);
WriteHello(You);
, , .
, -
.
// . , .
.
void WriteHello(string x)
{
Console.WriteLine(Hello + x);
}
: someName someBodysName
.
, , , -
:
WriteHello(Petr, Ivanov);
, ,
: 5 .
38 2
? , , , -
. .
, -
. . - , -
, , , , -
, .
:
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 i;
// i .
i = NumberOfLegs();
// i = 2, , NumberOfLegs
Console.WriteLine( + i);
i = NumberOfLegs();
// i = 0. , !
Console.WriteLine( + i);
: 2,
0. , , .
:
, ,
(void), , . -
Integer, int .
, . -
, void .
: , , return . -
, .
:
class Person
{
//
string firstName;
string lastName;
. //
void, , 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 .
-
. ,
. , -
. .
. :
(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.
, :
-, .
- , -.
, -
.
.
:
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;
}
}
, .
, :
p.lastName Rocket.
:
p.lastName Ivanov.
this , , : -
, --
.
// . this
. , -
this. . ,
f, firstName, -
, this , -
, , , .
, . -
, ( ,
). : , .
, :
, ;
;
;
.
, , ,
. ( ?) ,
46 2
- , ,
.
, , , -
, . ,
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;
F5 ( -
). , , .
, .
. ,
, .
, .
, . -
-
, :
using System;
using System.Windows.Forms;
// . :
. . ,
, -
.
, -
. .
: -
, -
.
, ? , -
, , -
49
:
.
, -
,. , -
.
-
. .
using System;
using System.Windows.Forms;
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;
. -
, ,
MouseEventArgs, , , .
, . -
: , -
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;
}
-
, :
//
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 2
, , , , .. .
, ? ,
. , , .
, :
, 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 --
:
Point Microsoft :
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
{
...
}
, ,
.
, , .
, .
, . ,
,
.
-
, - .
56 2
. 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 .
, :
: Bird, -
Animal. , Bird -
Animal.
Bird ,
Bird, Animal, ,
:
// . , -
. ,
: Bird, (protected)
isMammal , Bird Animal; -
, .
: ,
. ( Bird)
( Animal). Bird
Animal, Car, , Animal,
.
Animal Bird,
, .
, .
, , ,
, , , -
. , ,
.
System.Windows.Forms.Form.
: , -
Form.
!
, , , -
, .
58 2
:
public class Musician
{
. public string name;
public int age;
public bool canSing;
}
3. .NET Framework
.NET Framework?
C# .NET Framework -
. , .
.NET Framework? , -
Microsoft, C# .. .NET Framework -
.
1. -, ,
, C#.
.
2. -, , -
(
).
, C# ( .NET)
, .
.NET framework , -
, . ,
, , .
, , ,
60 3
. , ,
.
: , -
, , -
. , ,
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 3
, -
, . ,
.
. -
,
.
-
, , , .
. -
, , -
, :
. -
, ? .NET
.
.
.
, - , , ,
, , -
.
:
1. Console.ReadLine , (
).
2. Console.WriteLine ( )
.
3. Console.Write .
.NET Framework 63
1
! -
.
using System;
class PleaseSayUra
{
static void Main()
{
//
Console.Write(!);
//
Console.ReadLine();
}
}
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 3
Windows Forms Visual C# Express
, , -
. , C# Express
( Form1.cs Program.cs) -
C#, , . -
:
Form1.cs.
Program.cs , .
,
, -
.
Windows Forms.
.
using System.Windows.Forms;
,
.
using System.Windows.Forms;
// -
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);
}
, . .
, -
. . -
( ) -
, , , . ,
, .
using System;
using System.Windows.Forms;
using System.Drawing;
// ,
void mrButton_Click(object sender, EventArgs e)
{
//
mrButton.Text = !;
}
}
: .
, -
. 4- : Button, ListBox, MessageBox
PictureBox.
70 3
: System.Windows.Forms -
System.Drawing. , PictureBox,
Drawing.
using System.Windows.Forms;
using System.Drawing;
// -
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 71
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);
}
, . -
, . -
, , ,
.
, .
,
TreeView. .
, -
. ,
, . . -
, , -
.
, -
, , CheckBox, , -
.
: PictureBox
DataGridView, System.Drawing, System.Data System.Xml.
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
using System.Xml;
// -
public FormWithManyControls()
{
//
this.Text = , !;
this.Height = 800; this.Width = 900;
.NET Framework 73
// TreeView
tn.Nodes.Add(new TreeNode([]));
tn.Nodes.Add(new TreeNode(Label));
tn.Nodes.Add(new TreeNode(Button));
tn.Nodes.Add(new TreeNode(CheckBox));
tn.Nodes.Add(new TreeNode(RadioButton));
tn.Nodes.Add(new TreeNode(ListBox));
tn.Nodes.Add(new TreeNode(TextBox));
tn.Nodes.Add(new TreeNode(TabControl));
tn.Nodes.Add(new TreeNode(DataGridView));
tn.Nodes.Add(new TreeNode(MainMenu));
tn.Nodes.Add(new TreeNode(ToolBar));
tn.Nodes.Add(new TreeNode(PictureBox));
tn.Nodes.Add(new TreeNode(RichTextBox));
treeView1.Nodes.Add(tn);
this.Controls.Add(treeView1);
//
this.Controls.Add(panel1);
}
// ,
// TreeView
private void treeView1_AfterSelect
(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
74 3
//
if (e.Node.Text == [])
{
//
panel1.Controls.Clear();
}
else if (e.Node.Text == Button)
{
//
panel1.Controls.Add(button1);
}
else if (e.Node.Text == Label)
{
//
panel1.Controls.Add(label1);
}
else if (e.Node.Text == CheckBox)
{
//
radioButton1.CheckedChanged +=
new EventHandler(RadioButton_CheckedChanged);
panel1.Controls.Add(radioButton1);
radioButton2.CheckedChanged +=
new EventHandler(RadioButton_CheckedChanged);
panel1.Controls.Add(radioButton2);
}
else if (e.Node.Text == ListBox)
{
//
panel1.Controls.Add(listBox1);
}
else if (e.Node.Text == TextBox)
{
//
panel1.Controls.Add(textBox1);
}
else if (e.Node.Text == DataGridView)
{
// , xml
//dataGridView1.ColumnCount = 2;
panel1.Controls.Add(dataGridView1);
}
.NET Framework 77
else if (e.Node.Text == TabControl)
{
//
//
panel1.Controls.Add(tabControl1);
}
else if (e.Node.Text == PictureBox)
{
//
panel1.Controls.Add(pictureBox1);
}
else if (e.Node.Text == RichTextBox)
{
//
// XML
panel1.Controls.Add(richTextBox1);
}
else if (e.Node.Text == MainMenu)
{
// ( )
MainMenu mainMenu1 = new MainMenu();
.NET Framework 79
MenuItem menuItem1 = new MenuItem(File);
menuItem1.MenuItems.Add(Exit,
new EventHandler(mainMenu1_Exit_Select));
mainMenu1.MenuItems.Add(menuItem1);
this.Menu = mainMenu1;
MessageBox.Show( +
OK.);
}
else if (e.Node.Text == ToolBar)
{
//
//
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 81
// ,
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)
{
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( !);
}
}
, Drawing, -
. .
. .
, : , -
, . . , -
.
. -
, , . -
.NET Framework .
, , , -
. PictureBox.
using System;
using System.Windows.Forms;
using System.Drawing;
this.Text = ;
this.Size = new Size(302, 240);
//
// PictureBox
// PictureBox ( )
this.Controls.Add(pictureBox1);
flip (-
) , :
using System;
using System.Windows.Forms;
using System.Drawing;
// -
public PictureFlipper()
{
//
this.Text = ;
this.Size = new Size(302, 240);
//
// PictureBox
// PictureBox
// PictureBox
this.Controls.Add(pictureBox1);
// ,
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 :
2. .
Brush (),
.
SolidBrush ( ) :
10
, DrawSomeShapes, , .
10
using System;
using System.Windows.Forms;
using System.Drawing;
this.BackColor = Color.White;
//
// ,
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();
}
,
. , ,
.
, -
. , ,
.
,
, .
: (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;
PictureBox pictureBox1;
Label label1;
Point spotClicked;
// -
public FunWithTheMouse()
{
//
// PictureBox
//
// PictureBox
// ,
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 91
g.Dispose();
}
}
// ,
public void MouseButtonIsDown(object sender, MouseEventArgs e)
{
// , .
// ,
spotClicked.X = e.X;//
spotClicked.Y = e.Y;//
}
// ,
public void MouseButtonIsUp(object sender, MouseEventArgs e)
{
/* ! */
// ( ),
// ,
// ,
// .
// .
r.X = spotClicked.X;
r.Y = spotClicked.Y;
//
//
// ( ).
if (e.Button == MouseButtons.Left)
{
/* ,
*/
92 3
//
Graphics g = this.pictureBox1.CreateGraphics();
//
ChangeLightness(r);
}
}
// ,
//
public void ChangeLightness(Rectangle rect)
{
int newRed, newGreen, newBlue;
Color pixel;
// , PictureBox
// ,
// , .
// Cancel ()
//
.NET Framework 93
if (result == DialogResult.Cancel) return;
}
/*
RGB */
// ...
//
//
picture.SetPixel
(x, y, Color.FromArgb(
(byte)newRed, (byte)newGreen, (byte)newBlue));
}
}
// PictureBox,
//
this.pictureBox1.Image = picture;
}
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
, , , 108 200 -
, , , .
,
.
96 4
, . -
, . -
. , -
SQL Server, .
Population
, , , 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:
* . -
Planet .
PlanetName Inhabitants
PlanetName Inhabitants
PlanetName Population
40000
25
<NULL>
<NULL>
35000
3
8700
<NULL>
<NULL>
XML 99
, INSERT :
:
INSERT INTO PLANET (PlanetName, DistanceFromSun, Inhabitants)
VALUES (, 23500000, )
INSERT PLANET . --
, :
UPDATE :
UPDATE < , >
SET < , > = < >
WHERE <>
:
UPDATE PLANET SET PlanetName= , Inhabitants= WHERE PlanetName=
, PlanetName -
. ,
:
, , PLANET POP-
ULATION : PlanetName, -
. -
.
, , :
PLANET POPULATION
PlanetName DistanceFromSun Inhabitants PlanetName Year Population
108200 2008 25
2009 3
SELECT *
FROM PLANET INNER JOIN POPULATION ON PLANET.PlanetName=POPULATION.planetName
WHERE PlanetName=
PLANETS_AND_POPULATION
PlanetName DistanceFromSun Inhabitants Year Population
108200 2008 25
108200 2009 3
. -
. -
,
. , , 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 4
// 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;
//
//
string sql = SELECT * FROM PLANET;
string connectionString;
/*
// 1
// SQL Server Express Edition
// PLANETS
string dbLocation =
(../../../databases/planets.mdf);
// 2
// SQL Server 2005
XML 103
connectionString =
data source = localhost; Initial Catalog = Planets; +
Integrated Security = SSPI;
SqlConnection connection1 = new SqlConnection(connectionString);
//
connection1.Open();
//
dataReader1.Close();
connection1.Close();
/*
//
//
while (dataReader.Read())
{
listPlanets.Items.Add(dataReader[PlanetName]);
}
104 4
//
dataReader.Close();
connection.Close();
* */
}
13
.
DataGridView.
, ,
DataSet, DataGridView, -
.
,
.
13
using System.Windows.Forms;
using System.Data;
// SQL Server
XML 105
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
//
//
string sql = SELECT * FROM PLANET;
string connectionString;
// DataSet
// DataTable
DataSet dataSet1 = new DataSet();
/*
// 1
// SQL Server Express Edition
106 4
// PLANETS
string dbLocation =
(../../../databases/planets.mdf);
//
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 107
// 4. Access 2007 *.accdb
connectionString = Provider=Microsoft.Ace.OLEDB.12.0; +
@Data Source= ../../../databases/planets.accdb;
dataAdapter.Fill(dataSet1);
dataGridView1.DataSource = dataSet1;
//
connection.Close();
}
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 109
// 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);
// 2
// SQL Server 2005
connectionString =
data source = localhost; Initial Catalog = Planets; +
Integrated Security = SSPI;
SqlConnection connection = new SqlConnection(connectionString);
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();
}
, . -
. , ,
. Access, ,
Example 12 Example 13.
XML-
System.Xml XML- -
. :
XML-;
XML- ;
XML- .
XML
<> ,
< =>
</>
< =>
</>
</>
</>
XML- - , . -
, (<>) -
(</>).
, -
.
XML 113
. ,
.
XML- <> -
. <>, -
.
<> </>
<> </>
. -
. = . ,
<> , .
<> . -
-.
< = - > </>
, ,
. , -
: ; < .
> ... .
. -
. , <> -
:
< = = - />
, .
15
XML- .
System.Xml:
XmlDocument. Xml-. -
,
.
114 4
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 115
// 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);
}
// ,
void Button1_Click(object sender, EventArgs e)
{
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
5.
, -
. ? .
, .
, .
C#.
, -
.
119
, ,
. Windows
Application, . -
, Program -
. -
, . ,
, , .
-
.
, , -
. , -
Location, Height Width. ,
. -
,
, ; -
, , . -
, , .
, , -
, , . -
, .
Windows--
. , , -
, .
120 5
.
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. 5
5
5
Microsoft Visual C# Express Edition 6
() Visual C# Express 7
, 9
C# 10
2. 11
11
C# 16
19
22
String () 24
25
Boolean ( ) 26
26
27
, 29
31
VOID? 33
34
? 35
45
. 47
48
52
52
53
54
55
55
57
57
125
3. .NET Framework 59
.NET Framework? 59
60
61
Windows Forms 64
System.Windows.Forms 65
Windows Forms Visual C# Express 66
83
4. XML 95
95
SQL 96
100
100
, C# 101
XML- 111
XML 111
112
113
15 113
5. 118
119
120
C#
.
.
.
JetStyle
.
- , 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