lab 7 : c# # $ %& '#()theory.cpe.ku.ac.th/~jittat/204111/labs/lab7-c-sharp-intro.pdf ·...

15
Lab 7 : C# Lab 7 : ก C# กกSharp Sharp Deve Sharp C# ก Solu 1. Cons 1. ก Cons 2 กก Solutio (1) 01204111 : C# C# Py กก Develop (http://www.icsharpcode.net/opensource/sd elop p Develop 1 1 Sharp Develop ution กก sole 2. W Console ก sole File > New > Solution New Projec 1. Categories กก C# Wi 2. Templates ก Console Applicatio 3. (Name) ก Solution 4. ก Create .................................................................... on Computer & Programming ython กก d/ ) Windows application t 2 indows Applications on mech ......................... ...............

Upload: dangxuyen

Post on 07-Feb-2018

228 views

Category:

Documents


8 download

TRANSCRIPT

Page 1: Lab 7 : C# # $ %& '#()theory.cpe.ku.ac.th/~jittat/204111/labs/lab7-c-sharp-intro.pdf · Lab 7 : C# Lab 7 : ก ˘ˇˆก˙ ˝ ˙ ก ˛˚ ˜ ก ˚˝ ˙ C# # ... Windows application

Lab 7 : ������ � C# �������������

Lab 7 : ������ � ��������ก��� ����������������ก������ ก������������ ก������ C# ������#� $�% � �����ก&�ก��� '&����(�)�*+��*,���*-�����'-�ก��ก�.���ก���/ -���� ก��� ���� ก' Sharp Develop

����ก�� Sharp Develop ��0����������.���� ก�� Sharp Develop

������ C# ���� -ก+'� Solution

1. �� Console

��'�ก��A#��.�A'������+��ก��

��/����� �ก� �����������$�B����� ก�� ��

1. ����ก����� Console ����������&������� ก����-��0�ก���#

�#�� � 2 ก����0�ก������/�� Solution������������� (1) 01204111 : Computer

������ � C# �#$��%�&'#()���������ก��� ����������������ก������ C# ��0����� &)�D���*�� ��*-�/ -����� Python

������#� $�% � �����ก&�ก��� '&����(�)�*+��*,���*-�����'-�ก��กSharp Develop (http://www.icsharpcode.net/opensource/sd/

Sharp Develop Sharp Develop ������DN�D���'����ก�O�����#�� � 1

�#�� � 1 D���'�� Sharp Develop ��0����������)����

Solution ��+�.�� ����������� -�ก��$�B����� ก��&���#� ��*0�

Console D�0� ��� ���'�ก��A#��.�A'������+��ก��

�� 2. �� Windows application

D�0� ��� ���'�ก��A#��.���+-����D���'��

��/����� �ก� �����������$�B����� ก�� �� Console ก'��

Console ����������&������� ก����-��0�ก���# File > New > SolutionS D���'�� New Project

�D��)���/��������� � 1. ��&'+� Categories �D�*��Vก��0�ก C# �� Windows Applications 2. ��&'+� Templates ��0�ก Console Application 3. �D����.0�� (Name) �D�ก�� Solution ��� �� ��D��.�.0��+'� 4. ก� Create

����......................................................������ ......................................

Solution 01204111 : Computer & Programming

�%�&'#()� Python ��ก'��

������#� $�% � �����ก&�ก��� '&����(�)�*+��*,���*-�����'-�ก��ก &)�D����*�0����0�� �http://www.icsharpcode.net/opensource/sd/ )

��+�.�� ����������� -�ก��$�B����� ก��&���#� ��*0�

Windows application D�0� ��� ���'�ก��A#��.���+-����D���'��

New Project �����ก�/�������#�� � 2

Windows Applications Console Application

��� �� ��D��.�.0��+'� mech

......................................���...............

Page 2: Lab 7 : C# # $ %& '#()theory.cpe.ku.ac.th/~jittat/204111/labs/lab7-c-sharp-intro.pdf · Lab 7 : C# Lab 7 : ก ˘ˇˆก˙ ˝ ˙ ก ˛˚ ˜ ก ˚˝ ˙ C# # ... Windows application

Lab 7 : ������ � C# �������������

������ *)��0��+'�� Solution .0��� ��-#' ��+�$���+'�� ก���.������ก'�� Develop �/ -����/���ก'��D�'�����

��0�����&���� Solution �� -����- ��+ ����#�� � 3

�#�� � 3 D�����&)�D��� ก��/ ���+�&����� ก��/��

��0��������� ���� Sharp Develop

��� ก�������.'����ก���D���'�� ��&����(�� -ก�D���� ก�����ก�'�+�)���������-ก��,^�

�����ก������ 1 : "�#$%&%'()*ก�+,-. /#0*"�#ก�+,-."� 1 �$�+2$3%.34

������������� (2) 01204111 : Computer

.0��� ��-#' ��+�$���+'�� ก���.������ก'�� D�ก�ก��/����D�ก� _Overwritea

�� -����- ��+ Sharp Develop �� &��D������ �$�����D���� ก��/ ���������� ก��

D�����&)�D��� ก��/ ���+�&����� ก��/�� Sharp Develop

