Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB6 OnFocus() 1

Status
Not open for further replies.
Joined
Jun 30, 2003
Messages
196
Location
GB
This is tricky to explain but ill give it a go.

Im developing a mobile phone user interface. when in the main menu i want to allow my user to navigate from one command button (or icon) to another, they can go left, right, up or down depending on direction of the key thye press. so bassically the form will open up and focus will be set to one specific icon, when they press left focus will move to the icon to the left of the starting position.

My question how do i implement this to move focus?
and when focus is moved to an new icon how do i change that icons (command button) properties say to increase it in size?

if more clarification is needed please ask.
 
This works on the number keypad with the numlock control off
To set the focus to a control you say (for example)
Enable Keypreview in form properties

Sub Form_KeyPress(KeyAscii as Integer)
'Test routine to see number = label1.caption=Keyascii

Select case KeyAscii

case 56
'up
UpButton.Setfocus

Case 52
'left
LeftButton.Setfocus

case 54
'right
RightButton.Setfocus

case 50
'down
DownButton.Setfocus

end select
end sub

Every key has a different number which is the ascii value.
You can also use sub form_keydown to get shift keys etc

 
ok but what if instead of the the keypad you wanted to develop four command buttons each one representing a direction e.g. buttonleft, buttonright, buttonup and buttondown. How would you produce the same functionality in the code you provided above but using these four command buttons instead?
 
It sounds like each icon needs a 'spatial awareness' - either it needs to know its x,y co-ordinate or it needs to know which icon gets focus when it is lost up, down, left or right.

The code above always sets focus to the same button/icon when you press Right, so that's not what you need. (I think)


Let's say your icons are an array (1..16)
Your form can have 4 'synchronised' arrays:

Dim arrUP (16) as integer
Dim arrDOWN (16) as integer
Dim arrLEFT (16) as integer
Dim arrRIGHT (16) as integer
Dim iCurrentIcon as integer

For each of the 17 buttons, set these up in an initialiser like so:

'For icon index 1
arrUP(1) = 0 ' zero means NO VALID BUTTON FROM HERE
arrDOWN(1) = 5 'if DOWN is pressed, move to icon 5
arrLEFT(1) = 0 'if LEFT is pressed, move nowhere
arrRIGHT(1) = 9 'if RIGHT is pressed, move to icon 9

..repeat for the other 15 icons


Then, in your 4 command buttons, you need code like this:

sub cmdRight_Click()
'iCurrenticon holds the one with the 'focus'

'where do we go when RIGHT is pressed?
if arrRIGHT(iCurrentIcon) > 0 then

'make the old icon look normal somehow
'SetState(iCurrentIcon,0)
iCurrenticon = arrRIGHT(iCurrentIcon)

'make the new icon look selected somehow
'SetState(iCurrentIcon,1)
end if
'

end sub


..recreate for the other 3 directional buttons


The SetState procedure might look like this:

Private Sub SetState(iWhichIcon as integer, iState as integer)

'increase the border width to indicate selected
'status

icon(iWhichIcon).borderwidth = 1 + iState

end sub



 
ok thanks that is great i am going to try to grapple with implementin this solution. Thanks for the great code.
 
Creat 4 indexed pushbuttons and have the index do the work
(by placing the first one on the form and copying the others from the first one)
Sub Command1_Click(Index as integer)
select case index
Case 0
'left (do whatever happens when you want to go left)
Case 1
'right etc
Case 2
'up
Case 3
'down
end select
end sub
 
hi jeffTullin

i have just been going over the solution you gave me a couple of weeks ago to a problem i had. One question

the solution depends upon knowing what the current button is that has focus, but when the code executes wont the same button always have focus last, what ever directional button was pressed? Do you see what i mean, or is this not a problem. I am just getting to the stage now that i am ready to implement this code you gave me so thats why i have only just figured this out. What do you think?
 
The variable iCurrentIcon is updated when the 'focus' is changed, but in fact is nothing to do with the 'real' focus.
It is simply an internal record of which icon is lit up.

If you amend this when the directional buttons are clicked, the buttons may have the 'real' focus, but the iCurrentIcon variable knows where the 'lit up' icon is.

If people can actually click on the icons too, then you need to have the click event of the icon set the iCurrentIcon variable to its own index.


ie

