4 Replies - 1654 Views - Last Post: 25 March 2007 - 11:02 AM Rate Topic: -----

#1 orangeslide8   User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 203
  • Joined: 29-December 06

2d program problems with keychar

Posted 24 March 2007 - 03:32 PM

ok so im making a 2d shooting game with stick figures. i have the whole drawing the stick figure down but when i use the form1_keypress sub and write
if e.keychar = "a" then msgbox("it worked") 
' just used that to show you something was rong with the keychar thing


it doesn't work
heres my code
Dim graphics As Graphics
	' blue guys body coordinates
	Dim bluebodyx, bluebodyy, bluebodyx2, bluebodyy2 As Integer
	' blue guys head coordinates
	Dim blueheadx, blueheady, blueheadwidth, blueheadheight As Integer
	' blue guys arm coordinates
	Dim bluearmx, bluearmy, bluearmx2, bluearmy2 As Integer
	Dim bluearm2x, bluearm2y, bluearm2x2, bluearm2y2 As Integer
	' blue guys leg coordinates
	Dim bluelegx, bluelegy, bluelegx2, bluelegy2 As Integer
	Dim blueleg2x, blueleg2y, blueleg2x2, blueleg2y2 As Integer
	' blue guys gun coordinates
	Dim blueguntopx, blueguntopy, bluegunbottomx, bluegunbottomy As Integer
	Const guntopwidth = 20
	Const guntopheight = 5
	Const gunbottomwidth = 5
	Const gunbottomheight = 10
	Dim ground = 640
	Dim onground As Boolean
	Private Sub start_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles start.Click
		start.Visible = False
		graphics = Me.CreateGraphics()
		graphics.Clear(Color.Black)
		' set body places
		bluebodyx = Me.Width / 7 - 90
		bluebodyy = Me.Height / 2
		bluebodyx2 = bluebodyx
		bluebodyy2 = bluebodyy + 50


any help would be great

Is This A Good Question/Topic? 0
  • +

Replies To: 2d program problems with keychar

#2 Jayman   User is offline

  • Student of Life
  • member icon

Reputation: 423
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: 2d program problems with keychar

Posted 24 March 2007 - 04:44 PM

For some reason when you try to use the KeyPress event of the form, in this manner, and there are objects on your form, then it does not work.

The only time I have ever got it to work is if the form is completely devoid of objects. Which ultimately defeats the purpose, since you will always have objects on the form.
Was This Post Helpful? 0
  • +
  • -

#3 RobC   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 24-March 07

Re: 2d program problems with keychar

Posted 24 March 2007 - 07:59 PM

I am new to this site.
There seems to be a lot of flippin VB.NET stuff in this Forum ?

I am assuming that you are using VB6 ?

If you click on the Form in the IDE, and press F4, there is a Property called KeyPreview.
Set this to True.
And then place your code in the KeyPress event of the Form.

The problem you describe is because that property is False by default. The reason your code works (when there are no other controls taking the focus). is because the focus then goes to the Form.
Using the Form's KeyPreview and events avoids that problem




View Postjayman9, on 24 Mar, 2007 - 04:44 PM, said:

For some reason when you try to use the KeyPress event of the form, in this manner, and there are objects on your form, then it does not work.

The only time I have ever got it to work is if the form is completely devoid of objects. Which ultimately defeats the purpose, since you will always have objects on the form.

This post has been edited by RobC: 24 March 2007 - 08:03 PM

Was This Post Helpful? 0
  • +
  • -

#4 Jayman   User is offline

  • Student of Life
  • member icon

Reputation: 423
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: 2d program problems with keychar

Posted 24 March 2007 - 09:42 PM

That is an excellent suggestion RobC and it works in VB.Net, too. :)

I will have to file that lit tidbit away for future use.

Thank you for your advice and welcome to the Dream in Code community.
Was This Post Helpful? 0
  • +
  • -

#5 orangeslide8   User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 203
  • Joined: 29-December 06

Re: 2d program problems with keychar

Posted 25 March 2007 - 11:02 AM

thnx a bunch that fixed it
john
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1