Sharp Develop �� &���*��/����� ก�����D���-������� ���&�����/ -�D�0� ก��/

��� ก�������.'����ก���D���'�� ��&����(�� -ก�D���� ก�����ก�'�+�)���������-ก��,^�

"�#$%&%'()*ก�+,-. Run 70ก$�8$��$�9ก:((�3;4���<�#=>0?ก%�)@<AB98$)C>0*<A

Ê�ǹÊ��ËÃѺÊ�ǹÊ��ËÃѺÊ�ǹÊ��ËÃѺÊ�ǹÊ��ËÃѺà¢Õ¹â»Ãá¡ÃÁà¢Õ¹â»Ãá¡ÃÁà¢Õ¹â»Ãá¡ÃÁà¢Õ¹â»Ãá¡ÃÁ

»Ø�Á »Ø�Á »Ø�Á »Ø�Á RunRunRunRun

01204111 : Computer & Programming

_Overwritea �$0���D� Sharp

�� &��D������ �$�����D���� ก��/ ���������� ก��

�� &���*��/����� ก�����D���-������� ���&�����/ -�D�0� ก��/

��� ก�������.'����ก���D���'�� ��&����(�� -ก�D���� ก�����ก�'�+�)���������-ก��,^� Run ( )

70ก$�8$��$�9ก:((�3;4���<�#=>0?ก%�)@<AB98$)C>0*<A D#0E+A�กA.�F0*0$

à¢Õ¹â»Ãá¡ÃÁà¢Õ¹â»Ãá¡ÃÁà¢Õ¹â»Ãá¡ÃÁà¢Õ¹â»Ãá¡ÃÁ

Page 3: Lab 7 : C# # $ %& '#()theory.cpe.ku.ac.th/~jittat/204111/labs/lab7-c-sharp-intro.pdf · Lab 7 : C# Lab 7 : ก ˘ˇˆก˙ ˝ ˙ ก ˛˚ ˜ ก ˚˝ ˙ C# # ... Windows application

Lab 7 : ������ � C# ������������� (3) 01204111 : Computer & Programming

1.1 �*�+$�(�+,)+����ก�� �*��/����� ก��� � Sharp Develop &����/�����c��+�-'��/����� ก������ C# ��-���+�� ����#�� � 4

1: 2: 3: 4: 5: 6: 7: 8: 9:

10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20:

/*

* D��-�D,/����� ก�� �.�&)�D����/ -��%���-���+��

*/ using System; namespace mech { class Program { public static void Main (string[] args) { Console. WriteLine ("Hello World!"); // TODO: Implement Functionality Here Console. Write ("Press any key to continue . . . "); Console. ReadKey( true ); } } }

�#�� � 4 �*��&������� ก����0�����

��� ก�����ก�'�+ &���D��DN����&��& (namespace) *��& (class) ����%�� Main (d�����c���%�� �ก� ���(#ก�)��������� ก������ C#) d�������0������������������/ -���� ก����&'+�/�� class ��'����� d���กN*0���%�� Main ������� ��&'+��0�� f �D���ก�� -�*��+����������-������%���-����-D���

�����ก������ 1.1.1 : Good morning teacher. ��ก��� ก�����#�� � 4 �D���ก�� -���*)�&������������ � 12 g 17 ��ก��ก���� ����+-*)�&����D�'�� � Console. WriteLine ("Good morning teacher."); Console. WriteLine ("How are you?"); Console. WriteLine ("I'm fine thank you, and you?"); Console. ReadLine ();

��ก����ก� Run ��+�����กA�� ������กก���)����/����� ก��

�����ก������ 1.1.2 : Good morning teacher again. ��ก��� ก���� ��ijกD�� 1.1.1 �D���ก�� -� ก��/*)�&��� Console.WriteLine ��c� Console.Write A�� ������กก�������� ก������c��.'���

$-&�,)+ namespace

$-&�,)+ class

$-&�,)+0�1)' MainMainMainMain

Page 4: Lab 7 : C# # $ %& '#()theory.cpe.ku.ac.th/~jittat/204111/labs/lab7-c-sharp-intro.pdf · Lab 7 : C# Lab 7 : ก ˘ˇˆก˙ ˝ ˙ ก ˛˚ ˜ ก ˚˝ ˙ C# # ... Windows application

Lab 7 : ������ � C# ������������� (4) 01204111 : Computer & Programming

��ก ��ijกD��� � 1.1.1 �� 1.1.2 ��ก�� -�*��+'�*)�&��� Console.Write �� Console.WriteLine �D�A�ก���)���� ก'��ก���-'����

�����ก������ 1.1.3 : Console.ReadLine()

��ก��� ก���� ��ijกD��� � 1.1.1 �� 1.1.2 ����� *)�&��� Console.ReadLine(); �-#'��������&,����-/�� ��%�� Main �D���ก�� -������*)�&��� Console.ReadLine(); ��ก ��+ ��+��� Run ��� ก�� A�� ������ก��� ก����c��.'���

��ก�� -�*��+'�*)�&��� Console.ReadLine(); � D���� ��)�����.'���

1.2 ����ก����*�$3�*(� �D���ก�� -�$����O���� ก���������$%��'���� � 1: 2: 3: 4: 5:

name = input("Enter item's name: ") price = int(input("Enter item's price: ")) pay = int(input("Enter the amount the customer pay: ")) print(name,"price is",price) print("Customer pay", pay, ",change is", pay - pric e, "baht.")

��� ก�����ก�'�+��� -��� -�ก����� ก�������� C# ������� � 1:

2:

3:

4:

5:

6: 7:

8:

9:

10:

11: 12:

13:

14:

15:

16:

17: 18:

19:

20:

21:

using System;

namespace mech

{

class Program

{

public static void Main(string[] args) {

string name;

int price, pay;

Console.Write("Enter item's name: ");

name = Console.ReadLine(); Console.Write("Enter item's price: ");

price = int.Parse(Console.ReadLine());

Console.Write("Enter the amount the customer pay: ");

pay = int.Parse(Console.ReadLine());

Console.WriteLine("{0} price is {1}",name, price);

Console.WriteLine("Customer pay {0},change is {1} baht.",pay,pay-price); Console.ReadLine();

}

}

}

Page 5: Lab 7 : C# # $ %& '#()theory.cpe.ku.ac.th/~jittat/204111/labs/lab7-c-sharp-intro.pdf · Lab 7 : C# Lab 7 : ก ˘ˇˆก˙ ˝ ˙ ก ˛˚ ˜ ก ˚˝ ˙ C# # ... Windows application

Lab 7 : ������ � C# ������������� (5) 01204111 : Computer & Programming

��� ก��/��������ก�'�+ &���D��DN�*+�� ก'��D��-���ก����D+'������ C# ������ Python �����$����O�&'+�-'�- f /����� ก�����ก�'�+ ���� �

�AA��� /F0&��* /F0);%�0C

8 - 9 string name;

int price, pay; ������ C# �+ ���,ก�+������ ก�����ก��ก'��ก���.���� �+ ��D�����+��� ��.��� (type) � �*�� � ����*0����กN�/���#���� ��.����� -+���ก���)����

10 Console.Write

("Enter item's name:"); *)�&��� Console.Write �)�D���� � &��/��*+����ก��D�����

11 name =

Console.ReadLine(); Console.ReadLine() �)�D���� ��'��*'���กA#��.� ��+*0�*'�������c�&��� d������ �� �����)�&��������กN��&'�+����+ �� name � ก�

12 Console.Write

("Enter item's price: "); *)�&��� Console.Write �)�D���� � &��/��*+����ก��D����� �.'��� -+ก��ก��*)�&������������ � 10

13 price = int.Parse

(Console.ReadLine()); *)�&��� Console.ReadLine() �)�D���� ��'��*'���กA#��.���c�&��� *���-ก��*)�&��� input ������ python �������D�ก���ก��*'���c��+��/�)��+��N�������� *)�&��� ���.���� ก� d������ �� �กN*0�*)�&��� int.Parse *���-ก��*)�&��� int() ������ python��ก�����กN��+��/� �����+����+ ��.0�� price

14 - 15 Console.Write

("Enter customer pay: ");

pay = int.Parse

(Console.ReadLine());

�)������ก�O��� -+ก��ก��*)�&������������ � 12 - 13

16 Console.WriteLine

("{0} price is {1}",name,

price);

*)�&��� Console.WriteLine �)�D���� � &��/��*+����ก���D����� ��-�� ��� �/��*+����)� D�'� {0} �� {1} ��+-*'�� ��กN��+����+ �� name �� price ���)����

17 Console.WriteLine

("Customer pay {0},change

is {1} baht.",pay ,pay-price);

*)�&��� Console.WriteLine *'�� �(#ก�)��� ���� {0} D�0� {1} ��'�)���c������c��+ ���&���� &����(�.���$��t���*O���&�t (�+�-'����������� �*0� pay g price) '�� f ������.'�ก�� ��-��� ก����*)��+O��$��t���*O���&�t���� f �����A���$%t&,����- �$0���)�A���$%t������ ��*'�

Page 6: Lab 7 : C# # $ %& '#()theory.cpe.ku.ac.th/~jittat/204111/labs/lab7-c-sharp-intro.pdf · Lab 7 : C# Lab 7 : ก ˘ˇˆก˙ ˝ ˙ ก ˛˚ ˜ ก ˚˝ ˙ C# # ... Windows application

Lab 7 : ������ � C# ������������� (6) 01204111 : Computer & Programming

�����ก������ 1.2.1 : �ก#<BE+A�กA.

�D���ก�� -��)��� ก���� ก����/��ก)�D��'���� � ��+�������ก/��A��$���� �*���$����t �����ก�� (��&�� -กก�� '��/��)

ก0A�ก#E+A�กA. B#):%�3(0����/).<3?()A4�7#*3A#).);%�0C&0?�',B)*/=0.:%�3(0�

1. �� ; � ����-���������������

2. �������� string name;

��������� � 8 ��ก

3. ก��/������� � 13 ��-���*)�+'� int.Parse ��ก �D��D�0���c� price =

(Console.ReadLine());

����������� ����

/F0D0. /F0')�

1. ��ก*)�&������������ � 16 D�ก��� �-� {0} �� {1} ��c� {1} �� {0} ���)���� A���$%t� ������c��.'���

2. ��ก*)�&������������ � 16 D�ก��� �-�/��*+�� name �� price ��c�/��*+�� price �� name �����)���� A���$%t� ������c��.'���

3. ��ก*)�&������������ � 16 D�ก��� �-� {0} �� {1} ��c� {1} �� {2} ���)���� A���$%t� ������c��.'���

