Download presentation
Presentation is loading. Please wait.
Published byMitchell Stevenson Modified over 9 years ago
1
Lecture 4 Advanced Topics Devi, Erick, Reddy
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 ); }
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; }
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:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.