sub icon_Click(index as integer)
'record which was clicked last,
'regardless of where the windows focus is
'(after all, our icon may not actually be
' able to accept focus if TABSTOP is false)

call SetState(iCurrentIcon, 0)'unset the previous icon
iCurrentIcon = index
Call SetState(iCurrentIcon, 1)'light up the current one

end sub
 
oh ok thanks for the great advice really appreciated.
 
woops sorry one more question jeff, the first section of your code where the arrays are set up, should i place this in each sub for the directional buttons or in some global position where it can be seen by all the directional buttons so i dont have to repeat it every time. if so how do i do this, where do i write this section of code?
 
If this is a one-form application, put the dim statements right at the top of the code, under OPTION EXPLICIT, but outside of any subroutine.
Now they are 'global' to the whole form.

(Otherwise put them into a Module, declared as PUBLIC variables)

Put the initialisation code into the FORM_INITIALISE or FORM_LOAD screens.
Set the iCurrentIcon there too.

On the FORM_ACTIVATE, set the icon with the 'focus' to be highlighted.
 
i havent come across the fome initialise or form load screens where do i locate these
 
so for this to work the solution requires my icons to be called 1, 2, 3, 4, 5, and 6 etc etc.

and also i am getting a compile error when i type the following code:

SetIconState(iCurrentIcon,1)
 
forget those last two message i figured them out i was missing the keyword call and the number thing i hadnt set up a control array. but im sure i will still get stuck because i still havent finished yet so plaese be patient with me as i know ill have to ask you another question before i finish
 
Option Explicit

Dim arrUP(9) As Integer
Dim arrDOWN(9) As Integer
Dim arrLEFT(9) As Integer
Dim arrRight(9) As Integer
Dim iCurrentIcon As Integer

..........................................................
Private Sub cmdIcon_Click(Index As Integer)
'cmdIcon(Index).BackColor = &HC0C0C0
End Sub
............................................................
Private Sub FORM_INITIALISE()
'initialiser for icon index 1
arrUP(1) = 0 'zero means no valid button from here
arrDOWN(1) = 4 'if down pressed move to icon 4
arrLEFT(1) = 0 'zero means no valid button from here
arrRight(1) = 2 'if right is pressed move to icon 2

Dim iCurrentIcon As Integer
iCurrentIcon = cmdIcon(1)

End Sub

...........................................................
Private Sub cmdKeypadEnd_Click()
'Go back to idle screen
frmMainMenu.Hide
frmIdle.Show
End Sub
...........................................................
Public Sub SetIconState(iWhichIcon As Integer, iState As Integer)

'increase icon border width to indicate selected status

cmdIcon(iWhichIcon).BackColor = &HC0C0C0
End Sub
...........................................................
Private Sub cmdKeypadRight_Click()
'iCurrentIcon holds the one with focus


'where do we go when right is pressed

If arrRight(iCurrentIcon) > 0 Then

'make the old icon look normal somehow
Call SetIconState(iCurrentIcon, 0)
iCurrentIcon = arrRight(iCurrentIcon)

'make new icon look selected somehow
Call SetIconState(iCurrentIcon, 1)

End If


End Sub
...........................................................

The above code does not work, it does not support the functionality i want it to. As far as i can see it is the same as the code you gave me jeff can you see any reason why this is not working? if you need more details feel free to ask.
 
Your version of SetIconState ignores the state.
Every time it is called, it sets the backcolor to grey.
You need to test the iState value, and have it one color for 0, and another color for 1

 
yeah i understand but the icons are now set to red and in fact the SetIconState doesnt appear to work as when i run the program they do not get changed to grey. Any ideas?
 
Ok jeff new problem, i now got the selection to move from icon to icon changing the icon grey to red, and then back to grey. Thanks for all your help in that, but the new problem is, the first time i click one of the directional arrows nothin happens, it only starts to work when i click the directional arrow for a second time.
 
Try setting all the icons to green initially.
I suspect that the first time you hit the button, it may be changing grey to grey.

If that does not tell you what is happening, use the debugger and trace through the code.
 
i took your suggestion abvout changing the coulour but it does not change the situation, it still takes two clicks. Also if i just moved the selection to the right, then wanted to press down, the first time i press down the selection would still move to the right and only the second time i press down will the selection actually move down. I have changed the code slightly to accomidate the different states could you take a look at it and tell me if there is a problem with it causing this to happen.

