Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 4 Advanced Topics Devi, Erick, Reddy. » Declare dynamic component, an example: ˃Create a button Button newBtn = new Button(); ˃Create an array.

Similar presentations


Presentation on theme: "Lecture 4 Advanced Topics Devi, Erick, Reddy. » Declare dynamic component, an example: ˃Create a button Button newBtn = new Button(); ˃Create an array."— Presentation transcript:

1 Lecture 4 Advanced Topics Devi, Erick, Reddy

2

3 » Declare dynamic component, an example: ˃Create a button Button newBtn = new Button(); ˃Create an array of to-be-filled-with-buttons Button[] newBtn = new Button[4];

4 » Set Properties: newBtn[0] = new Button(); newBtn[0].Text = "halo"; newBtn[0].Left = 0; this.Controls.Add(newBtn[0]);

5 » Add event for dynamic component: newBtn[0].Click += new EventHandler(oke); void oke(object sender, EventArgs e) { Button xx= (Button) sender; MessageBox.Show(xx.Text ); }

6

7 » Windows Forms provides two events that occur when a user presses a keyboard key and one event when a user releases a keyboard key: ˃The KeyDown event occurs onceKeyDown ˃The KeyPress event, which can occur multiple times when a user holds down the same key.KeyPress ˃The KeyUp event occurs once when a user releases a key.KeyUp

8 public Form1() { InitializeComponent(); this.KeyPress += new KeyPressEventHandler(Form1_KeyPress); } private void Form1_KeyPress(object sender, KeyPressEventArgs e) { MessageBox.Show(e.KeyChar.ToString()); } Ubah KeyPreview menjadi true, agar event ditangkap oleh Form

9 private void TextBox1_KeyDown(object sender, KeyEventArgs e) { switch (e.KeyCode) { case Keys.Up: //Do stuff break; case Keys.Down: //Do stuff break; case Keys.Left: //Do stuff break; case Keys.Right: //Do stuff break; }

10

11 Random rnd = new Random(); » Create a number between 1 and 12 int month = rnd.Next(1, 13); » Create a number between 1 and 6 (inclusive) int dice = rnd.Next(1, 7); » Create a number between 0 and 51 (inclusive) int card = rnd.Next(52);

12 » Starts raising the Elapsed event by setting Enabled to true.ElapsedEnabled Timer1.Start » Gets or sets the interval at which to raise the Elapsed event.Elapsed Timer1.Interval » Stops raising the Elapsed event by setting Enabled to false.ElapsedEnabled Timer1.Stop

13 System.Timers.Timer aTimer = new System.Timers.Timer(); » Hook up the Elapsed event for the timer. aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent); » Set the Interval to 2 seconds (2000 milliseconds). aTimer.Interval = 2000; aTimer.Enabled = true;

14 Make a simple Arkanoid Game using buttons like this figure:


Download ppt "Lecture 4 Advanced Topics Devi, Erick, Reddy. » Declare dynamic component, an example: ˃Create a button Button newBtn = new Button(); ˃Create an array."

Similar presentations


Ads by Google