[Main]

Tab Order, Control Focus, and Alt-Keys

The tab order is used to set the order by which control gets the input focus when the tab key is pressed. The tab order can be set by assigning values into the tabindex property of visible controls. A control with a tabindex of zero will be the first control that will receive the focus. The control with a tabindex
of 1 will receive the next focus. Each control will progressively receive the focus. When the last control receives the focus, the focus is sent back to the first control again. When an object receives the focus, the GotFocus event handler of the control is executed. When focus is lost from a control, the LostFocus event
is triggered. The Lostfocus event can be used to test validation of entered data.  If you would like to forcibly move focus to another object, you can use the SetFocus method of the control that you would like to move to.  The label can not have a focus.

Project 1: In this program, the tab order of four controls is set. One of the controls has code attached to the GotFocus and LostFocus events. One of the buttons automatically moves the focus into one of the text boxes.

Properties:

Component Property Value
Form name frmFocus
caption Text Focus Example
Command Button name cmdOne
caption One
tabindex 0
Command name cmdTwo
caption Two
tabindex 1
Text Box name txtOne
text
tabindex 2
Text Box name txtTwo
text
tabindex 3
Label name lblOutput
caption


Code:

option explicit
private sub txtOne_GotFocus()
lblOutput.Caption = "It is I, TxtOne, who has the focus"
end sub

private sub txtOne_LostFocus()
lblOutput.caption = "Dont leave ... Come back... "
end sub

private sub cmdTwo_Click()
txtTwo.SetFocus ' Set Focus to text box
end sub

private sub cmdTwo_Click()
txtTwo.SetFocus ' Set Focus to text box
end sub

Alt Speed Keys:

To both move the focus to a control and execute the click event of the associated control, use an & character in the caption of the control adjacent to the character that you would like to use. Although labels cannot receive the focus, you can still set an alt speed character in the caption. When the alt speed
character is used in the label, the next control with the following tabindex after the label receives the focus. The click event of this control is also executed.

Project 2:
This program show you how to set the alt speed characters for a two buttons and a label.

Properties:

Component Property Value
Command Button name cmdOne
caption &One
tabindex 0
Label name lblTwo
caption &Two
tabindex 1
Command Button name cmdTwo
caption T&wo
tabindex 2
Label name lblOutput
caption

Code:

option explicit

private sub cmdOne_Click()
lblOutput.caption="You pressed One Button"
end sub

private sub cmdTwo_Click()
lblOutput.caption="You Pressed Two Button"
end sub


Project 3:

This program will ask the user to enter his age into a text box. The user can move to the next control only if he can enter a legal age of 10-50. Otherwise he cannot move out of the control.

Properties:

Component Property Value
Text Box name txtAge
text
Command Button name cmdExit
caption E&xit


Code:

option explicit
private sub txtAge_LostFocus()

if CInt(txtAge.caption)<10 then

txtAge.SetFocus

exit sub ' exit sub

endif

if Cint(txtAge.caption)>50 then

txtAge.SetFocus

exit sub ' exit sub

endif

end sub


private sub txtAge_KeyPress(keyascii as integer)

if Keyascii<asc("0") or keyascii>asc("9") then

keyascii=0 ' lose the character

beep ' beep

end if

endsub

private sub cmdExit_Click()

end ' exit program

end sub

 

Project 4:

Our next program will accept the temperature in degrees Celsius from the user and then calculate the Fahrenheit: F=(9/5)*C+32.

Properties:

Component Property Value
Form name frm
caption Celcius to Faherenheit
Text Box name txtCelcius
text
Label name lblFahren
caption
Command Button name cmdCalc
caption &Evaluate Fahrenheit


Code:

option explicit

private sub cmdCalc_click()

lblfahren.caption = (9.0/5.0) * Cint(txtcelcius.text)+32.0 ) & "F"

end sub

 

Assigment #1:

Write a metric-British units program that will inter-convert either distances or weights from British units and Metric units.  Distance are measured in miles or kilometers, and weights are measured either in pounds(lbs.) or kilograms. The conversion factors are  2.2lbs=1Kg and  1.6km=1mile.

 

Assignment #2:

Write a calculator program that will perform multiplication, division, addition and subtraction.  Take a look at the Calc.exe program in windows for some ideas.

copyrightę2001 Prof Devi - mdevi@comine.com, mdevi@liu.edu. All rights reserved