1.3 0ก�5�60#,: 0�1)' ��&'+�� ������������ก���-��/ �D�&���� solution /����� ก�� �� Console /������D�' ��ก������������������-ก���$�����%�� RandInt � ���c���%��&)�D���&������/&,'��/��������� ก�� ��%�����ก�'�+���.��+ �� randGen � ���c��+ �� �� global ��ก��&,'��+��/ /��&���กกN*0�������%�� ���+ ��/�����ก�����ก����+-*)�+'� static ����������ก����-���� -�/��ก���/ -���%�� ����0���/���+ �� global �-'������ -�'��� &)�D������ ��D��.������ก'��

Page 7: Lab 7 : C# # $ %& '#()theory.cpe.ku.ac.th/~jittat/204111/labs/lab7-c-sharp-intro.pdf · Lab 7 : C# Lab 7 : ก ˘ˇˆก˙ ˝ ˙ ก ˛˚ ˜ ก ˚˝ ˙ C# # ... Windows application

Lab 7 : ������ � C# ������������� (7) 01204111 : Computer & Programming

�D��$���ก�����ก���+ �� �� ��%�����ก�'�+����*��& Program '�-#'��-��ก��%�� Main ��%�� &����c� (�& ������#�� � 6 class Program { static Random randGen = new Random(); static int RandInt(int fr, int to) { return fr + (randGen.Next() % (to - fr + 1)); } public static void Main(string[] args) { // ……… } }

