Oct 30, 2011

Some simple code in C #

Well, code easy to begin to understand this language.














Run Program:

1. System.Diagnostics. Process. Start
("file");







Mini-Calculator in console mode:

1.

using



System;2.

3.

namespace



Calculator

4.

{5.

class



Program

6.

{7.

public static void



Main



(string [] args)8.

{9.

first



float



/ / The first number10.

float



second;



/ / The second number11.

operation string



/ / The operation to perform12.

13.

Console.



Title



=



"Mini Calculator"



/ / We format the console14.

Console.



BackgroundColor



= ConsoleColor.



White;15.

Console.



ForegroundColor



= ConsoleColor.



Blue;16.

Console.



Clear



();17.

18.

Console.



SetCursorPosition



(3, 2)



/ / We call the first number19.

Console.



WriteLine



("Enter



first



number");20.

Console.



SetCursorPosition



(60, 2);21.

first =



float.



Parse



(Console.



ReadLine



());22.

23.

Console.



SetCursorPosition



(3, 3)



/ / We call the operation24.

Console.



WriteLine



("Enter



the operation to perform



(+,-,*,/)");25.

Console.



SetCursorPosition



(59, 3);26.

operation = Console.



ReadLine



();27.

28.

Console.



SetCursorPosition



(3, 4)



/ / We call the second number29.

Console.



WriteLine



("Enter



second



number");30.

Console.



SetCursorPosition



(60, 4);31.

second =



float.



Parse



(Console.



ReadLine



());32.

33.

34.

Console.



SetCursorPosition



(57, 5)



/ / show the solution ...35.

Console.



WriteLine



("__________");36.

37.

Console.



SetCursorPosition



(3, 6);38.

Console.



WriteLine



("Result is");39.

Console.



SetCursorPosition



(60, 6);40.

41.

Console.



WriteLine



(calculate (first,



second,



operation));42.

Console.



ReadKey



();43.

44.

}45.

46.

private static



string calculate



(float



first,



float



second string



operation)47.

{48.

float



temp;

49.

switch



(operation)



/ / Structure with switch50.

{51.

case



"+":52.

temp = first + second;

53.

return



temp.



ToString



();54.

case



"-":55.

temp = first-second;

56.

return



temp.



ToString



();57.

case



"*":58.

temp = first * second;

59.

return



temp.



ToString



();60.

case



"/":61.

temp = first / second;

62.

return



temp.



ToString



();63.

}64.

return



"-1";65.

}66.

}67.

}68.









Now a graphic mode (look in the code the name of controls)

1.

using



System;2.

using



System.



Collections. Generic;3.

using



System.



Drawing;4.

using



System.



Windows. Forms;5.

using



System.



Text;6.

7.

namespace



Calculator

8.

{9.

/ / / <summary>10.

/ / / Description of MainForm.11.

/ / / </ Summary>12.

public



partial



class



MainForm: Form

13.

{14.

int



oper



/ / 1 -> + | 2 -> - | 3 -> * | 4 -> /15.

float



first;

16.

17.

18.

public



MainForm



()19.

{20.

InitializeComponent



();21.

}22.

23.

Numero7Click



void



(object



sender, EventArgs



e)24.

{25.

txtnum.



Text



= txtnum.



Text



+



7;26.

}27.

28.

Numero8Click



void



(object



sender, EventArgs



e)29.

{30.

txtnum.



Text



= txtnum.



Text



+



8;31.

}32.

33.

Numero9Click



void



(object



sender, EventArgs



e)34.

{35.

txtnum.



Text



= txtnum.



Text



+



9;36.

}37.

38.

Numero4Click



void



(object



sender, EventArgs



e)39.

{40.

txtnum.



Text



= txtnum.



Text



+



4;41.

}42.

43.

Numero5Click



void



(object



sender, EventArgs



e)44.

{45.

txtnum.



Text



= txtnum.



Text



+



5;46.

}47.

48.

Numero6Click



void



(object



sender, EventArgs



e)49.

{50.

txtnum.



Text



= txtnum.



Text



+



6;51.

}52.

53.

Numero1Click



void



(object



sender, EventArgs



e)54.

{55.

txtnum.



Text



= txtnum.



Text



+



1;56.

}57.

58.

Numero2Click



void



(object



sender, EventArgs



e)59.

{60.

txtnum.



Text



= txtnum.



Text



+



2;61.

}62.

63.

Numero3Click



void



(object



sender, EventArgs



e)64.

{65.

txtnum.



Text



= txtnum.



Text



+



3;66.

}67.

68.

Numero0Click



void



(object



sender, EventArgs



e)69.

{70.

txtnum.



Text



= txtnum.



Text



+



0;71.

}72.

73.

CClick



void



(object



sender, EventArgs



e)74.

{75.

txtnum.



Text



=



"";76.

}77.

78.

DivClick



void



(object



sender, EventArgs



e)79.

{80.

oper =



4;81.

first =



float.



Parse



(txtnum.



Text);82.

txtnum.



Text



=



"";83.

}84.

85.

MulClick



void



(object



sender, EventArgs



e)86.

{87.

oper =



3;88.

first =



float.



Parse



(txtnum.



Text);89.

txtnum.



Text



=



"";90.

}91.

92.

ResClick



void



(object



sender, EventArgs



e)93.

{94.

oper =



2;95.

first =



float.



Parse



(txtnum.



Text);96.

txtnum.



Text



=



"";97.

}98.

99.

SumClick



void



(object



sender, EventArgs



e)100.

{101.

oper =



1;102.

first =



float.



Parse



(txtnum.



Text);103.

txtnum.



Text



=



"";104.

}105.

106.

SolClick



void



(object



sender, EventArgs



e)107.

{108.

float



second =



int.



Parse



(txtnum.



Text);109.

float



result;

110.

111.

switch



(oper)112.

{113.

case



1:114.

result = first + second;

115.

txtnum.



Text



= result.



ToString



();116.

break;117.

case



2:118.

result = first-second;

119.

txtnum.



Text



= result.



ToString



();120.

break;121.

case



3:122.

result = first * second;

123.

txtnum.



Text



= result.



ToString



();124.

break;125.

case



4:126.

result = first / second;

127.

txtnum.



Text



= result.



ToString



();128.

break;129.

}130.

}131.

}132.

}







File Browser: (add a text box "txtRuta" two lists "lbcar" and "LBAR" and a button with the default name)




1.

using



System;2.

using



System.



Collections. Generic;3.

using



System.



ComponentModel;4.

using



System.



Data;5.

using



System.



Drawing;6.

using



System.



Text;7.

using



System.



Windows. Forms;8.

using



System.



IO;9.

10.

namespace



ExploradorCarpetas

11.

{12.

public



partial



class



Form1: Form

13.

{14.

public



Form1



()15.

{16.

InitializeComponent



();17.

}18.

19.

/ / The form load20.

private void



Form1_Load



(object



sender, EventArgs



e)21.

{22.

/ / Start the txtRuta23.

txtRuta.



Text



= Directory.



GetDirectoryRoot



(Directory.



GetCurrentDirectory



());24.

25.

/ / List folders26.

folders



(txtRuta.



Text);27.

28.

/ / List files29.

files



(txtRuta.



Text);30.

}31.

32.

/ / The button to explore routes33.

private void



button1_Click



(object



sender, EventArgs



e)34.

{35.

/ / List folders36.

folders



(txtRuta.



Text);37.

38.

/ / List files39.

files



(txtRuta.



Text);40.

}41.

42.

/ / When you double click on a route put it in txtRuta43.

private void



lbcar_DoubleClick



(object



sender, EventArgs



e)44.

{45.

txtRuta.



Text



= lbcar.



SelectedItem. ToString



();46.

47.

/ / List folders48.

folders



(txtRuta.



Text);49.

50.

/ / List files51.

files



(txtRuta.



Text);52.

}53.

54.

/ / Method that places the folders in the path indicated in the list55.

/ / Appropriate box56.

private void



folders



(string path)57.

{58.

lbcar.



Items. Clear



();59.

60.

string



[]



folder = Directory.



GetDirectories



(path);61.

62.

foreach



(string



car in



folder)63.

lbcar.



Items. Add



(car);64.

}65.

66.

/ / Method that places the files in the path indicated in the list67.

/ / Appropriate box68.

private void



files



(string path)69.

{70.

LBAR.



Items. Clear



();71.

72.

string



[] filename



= Directory.



GetFiles



(path);73.

74.

foreach



(string



r in



file)75.

LBAR.



Items. Add



(ar);76.

}77.

}78.

}







Windows-pong, with two buttons and a radio buton:

1.

2.

using



System;3.

using



System.



Collections. Generic;4.

using



System.



Drawing;5.

using



System.



Windows. Forms;6.

7.

namespace



MiniJuegoPelota

8.

{9.

/ / / <summary>10.

/ / / Description of MainForm.11.

/ / / </ Summary>12.

public



partial



class



MainForm: Form

13.

{14.

/ / 1 -> -1 Right -> Left15.

/ / 1 -> Down -1 -> Top16.

private int



dx =



-1,



d =



1;17.

18.

/ / Variables q contains the last key pressed by some shovel19.

/ / For q the rebound took place in one or another direcion20.

/ / 'U' (up) -> top 'd' (down) -> down21.

private char



d1, d2;

22.

23.

24.

public



MainForm



()25.

{26.

InitializeComponent



();27.

}28.

29.

Timer1Tick



void



(object



sender, EventArgs



e)30.

{31.

/ / Move the "ball"32.

ball.



Left



+ = dx;

33.

ball.



Top



+ = dy;

34.

35.

/ / For the movement of the ball36.

37.

/ / Dx = pelota.Location.X> = this.ClientSize.Width?



-1: Dx;38.

/ / Dx = pelota.Location.X == 0?



1: dx;39.

40.

if



(ball.



Location. X



+



18>



=



this.



ClientSize.



Width)41.

{42.

timer1.



Enabled



=



false;43.

MessageBox.



Show



("The



player 1",



"Congratulations");44.

}45.

if



(ball.



Location. X



==



0)46.

{47.

timer1.



Enabled



=



false;48.

MessageBox.



Show



("The



player 2",



"Congratulations");49.

}50.

51.

/ / If it hits the bottom or the menu52.

dy = ball.



Location. Y



+



50>



=



this.



ClientSize. Width?



-1:



d;

53.

dy = ball.



Location. Y



==



25? 1:



d;

54.

55.

/ / If it hits the pala156.

if



(Left



== ball. pala1.



Left



+ pala1.



Width)57.

{58.

if



(ball.



Top>



pala1.



Top



& & ball.



Top



<pala1.



Top



+ pala1.



Height)59.

{60.

dx =



1



/ / We make fence to the right61.

/ / And function of the last key pressed up or down62.

d = d1 ==



'u'?



-1: 1;63.

}64.

}65.

66.

/ / If it hits the pala267.

if



(Left



== ball. pala2.



Left



- pala2.



Width)68.

{69.

if



(ball.



Top>



pala2.



Top



& & ball.



Top



<pala2.



Top



+ pala2.



Height)70.

{71.

dx =



-1



/ / We make fence to the left72.

/ / And function of the last key pressed up or down73.

d = d2 ==



'u'?



-1: 1;74.

}75.

}76.

77.

}78.

79.

80.

81.

/ / To move the paddle 1 A = up, Z = down82.

/ / To move the paddle 2 K = high, M = down83.

MainFormKeyPress



void



(object



sender, KeyPressEventArgs



e)84.

{85.

switch



(Char.



ToUpper



(e.



KeyChar))86.

{87.

case



'A':



/ / The pala188.

pala1.



Top



-=



10;89.

if



(pala1.



Top



<25)



pala1.



Top



=



25;90.

d1 =



'u';91.

break;92.

case



'Z':93.

pala1.



Top



+ =



10;94.

if



(Top



+ pala1 pala1..



Height>



=



this.



ClientSize.



Height)



pala1.



Top



=



this.



ClientSize. Height



- pala1.



Height;95.

d1 =



'd';96.

break;97.

98.

case



'K':



/ / The pala299.

pala2.



Top



-=



10;100.

if



(pala2.



Top



<25)



pala2.



Top



=



25;101.

d2 =



'u';102.

break;103.

104.

case



'M':105.

pala2.



Top



+ =



10;106.

if



(Top



+ pala2 pala2..



Height>



=



this.



ClientSize.



Height)



pala2.



Top



=



this.



ClientSize. Height



- pala2.



Height;107.

d2 =



'd';108.

break;109.

}110.

}111.

112.

113.

/ / Menu options114.

NuevoToolStripMenuItemClick



void



(object



sender, EventArgs



e)115.

{116.

timer1.



Enabled



=



true;117.

ball.



Left



=



154;118.

ball.



Top



=



134;119.

}120.

121.

ContrrolesToolStripMenuItemClick



void



(object



sender, EventArgs



e)122.

{123.

MessageBox.



Show



("Press



A and K keys to move up and Z and M keys to lower pallets respective players 1 and 2",



"Controls");124.

}125.

126.

SalirToolStripMenuItemClick



void



(object



sender, EventArgs



e)127.

{128.

Application.



Exit



();129.

}130.

}131.

}

























Well, that's all, I hope they serve ..

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Premium Wordpress Themes