GUI: tabs and timers and focus


A TabControl is a container that simplifies an interface by dividing functionality into selectable tabs.

A timer allows you to generate an event periodically without user input.

      T A B S
. tabControl class (tab)
TabControl ========== - a container much like a panel or groupbox - allows you to have many "virtual forms" - controls on each tab are as if they were in group box (radio buttons) - controls on each tab can access and change control on every other tab - code for all tabs in the same class NOTE: ==== Using a tab control gives you 1. a tab control (tabControl1) 2. a tab page for every tab (tabPage1,2,3...) The tabControl properties effect the entire control while the page properties affect ONLY that tab. The properties on the tabControl and tabPage are DIFFERENT!
TabControl Properties ===================== TabPages -------- a collection listing the different tabs Can access each tabPage and its properties here Alignment --------- TOP BOTTOM RIGHT LEFT Where the tab appears Appearance ---------- Changes how the tab looks SelectedTab (not in properties window) ----------- Current tab tabControl1.SelectedTab = tabPage2; TabPage Properties ===================== Text BackColor ForeColor Font

      T I M E R S
. about timers (tmr)
Name property 3 letter prefix is tmr... Use timers for things to happen automatically without user input Does NOT show up in GUI interface - No visual part of the object Timer icon place in COMPONENT TRAY
Enabled property to "turn on" the timer default is False(OFF) can set at design time OR run-time Interval is how often it "ticks" in MS (milliseconds) An interval of zero means timer will not fire 100 MS = 1/10 of a second 500 milliseconds = 1/2 second 3000 milliseconds is 3 seconds
ONLY ONE EVENT => Tick ============== Place code in this event handler to be executed when timer fires.
EXAMPLE ======= int x = 0; // class level - LIFETIME!! // Simple counter. Counts once per Interval. private void timer1_Tick(object sender, EventArgs e) { label1.Text = (++x).ToString(); // write EACH TICK }

. using a timer to move a control
Example: ======== Can "move" a control by changing its "Left" or "Top" property // move a picture of a car picCar.Left += 25; //move RIGHT picCar.Left -= 25; //move LEFT picCar.Top += 25; //move DOWN picCar.Top -= 25; //move UP Only ONE Event // move a picture of a car private void timer1_Tick(object sender, EventArgs e) { picCar.Left += 25; // move RIGHT }
// move car 25 units right // set timer to RANDOM interval private void tmrMove1_Tick(object sender, EventArgs e) { Random rand = new Random(); int randomNum; picCar.Left += 25; // move RIGHT randomNum = rand.Next(0, 11); tmrMove.Interval = randomNum; }
Turn "OFF" by setting tmrMove.Enabled = False; by setting tmrMove.Interval = 0; Change by setting tmrMove.Interval = n; where 'n' is milliseconds

. whack a button using a timer
// set timer to RANDOM interval private void tmrMove1_Tick(object sender, EventArgs e) { Random rand = new Random(); int randomNum; //Change Visibilty, Interval and Position if( btnMole.Visible == false ) { // reset interval randomNum = rand.Next(20, 201); tmrMove.Interval = randomNum; // Move randomNum = rand.Next(-30, 30); btnMole.Left += randomNum; randomNum = rand.Next(-30, 30); btnMole.Top += randomNum; // pop up out of hole btnMole.Visible = true; } else { // reset interval LONGER randomNum = rand.Next(50, 500); tmrMove.Interval = randomNum; // back in the hole btnMole.Visible = false; } } private void btnMole_Click(object sender, EventArgs e) { // HIT!! lblHit.Text += 1; } private void frmMain_Click(object sender, EventArgs e) { // MISS!! lblMiss.Text += 1; }

. 3-letter control prefixes
Prefix Control ====== ====================== frm Form (except Form1) btn Button txt TextBox lbl Label pic PictureBox chk CheckBox rad RadioButton (option) grp GroupBox tmr Timer msk MaskedTextBox lst ListBox cbo ComboBox hsb HorizontalScrollBar vsb VerticalScrollBar