�#�� � 6 &��ก�����ก���+ �� randGen ����%�� RandInt

������������%�����ก�'�+ �D� ก���%�� Main �D���c���������'�� public static void Main(string[] args) { Console.WriteLine(RandInt(1,100)); Console.WriteLine(RandInt(1,100)); Console.WriteLine(RandInt(1,100)); Console.ReadLine(); }

�D�������� -ก�D���� ก���)���� �����$�+'���� ก����$��$t�+��/��ก��� Console d����+��/�D�'�� ��'��� ก'��ก�� ��ก������������ �-��+��/ ��� ���/ 1 ����/ 100 ��-�D��+��/�+D���� *'����-ก+'��&�� ��+��*)�(��+'� _��%�� RandInt() �)�D���� �����a

1.4 0ก�5�60#,: ����ก��8#9ก ������/ -���� ก����-��/ ���'�-������ C# ��� ก�����ก�'�+��� ��ก�O�ก���)�����.'��� -+ก����� ก��� �����*-�/ -������� Python ก���� ก��D��ก��c���������'�� ��ก�����D������&����D���� ก���)���� public static void Main(string[] args) { int s = RandInt(1,100); int g = -1; while(g != s) { Console.Write("Please guess: "); g = int.Parse(Console.ReadLine()); if(g > s) Console.WriteLine("Your guess is too high ."); if(g < s) Console.WriteLine("Your guess is too low. "); } Console.WriteLine("You guessed correctly."); Console.ReadLine(); }

Page 8: Lab 7 : C# # $ %& '#()theory.cpe.ku.ac.th/~jittat/204111/labs/lab7-c-sharp-intro.pdf · Lab 7 : C# Lab 7 : ก ˘ˇˆก˙ ˝ ˙ ก ˛˚ ˜ ก ˚˝ ˙ C# # ... Windows application

Lab 7 : ������ � C# ������������� (8) 01204111 : Computer & Programming

�����ก������ 1.4.1 "�#')�/F0D0.'>)<+$�8

1. ��/� �&,'����� /���/��c���'���?

2. �D��%���-ก���)����/��*)�&��� while ����� ก�� ��0����/��� ��)��D���� ก�����ก�)����d�)� (*)����: *)�&�����ก�'�+� ก���)�����D�0��ก�������� Python)

3. &���+'�*'�� �*0���ก��%�� RandInt *0� 45 ��A#��.���-+'� 70 ��� ก����$��$t����-*����ก��

�����ก������ 1.4.2 7F0$=$/A�8*B)*ก0A�0C

ก���� ก���D�$��$t�)��+�*����/��ก����-��ก����0����� ก���)�����&�N���+- ��-���*)�&���� ����ก��������� ก�������'�� ������� ��ก �-+/���� �(#ก�$�������� ก�� &����c� (�& ��� public static void Main(string[] args) { int s = RandInt(1,100); int g = -1; int count = 0; while(g != s) { ___________________________________________ _______________ Console.Write("Please guess: "); g = int.Parse(Console.ReadLine()); if(g > s) Console.WriteLine("Your guess is too high ."); if(g < s) Console.WriteLine("Your guess is too low. "); } Console.WriteLine("You guessed correctly."); Console.WriteLine("You guessed for {0} times. ”,_________________); Console.ReadLine(); }