Option Explicit

Dim arrUP(9) As Integer
Dim arrDOWN(9) As Integer
Dim arrLEFT(9) As Integer
Dim arrRIGHT(9) As Integer

Dim iCurrentIcon As Integer
............................................................
Private Sub cmdKeypadDown_Click()

'iCurrentIcon holds the one with focus


'where do we go when down is pressed

If arrDOWN(iCurrentIcon) > 0 Then

'make the old icon look normal somehow
Call SetIconState(iCurrentIcon, 0)
iCurrentIcon = arrDOWN(iCurrentIcon)

'make new icon look selected somehow
Call SetIconState(iCurrentIcon, 1)

End If
End Sub
.......................................................
Private Sub cmdKeypadLeft_Click()
'iCurrentIcon holds the one with focus


'where do we go when left is pressed

If arrLEFT(iCurrentIcon) > 0 Then

'make the old icon look normal somehow
Call SetIconState(iCurrentIcon, 0)
iCurrentIcon = arrLEFT(iCurrentIcon)

'make new icon look selected somehow
Call SetIconState(iCurrentIcon, 1)

End If
End Sub
...........................................................
Private Sub cmdKeypadUp_Click()
'iCurrentIcon holds the one with focus


'where do we go when up is pressed

If arrUP(iCurrentIcon) > 0 Then

'make the old icon look normal somehow
Call SetIconState(iCurrentIcon, 0)
iCurrentIcon = arrUP(iCurrentIcon)

'make new icon look selected somehow
Call SetIconState(iCurrentIcon, 1)

End If
End Sub
...........................................................
Private Sub FORM_Load()

'initialiser for icon index 1
arrUP(1) = 0 'zero means no valid button from here
arrDOWN(1) = 4 'if down pressed move to icon 4
arrLEFT(1) = 0 'zero means no valid button from here
arrRIGHT(1) = 2 'if right is pressed move to icon 2

'initialiser for icon index 2
arrUP(2) = 0
arrDOWN(2) = 5
arrLEFT(2) = 1
arrRIGHT(2) = 3

'initialiser for icon index 3
arrUP(3) = 0
arrDOWN(3) = 6
arrLEFT(3) = 2
arrRIGHT(3) = 1

'initialiser for icon index 4
arrUP(4) = 1
arrDOWN(4) = 7
arrLEFT(4) = 6
arrRIGHT(4) = 5

'initialiser for icon index 5
arrUP(5) = 2
arrDOWN(5) = 8
arrLEFT(5) = 6
arrRIGHT(5) = 5

'initialiser for icon index 6
arrUP(6) = 3
arrDOWN(6) = 9
arrLEFT(6) = 5
arrRIGHT(6) = 4

'initialiser for icon index 7
arrUP(7) = 4
arrDOWN(7) = 0
arrLEFT(7) = 9
arrRIGHT(7) = 8

'initialiser for icon index 8
arrUP(8) = 5
arrDOWN(8) = 0
arrLEFT(8) = 7
arrRIGHT(8) = 9

'initialiser for icon index 9
arrUP(9) = 6
arrDOWN(9) = 0
arrLEFT(9) = 8
arrRIGHT(9) = 7



iCurrentIcon = 5
cmdIcon(5).BackColor = &HC0&


End Sub

.......................................................
Private Sub cmdKeypadEnd_Click()
'Go back to idle screen
frmMainMenu.Hide
frmIdle.Show
End Sub
.......................................................
Public Sub SetIconState(iWhichIcon As Integer, iState As Integer)

'increase icon border width to indicate selected status
If iState = 0 Then

cmdIcon(iWhichIcon).BackColor = &HC0C0C0

Else

cmdIcon(iWhichIcon).BackColor = &HC0&

End If

End Sub
.........................................................
Private Sub cmdKeypadRight_Click()
'iCurrentIcon holds the one with focus


'where do we go when right is pressed

If arrRIGHT(iCurrentIcon) > 0 Then

'make the old icon look normal somehow
Call SetIconState(iCurrentIcon, 0)
iCurrentIcon = arrRIGHT(iCurrentIcon)

'make new icon look selected somehow
Call SetIconState(iCurrentIcon, 1)

End If


End Sub



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top