Key Related Events in C# .Net

Key Related Events

  1. Key Press
  2. Key down
  3. Key Up

 

          Difference in between Key press and Key down:

  • In key press, logic will be executed Before displaying a character
  • In key down, logic will be executed After displaying a character
  • Key board character are divided in to two types :-

 

Screenshot_70

 

 

Example developing a Ball and Bat game:

Screenshot_71

 

 

  • Open WFAP
  • Place a Label control arrrow properties arrrow

Back  Color = black

Auto Size  = false

Location  = 202, 440

Left,    top

  • Place a Picture Box Control arrrow Properties arrrow

Image = Select an image

Size mode  = Stretch image

Location  = 13, 200

Left,    top

  • Place a Timer with Enabled = true;
  • Place one more Timer with Enabled = false
  • Code for Form1_Keydown event

{

Int I = (int) e.  keycode;

//Message Box. Show (I + “”);

If ((I = = 37)  && Label1.left > 0 )

Label 1.left = Label1.left -3;

If ((I = = 39)  && Label1.left < this. Width - 100 )

Label 1.left = Label1.left +3;

}

  • Code for Timer 1 _ Tick Event

{

Picture Box. Top = Picture Box 1. Top – 15;

If (Picture Box. Top  < 0)

{

Timer 1. Enabled = false;

Timer 1. Enabled = true;

}

  • Code for Timer 2 _ Tick Event

{

Picture Box1. Top = Picture Box 1. Top +5;

Picture Box2. Left = Picture Box 1. Left +1;

If  (Picture Box1. Top = Label1.Top &&

Picture Box1.left  >Label1.left &&

Picture Box1. Left  < Label1.right )

{

Timer 1.enabled = true;

Timer 2.enabled = false;

}

 

Screenshot_72

 

 

2.Every item will be identified with unique index number which tarts always from zero.

 

Screenshot_73

 

  1. Como box allows to select only one item at a time
  2. List box allows to select multiple items also.

 

Common Properties and methods of list box and combo box:- 

Properties:-

 

  1. Items arrrowused to add items at design time
  2. Selected item
  3. Selected Index

 

Methods: -  

Screenshot_74

 

  1. Combo box1. Items Add (String)
  2. Combo box1. Items Insert(index, String)
  3. Combo box1. Items Remove(String)
  4. Combo box1. Items Remove(index)
  5. Combo box1. Items clear();
  6. Combo box1. Items Index of (String)
  7. Combo box1. Items Countà(property)
  8. Combo box1. Items Get Enumerator ();  

 

Example on combo box :-

A program to change the shape of  a FORM

 

Screenshot_75

 

Name space arrrowSystem Drawing . Drawing 2 D

Screenshot_76

  • Open WFAP
  • Place a combo box arrrowproperties arrrow Items = add

 

  • Using System . Drawing . Drawing 2 D;
  • Code for combo box 1 _ Selected Index changed Event

 

Graphics path g = new Graphics path ();

Int I = combo box 1. Selected index;

If  (I = = 0) //circle

  1. Add Ellipse (10, 10, 150, 150) ;

else if (i ==1 ) //ellipse

  1. Add Ellipse (10, 10, 150, 400) ;

else //triangle

{

  1. Add Line (10, 10, 200, 300) ;
  2. Add Line (200, 300, 100, 150) ;
  3. Add Line (100, 150, 10,10) ;

}

Screenshot_78