1.5 0ก��&ก0#, ������/ -��ก�ijก�+ก��/ ��-�+�-'��/��ก����'��ก����ก�'�+&��*���� &������#������'�� 42 + 72 = ? 114 Good. You're correct. 56 + 75 = ? 7 Sorry. The correct answer is 131.

��ก���/ -� �D���&'+�/����%�� RandInt ��ก�#�� � 6 ���&'��*��&��+- �$0���.���ก��&,'��+��/

Page 9: Lab 7 : C# # $ %& '#()theory.cpe.ku.ac.th/~jittat/204111/labs/lab7-c-sharp-intro.pdf · Lab 7 : C# Lab 7 : ก ˘ˇˆก˙ ˝ ˙ ก ˛˚ ˜ ก ˚˝ ˙ C# # ... Windows application

Lab 7 : ������ � C# ������������� (9) 01204111 : Computer & Programming

�����ก������ 1.5.1 &>=$&,>./F0D0.

�D������� ก������%�� Main � �&,'�*)�(���D�&��#�Ot ��� ก�����ก�'�+��&,'��)��+��N���D+'�� 1 (�� 100 ��&���)��+� ��+$��$t*)�(�� (����� ก��(#ก��� ��+��0���� -ก�D���� ก���)���� ��� ก���� &��*)�(��� �&,'���� ��+��A#��.�ก��,�̂ Enter ก'����ก���)���� public static void Main(string[] args) { int x = ____________________________________; ____________________________________________; Console.Write(”{0} + {1} = ? ”,____, y); Console.ReadLine(); }

�����ก������ 1.5.2 &>=$'A=7/F0')�

��&'+��+�*)������� ������.�*)�&���*+�*,� if �$0���+�&����0����/ *)�&��� if � �#� ������ � if( �������� ) statement;

&���� � ก'����ก������ Python *0� *)�&��� if ��*+�*,�*)�&���� ����� *'*)�&����� -+��'����� (�����ก���D�*+�*,�D��-*)�&��� ������+�*)�&���D��- f �����c�ก�,'�*)�&����� -+ ��-�.��*�0���D��-�|กก� ����#� �������'�� ��&�������� -�*)�&���� ��-'������ -�� ก*����D����

if( �������� ) { statement1; statement2; … statement3; }

ก���/ -���0����/ D�0���$��t��ก��&�t������ C# ��-���+�� ��+��' ก'����ก������ Python ��ก��ก ก�'�+*0� (��������ก����&��+'� ��'�ก��, ��กก+'�, D�0����-ก+'� ������.��+�)�����ก�� _==a, _>a, D�0� _<a ���)���� &���� � ก'��*0������� C# ��'� �+�)�����ก�� and , or , D�0� not '���.�&�~��ก�Ot ��

������ C# ก��-'�D�����'� *+��D��-�� f '�ก��*���$�tD�0��)���� ก�����)���� �-'����กN�� ���*+���-'�D�������ก�O��� -+ก���� Python �$0���)��D���� ก���'���'�-

�D��/ -���� ก��&'+��+�*)��� ��-������ ก�������'���D�&��#�Ot ��'����/ -�&'+�&,'� �� &��*)�(�� public static void Main(string[] args) {

// ===== &>=$&,>.�(@�&�*/F0D0.DRก(@<=# ==========

int ans = _____________________________ if(_______________________) Console.WriteLine(”Good. You’re correct.”) ; else Console.WriteLine(_________________________ _________________); }

Page 10: Lab 7 : C# # $ %& '#()theory.cpe.ku.ac.th/~jittat/204111/labs/lab7-c-sharp-intro.pdf · Lab 7 : C# Lab 7 : ก ˘ˇˆก˙ ˝ ˙ ก ˛˚ ˜ ก ˚˝ ˙ C# # ... Windows application

Lab 7 : ������ � C# ������������� (10) 01204111 : Computer & Programming

2. ����ก��;3';-)ก9�<=(>?(���ก��@Aก$B

2.1 ��&*3'0�CD)+;(� ��ก��$�B����� ก��� ���'�ก��A#��.� ��ก����ก&t������+����+&t (D�0��� -ก+'� GUI) � �#� ��� � ก'������กก���/ -���� ก��� �����*-��ก�������� ��-��ก��� ก��� ������ก����������� �,�������� ���,�&���&,��� -+ ��ก��ก� � ��ก���)���� A#��.�����'�ก����� ก�����#� ��� �*'��/����-�+ A��ก������� ก��� ��)���� ��ก����ก&t � �A#��.�&����(��0�กก���)�ก��ก��D��- f &'+�/��D���'������-'����c���&����กก+'�

��������#� ��ก���/ -���� ก����'�ก��A#��.� ��ก����ก&t��ก��-��.�ก����� ก�� ����&����D,ก��Ot (event-driven) -ก�+�-'���.'� ��ก���/ -���� ก��ก��D���'��� �� �,^��-#'D��-�,^� ��� �������/ -���� ก��D��ก��� ก���� -+ ������/ -���%��-'�- f D��- f ��%���$0���)����ก���D,ก��Otก��ก��,^�D��- f �,^��D�'����� -กก��

