VB Problem (1 Viewer)

Olivia

New Member
Joined
May 23, 2003
Messages
22
Location
Sydney
I am doing a major project on VB and have lots of problems.

How do I program an object to move using those keyboard keys?

Thanks
 

del

Member
Joined
Jul 8, 2002
Messages
412
Gender
Undisclosed
HSC
N/A
Use the KeyPress method of a Form using selection statements to check which key is pressed.....

example:

Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
KeyAscii = 10
Label1.Left = Label1.Left + 100
End If
End Sub

(sorry no identation there)

with the above code, everytime the Enter key is hit, the label moves further to the right......


that what you mean? kinda like that?
 
Last edited:

Olivia

New Member
Joined
May 23, 2003
Messages
22
Location
Sydney
Thanks but what about moving an object in different direction using the arrow keys?

do I need an array to do so?
 

del

Member
Joined
Jul 8, 2002
Messages
412
Gender
Undisclosed
HSC
N/A
u need to distinguish between which arrow key was pressed.....
algorithm possibly like:

CASEWHERE(arrow key pressed)
CASE left key:
move whatever up
CASE right key:
move whatever right
.
.
.
.
END CASEWHERE
 

del

Member
Joined
Jul 8, 2002
Messages
412
Gender
Undisclosed
HSC
N/A
serious?

I guess personal preference. i prefer them instead of using heaps of IF statements....

keeps my indenting more manageable.....especially when it comes to printing
 

Fiona

Member
Joined
Apr 28, 2003
Messages
135
Location
Sydney
Hey Oivia ;)

I think I have some on my home drive that you might find useful - it's from those sheets that we did in year 10 computing. Also, the filing cabinet outside the room should still have them in there.

Fiona :)
 

Fiona

Member
Joined
Apr 28, 2003
Messages
135
Location
Sydney
Originally posted by del
Use the KeyPress method of a Form using selection statements to check which key is pressed.....

example:

Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
KeyAscii = 10
Label1.Left = Label1.Left + 100
End If
End Sub

...
With something like that, 13 is the code for the enter key, kind of thing, I think...

SO, what you could try and do, is use that sort of thing but instead of using 13 for the enter key, find out what the numbers are for the arrow keys, and do it like that :)

But I could be completely wrong.
 

del

Member
Joined
Jul 8, 2002
Messages
412
Gender
Undisclosed
HSC
N/A
you're spot on...... :)

i didn't use arrow keys in the example. dunno them of the top of my head
 

Fiona

Member
Joined
Apr 28, 2003
Messages
135
Location
Sydney
Ooooh yeah :D

*dance of joy!*

Ok well Olivia, here we have a site that lists the KeyAscii codes for VB... Although it does not appear to have the arrow keys.

According to this site, the arrow keys do not have Ascii codes for the KeyAscii function.

The KeyDown event should be used only when you need to detect a function key or arrow key or the the shift, alt or ctrl keys (these keys do not have ASCII codes and therefore do not generate KeyPress events. KeyPreview does not work for the KeyDown event, which in this example is therefore passed to the picturebox.
So you should use KeyDown, rather than KeyPress....

I don't know if I'm helping or not :)
 

neester

Member
Joined
Feb 22, 2003
Messages
30
Location
South Sydney
Just A Note On CASE Functions
DEL Stated before that you can use:
CASEWHERE

I prefer to use:

Code:
Select Case (INPUT)
   Case 1, 4, 60 to 90
      'Some Commang
   Case 99 to 102
      'Another Command
   Case is < 0
      'Yet Another Command
End Select
 

sunny

meh.
Joined
Jul 7, 2002
Messages
5,350
Gender
Male
HSC
2002
The one Del used was for psuedocode.

There is no CASEWHERE in VB. CASEWHERE is the preferred selection structure for psuedocode. Using Select Case for pseudocode might lose you marks.
 

Olivia

New Member
Joined
May 23, 2003
Messages
22
Location
Sydney
I came across some problems in my project: in VB

1. set an image to move randomly around the screen?

2. stop the timer after 1 min, ie. set the timer

3. set an array so that it can count
 

Fosweb

I could be your Doctor...
Joined
Jun 20, 2003
Messages
594
Location
UNSW. Still.
Gender
Male
HSC
2003
Sorry, but is this what you can't do?

If so:

1. Needs more explanation/details... but look up the .left and .top properties of controls. Is the randomness based on timing or direction? There are a number of options/methods depending on what you want...

2. Easy. Set the interval of the timer to 1000 (this is equal to 1 sec in milli(whatever)secs.
So you need to:
wherever you start the timer: timer.enabled = true
then in the timer code: (i.e. by double clicking on the timer)
i.e. Call your timer "timer" and paste this into code window...

Code:
private sub timer()
timCount = timCount + 1
If timCount = 60 then timer.enabled = false
end sub
Thats it. All a timer does is increment the code inside if every interval. So. Every 1000 millisecs, this timer adds one to a variable called timCount and then stops when this gets to 60.

3. Count what? Why use an array if all you want to do is count?
 

Fosweb

I could be your Doctor...
Joined
Jun 20, 2003
Messages
594
Location
UNSW. Still.
Gender
Male
HSC
2003
New Line character is Chr(13) + Chr(10)
[This is the same as typing vbCrLf or vbNewLine (however - the difference between these two is platform based... Ie: vbCrLf is not platform specific - vbNewLine is)]

Best way to find out character codes... If you cant be bothered looking anywhere on net/msdn is to make a new form, and in the keypress action of the form, type Debug.Print keyascii
Then the intermediate window will show you ascii code of any key pressed...
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top