2.2 5'#)+$�(�+����ก����ก ��&'+�� �������������� ก��� ���'�ก��A#��.�A'�����+����+&t ��� ก�����ก�'�+�� &���,^� ��0�����ก��,^����ก�'�+ /��*+����D������� &��*)�+'� Hello, world

�D�ก��&���� Solution ���ก�'�+ �� Sharp Develop �D�&���&���� Solution ��-��&'+�/�� Templates �D���0�ก SWindows applicationT ��0��������, templates &)�D���&'+�� ��D��&'.0�� solution ��c� winhello (��� solution � ��-#'���*�0����-#' ��+�D�ก� overwrite �$0���/ -����

��0��&���� solution �&�N� ��+ D�����/�� Sharp Develop ��� ��ก�O���''����ก�#�� � 3 � �����.��/ -���� ก���� Console ��'�����ก �-'����กN��� ������'��/��&'+�&)�D��� ก��/��� ก�� (Editor) ��� �N� Design ���ก�/��� ��� &�����#�� � 5 (����&���'$� �N����ก�'�+ �����c��$�����&���'���&���� Solution (#ก��� (�.'� ��'�����0�ก������ Windows application) �D�ก�����&���� Solution �D�'� ก*����

�#�� � 5 (a) �N� Design, (b) D�����&)�D�����ก ��

��0�����ก�� � �N����ก�'�+ ������DN�D���'�����'� f ��&'+�� ��*-��c���� ก�� D������ ���c�D�����&)�D�����ก �� �$0���D�ก���)����&��+ก �D�ก���0�ก �N� Tools � �&'+�D�����/���d��- �� �N� Properties � �&'+�D�����/���/+�/�������+- )� D�'�/�� �N�����&�� ��D�����A���$%t &�����#�� � 6 (a)

Page 11: Lab 7 : C# # $ %& '#()theory.cpe.ku.ac.th/~jittat/204111/labs/lab7-c-sharp-intro.pdf · Lab 7 : C# Lab 7 : ก ˘ˇˆก˙ ˝ ˙ ก ˛˚ ˜ ก ˚˝ ˙ C# # ... Windows application

Lab 7 : ������ � C# ������������� (11) 01204111 : Computer & Programming

(a) (b)

�#�� � 6 (a) &��D�����&)�D�����ก ��D���'�� +�ก������d��- ������/+� &��)� D�'�/�� �N� Tools �� Properties (b) &�� �N� Tools D�����ก��0�ก Windows Forms ��+ ���DN�+'�� +�(,� ����&����(��0�ก�.����D��- ��

��&'+���ก���D����� �� &��D���'��/����� ก��/����� �$0������ก��*+��&��&�������� -กD���'������+'�D���'�� winhello

�����ก���� 2.2 ��������������ก������� ����������� !""#��

�D�ก��,�̂ X � ��,�/+�/��D���'��� ����&����/��� �$0��ก�����-��D������/ -���� ก��/�� Sharp Develop

2.2.1 �$#0'>0*�(@=�'D,

��D��� Design &'+� �N� Tools � �����d��-����c�&'+�� �����.���0�ก+�(,�.'� �,^� ���-/��*+�� D�0� ก�'��/��*+�� �$0���)���+����D���'��� ����ก)��������ก ��

�� �N� Tools ������DN�ก�,'�/��+�(,D��- f ������ �D�ก���0�ก _Windows Formsa ������DN�+�(,$0������)��+���ก ��� &���D��#�� � 6 (b) ���&����(��0�ก+�(,���ก�'�+ ��ก�����)���+����D���'��/��������

������������ก ��D����� ���'�- f ��-ก���0�ก+�(,������ Button ��ก �N� Tools ��+*��Vก� ���-��D���'�� winhello ������DN��,^� ���ก�/��� ���,^����ก�'�+���/ -�+'� button1 ��ก�����D���0�ก+�(,������ Label ��+�)��.'��� -+ก�� �����$�+'�� ���-/��*+��+'� label1 ���ก�/���� ���D���'�� winhello ���&����(ก���0�ก�����,^�D�0����-/��*+����� ��0����0�ก ��+������DN�ก�����c��&��������+�(,���ก�'�+ ���&����(��0���)� D�'�/��+�(,� ������0�ก�+��D��� -��+&+-������ �+�-'��/��D���'�� winhello D���/������ � &������#�� � 7 (a)

Page 12: Lab 7 : C# # $ %& '#()theory.cpe.ku.ac.th/~jittat/204111/labs/lab7-c-sharp-intro.pdf · Lab 7 : C# Lab 7 : ก ˘ˇˆก˙ ˝ ˙ ก ˛˚ ˜ ก ˚˝ ˙ C# # ... Windows application

Lab 7 : ������ � C# �������������

��0����ก ��D������&�N� ��+ ���&����(&����D���� ก�/ -+) �#�� � 7 (b) &��D���'����0����� ก���)���� ��+ ก'�����)������/�����(���� �D������� ก��� ��)�����-#'�& -ก'�� ��'�.'������������'&����( ก��/��� ก��/��������

(a)

�#�� � 7 (a) &��D���'�� winhello

(b) �+�-'��D����� winhello

2.2.2 /,U&.��'% (Property)

+�(,'�� f ��D���'�� �.'� ���-/��*+�� �,^� D�0�ก�'��/��*+�� ��+�� *,O&����� ����&����(������� �-���� �.'� )� D�'�, & , /��*+��� � &�� ���&����(���� '�+�(,��D�����*,O&���� (property) ����-'��/��+�(,�D�'����� ��0�������0�ก+�(,D���� f ��-ก��*,O&����/��+�(,������(#ก &���� �N� Properties

�D�*��Vก��0�ก button1 *,O&����/�� �D���c� Hello ������DN�+'�/��*+�� button1

���� '���-���� -��0�� f /��+�(,���� ก �.'� ��� �-�& D�0���� �-��#� �����t �D���������� '��,^��D�� /����D~'/��� ��� & �����ก�� A@=�*)C>0+A��?+(��C$/,U&.��'%

(Name))

�����ก���� 2.2.2 ��"������������ก�������

&���ก+'�D���'��� � &����� ��ก�O�� ���� �-������ ������ก �� D���'�� ����� ���0�����ก���-����'����/ -���� ก���$0����&����D,ก��Ot�D�'����� ������/ -�&'+����ก�'�+��/�����(���� �-'��0������� ก��� ��)�����-#'� �ก'����+-

������������� (12) 01204111 : Computer

��0����ก ��D������&�N� ��+ ���&����(&����D���� ก���)����������� ��-ก��,^� Run ( &��D���'����0����� ก���)���� ��+ ก'�����)������/�����(���� �D������� ก��� ��)�����-#'

�& -ก'�� ��'�.'������������'&����( ก��/��� ก��/��������

(a) (b)

winhello �� Sharp Develop &���ก+'����#��,^� button1

winhello ��0�����&����D���� ก���)���� (c) *,O&����/���,�̂

+�(,'�� f ��D���'�� �.'� ���-/��*+�� �,^� D�0�ก�'��/��*+�� ��+�� *,O&����� ����&����(������� �-���� �.'� ���&����(���� '�+�(,��D����� winhello � ������ก ���-#'��� ��-��� �-� ���

����-'��/��+�(,�D�'����� ��0�������0�ก+�(,D���� f ��-ก��*,O&����/��+�(,������(#ก &����

*,O&����/�� button1 ��(#ก &������#�� � 7 (c) �D��������� �button1 ���,^���(#ก��� �-���c� Hello ��� ����ก)�D������ ���&����(

���� '���-���� -��0�� f /��+�(,���� ก �.'� ��� �-�& D�0���� �-��#� �����t �D���������� '��,^��D�� /����D~'+A��?+(��C$/,U&.��'% Name ( &���� �N� Properties �-#'��&'+�

��"������������ก������� ���$ก������

&���ก+'�D���'��� � &����� ��ก�O�� ���� �-������ ������ก �� �-'����กN��-����'� �����ก��/�����0�����ก��,^���D���'�� ����� ���0�����ก���-����'����/ -���� ก���$0����&����D,ก��Ot�D�'����� ������/ -�&'+����ก�'�+��/�����(���� �-'��0������� ก��� ��)�����-#'� �ก'����+-

01204111 : Computer & Programming

Run (&���D� �-� play & &��D���'����0����� ก���)���� ��+ ก'�����)������/�����(���� �D������� ก��� ��)�����-#'

(c)

button1 (#ก��0�ก�-#' *,O&����/���,�̂ button1

+�(,'�� f ��D���'�� �.'� ���-/��*+�� �,^� D�0�ก�'��/��*+�� ��+�� *,O&����� ����&����(������� �-���� �.'� � ������ก ���-#'��� ��-��� �-� ���

����-'��/��+�(,�D�'����� ��0�������0�ก+�(,D���� f ��-ก��*,O&����/��+�(,������(#ก &����

�D��������� �-� *,O&���� Text ��� ����ก)�D������ ���&����(

���� '���-���� -��0�� f /��+�(,���� ก �.'� ��� �-�& D�0���� �-��#� �����t �D���������� '��,^��D�� /����D~'�-#'��&'+� Design ��c�

�-'����กN��-����'� �����ก��/�����0�����ก��,^���D���'�� ����� ���0�����ก���-����'����/ -���� ก���$0����&����D,ก��Ot�D�'����� ������/ -�&'+����ก�'�+��/�����

Page 13: Lab 7 : C# # $ %& '#()theory.cpe.ku.ac.th/~jittat/204111/labs/lab7-c-sharp-intro.pdf · Lab 7 : C# Lab 7 : ก ˘ˇˆก˙ ˝ ˙ ก ˛˚ ˜ ก ˚˝ ˙ C# # ... Windows application

Lab 7 : ������ � C# ������������� (13) 01204111 : Computer & Programming

2.2.3 E+A�กA.&=�&��

������$������ ก���$0����&���ก��ก��,^� button1 ���&����(������/ -���� ก��ก���D,ก��Ot���ก�'�+�����-ก��������*��Vก� � button1 �� �N� Design ��0��ก� ��+ D�����/�� Sharp Develop ����� �-��� &��A���c�&'+� ก��/��� ก�� (Editor) �D�0��������/ -���� ก��ก�� Console &���ก+'� �N������'�� (#ก��0�ก�-#'� � Source ��'�.' Design �������0�ก �N�� ���0�����ก�� ก��/��� ก��

��0������������*��Vก� ��,^� Sharp Develop ��&������%���D�' .0�� Button1Click �$0���D�����/ -���� ก���$0����&���ก���D,ก��Ot������/���,^� (d���*0�ก��(#ก*��Vก) �D�ก����� �*��/����%�����ก�'�+ &�����#�� � 8

�#�� � 8 &���*��/����%�� Button1Click � �����&�����D�

��0��� ก��ก��,^� �����&����D� label1 ��� �-�/��*+��� � &����c� Hello, world ������������ ก���%�� Button1Click �D���c���������'��

void Button1Click(object sender, EventArgs e) { label1.Text = "Hello, world"; }

�D������&����D���� ก���)���� �����ก��,^� ��+�#A���$%t

�����ก���� 2.2.3 &��'�"�ก���������ก������(�" )��)*� Good-bye 2#'�"��ก�����ก����*)���? "4*����?

2.3 ?ก.ก�+,-.

��&'+�� �������/ -��ก�ก��,^� ��-�,^���-��-� �D� ��&,'�D�����ก���ก� ��+ ����D��-*0�ก��D�����)��+�*������ก� �&,�

2.3.1 'F0��$>*�$�$#07)

+�(,�,ก.�����D���'��� ������ก �� ��� *,O&����� ��.�ก)�D��)� D�'� ��/��� *0� Top, Left , Width , �� Height ���� *+��D��- &�����#������'��

Page 14: Lab 7 : C# # $ %& '#()theory.cpe.ku.ac.th/~jittat/204111/labs/lab7-c-sharp-intro.pdf · Lab 7 : C# Lab 7 : ก ˘ˇˆก˙ ˝ ˙ ก ˛˚ ˜ ก ˚˝ ˙ C# # ... Windows application

Lab 7 : ������ � C# ������������� (14) 01204111 : Computer & Programming

�#�� � 9 &��*,O&����� �ก)�D��)� D�'� ��/���/��+�(,��D���'��

�������������������-ก�� ก���%�� Button1Click �D���c���������'�� void Button1Click(object sender, EventArgs e) { button1.Left += 10; }

�����ก��� 2.3.1 �����"�����ก�����ก�*�) 2�ก��6�'"� ��&��7��*��6ก*"������'*"��

1. ��� ก��/������)��������?

2. (��������ก���D��,^���0���/��� ����� ก�*)�&�����c��-'����?

2.3.2 +,-.C#0C���

�D�*����ก��%�� RandInt ��&'+����ก���+ �� randGen ��ก�#�� � 6 ���$�����*��& MainForm (�)���+���+�ก'�� ��%�� Button1Click ) ��ก�����D� ก���%�� Button1Click �D�&,'���� �-�)� D�'�/���,^� ������ �+ ก� x �� y �D�&,'��D�*'� Top �� Left �-#'��D+'�� 0 (�� 100 (*)���� �-'��0���%�� RandInt )

�����ก��� 2.3.2 ���7(4�7��"� Button1Click �����

void Button1Click(object sender, EventArgs e) {

}

2.3.3 '�=$��

���������)��+�*����� �A#��.�*��Vก(#ก��� ก'���0���D����ก���+ �� clickCount � ��.��กN��)��+�*����� ����ก��,^����ก'�� ��-�D����ก���+���-��ก��%�� Button1Click '�-#'��-��*��& MainForm ��� &�����#�� � 10 &���ก+'����ก)�D���D�*'��������/���+ �����ก�'�+��c��#�-t

Page 15: Lab 7 : C# # $ %& '#()theory.cpe.ku.ac.th/~jittat/204111/labs/lab7-c-sharp-intro.pdf · Lab 7 : C# Lab 7 : ก ˘ˇˆก˙ ˝ ˙ ก ˛˚ ˜ ก ˚˝ ˙ C# # ... Windows application

Lab 7 : ������ � C# ������������� (15) 01204111 : Computer & Programming

namespace winhello { public partial class MainForm : Form { //……… //……… static int clickCount = 0; void Button1Click (object sender, EventArgs e) { // ……… } } }

�#�� � 10 ก�����ก���+ �� clickCount

�$��������������'��� � &��*'�/���+ �� clickCount ���,^� button1 �������-��%�� Button1Click button1.Text = clickCount. ToString () + " click(s)";

��ก����������� -ก�D���� ก���)���� ������DN�/��*+��+'� 0 click(s) &�����,^���0�����*��ก����,^�

�����ก���� 2.3.3 ���'"� ��&��'*"���6

1. �D��������*)�+'� _.ToString() a ��ก��ก������/����� ��ก����&����D���� ก���)���� /��A��$���� �$�*0�����? �$����D,��?

2. (��������ก���$����)��+�*����� �*��Vก����,^� ���������$���*)�&������/��������%�� Button1Click