Gentlemen I cannot figure out the following:
I have a loop in - Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Pressing a key I am checking what key was pressed and come back to the loop.
Thanks for help
11 2979
Can't understand what really you want to know. If you want to check which key was pressed it can be done in keypress of the form. - Private Sub Form_KeyPress(KeyAscii As Integer)
-
msgbox KeyAscii
-
End Sub
...Pressing a key I am checking what key was pressed and come back to the loop.
Can you please try to explain in more detail what the problem is? I didn't get it either.
Sorry gentlemen, my fault.
I am trying to create a "game" for my 6 year old son to learn keyboard letters.
I have a loop with an image of a letter that flies all over the screen until a key of keyboard is pressed to check if the pressed key is correct one.
So flying letter: - Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
-
-
'flying baloon
-
Dim y_Max As Integer = Me.Size.Height()
-
Dim x_Max As Integer = Me.Size.Width()
-
Dim x As Integer = x_Max / 2 - x_Max / 4
-
Dim y As Integer = y_Max - 150
-
Dim letter(26) As Image
-
Dim count As Integer
-
Dim a As Integer = 1
-
Dim b As Integer = -1
-
'Dim z As Integer
-
Dim KeyAscii As Integer
-
Dim KeyChar As Integer
-
-
letter(1) = My.Resources.E
-
letter(2) = My.Resources.I
-
letter(3) = My.Resources.O
-
letter(4) = My.Resources.Q
-
letter(5) = My.Resources.R
-
letter(6) = My.Resources.T
-
letter(7) = My.Resources.U
-
letter(8) = My.Resources.W
-
letter(9) = My.Resources.Y
-
-
For count = 1 To 8
-
y = y + b
-
x = x + a
-
If x >= x_Max - 150 Then a = -1
-
If x <= 0 Then a = 1
-
If y >= y_Max - 150 Then b = -1
-
If y <= 0 Then b = 1
-
-
Call Form_press(KeyAscii)
-
Call Form1_KeyPress(a, AscW)
-
e.Graphics.DrawImage(letter(count), x, y, 150, 150)
-
For i = 0 To 1000000
-
-
Next
-
e.Graphics.DrawImage(My.Resources.Empty, x, y, 151, 151)
-
-
Next
-
-
End Sub
Now I need get input from keyboard, check if it is correct and return to the loop displaying another letter or if it was wrong input to come back to the same place where I left the loop.
And another question: my flying image is fine displayed but time to time it is flickering, how to make it smooth?
Thanks masters.
BTW I use VB 2008 express
Nice idea, Andreig. :)
However, I would question one or two things about your method of implementing the idea.
For a start, I don't believe the Paint method is the correct place to do this. It probably should be driven by a Timer instead.
Secondly, if that For i = 0 To 1000000 is being used to slow things down, it's a really bad idea. These sort of delay loops (I think it's referred to as "software timing" or something) used to be quite common. But these days they are generally more trouble than they're worth. Each time you upgrade the computer, or possibly even when a Windows Update is installed, the timing can change. That "one to a million" loop might suddenly start taking ten times as long. Or in some cases, a "smarter" compiler might see that the loop does nothing and simply skip over it, reducing the delay time to a millionth of a second or something.
These days, you should either invoke a "real" (that is, hardware-related) delay mechanism. Or for preference, if you only want to do something ten times a second for example, you just use a timer to call it ten time a second. You see, another reason why delay loops like this are considered poor form is that they can tie up the CPU doing huge amounts of nothing.
Of course, these are all things you probably should consider if working professionally as a programmer. All that aside though, I suppose what you really want now is just some way to get it working. From what you've said, the animation seems to be working (though a bit flickery), but you're not sure how to detect the keypress. Since I use a much older version of VB, the technical details are likely to be quite different. But I can offer some general suggestions. - Set the form to handle all keypresses itself before allowing any controls to receive them. In VB6, this is done by setting the form's KeyPreview property to True. Don't know about your version.
- I would create a form-level (or global) variable to hold the details of the key that was pressed. In VB6 I would just use an Integer or something to hold the ASCII value. Not sure about your version - I believe the .Net versions of VB deal with the keyboard somewhat differently.
- In the form's KeyPress (or KeyDown) event, record what key was pressed by storing the details in your new variable.
- During your animation, periodically check the value in your new variable, to see whether something has been recorded there since you last checked.
- Note that in VB6 (not sure about VB 2008), the delay loop would have to include a DoEvents statement. This statement instructs VB to stop hogging the processor, and allow Windows to go handle other important functions such as reading the keyboard, updating the display and so on.
Well, I hope that's some help, anyway.
Thanks Killer for your description and I will do it for sure.
Can you please give me examples how to fix this problem with pressed keys?
so: - In what place of my code I have to put "Me.KeyPreview = True"
- Creating:
Private Sub Form1_KeyPress(ByVal KeyAscii As Integer)
End Sub - "In the form's KeyPress (or KeyDown) event, record what key was pressed by storing the details in your new variable" - sorry, have noooo clue.
- "During your animation, periodically check the value in your new variable, to see whether something has been recorded there since you last checked."
- you mean something like:
call Form1_KeyPress(KeyAscii )? - "to include a DoEvents statement" in what place and how to include it?
You see I am a total idiot in it.
Actually I am a software tester and want to put my nose a little bit in programming, thanks.
Thanks Killer for your description and I will do it for sure.
Can you please give me examples how to fix this problem with pressed keys? ...
Being inexperienced doesn't make you an idiot. :)
Let's see, to take your points in order... - I wouldn't do it in your code at all. Just set the property yourself in the IDE (Integrated Design Environment).
- VB will create the appropriate event procedure "skeleton" for you, so you can just put in the actual code. The declaration you showed (Form1_KeyPress) looks to me like VB6 format, and is probably not correct for VB 2008. You need to consult the documentation for how to create an event procedure. It'll be pretty simple.
- All I meant was, whatever details are provided to the event procedure to indicate the key pressed (in VB6, this would be KeyAscii As Integer) you should copy into a form-level variable of the same type. You might want to read up on variable "scope". It's a very fundamental and important concept.
- No, you're not getting the whole event-driven concept I think. You don't need to call the keypress event code, it happens when the user presses a key.
What you are likely to have here is a couple of more or less independent routines - one running each time a Paint event happens (or better still, each time a timer fires) and another which runs each time the user presses a key. The KeyPress code will just make a note of it in a variable, so the other routine can check it.
At the point where you want to know whether a key has been pressed, just look at the variable which was set in point 3. - I don't know whether DoEvents is still relevant or not, in VB 2008. The point is that, in VB6 at least, if your code is looping around doing stuff continuously, it doesn't allow time for Windows to go and do all its regular housekeeping stuff like checking the keyboard, updating the display and so on. That's why things sometimes stop half-drawn or take a while to appear on-screen, if the system is busy. Executing a DoEvents statement simply tells Windows "ok, I'll take a breather for a moment while you go check on things". It might be best to ignore this for now, and we can go into it further, if it turns out to be necessary.
Well, gotta go! It's after 7pm, on New Year's Eve! :-D
Talk to you next year...
Hi
Try 'DoEvents'
I haven't followed your code closely because your programming capability seems to surpass my own but when I had a similar problem calling 'DoEvents' did it for me. (Using Access and VBA)
I had a loop that was reading the COM port for digital data and wanted it to be interrupted to 'Stop' or 'Save Reading' etc. Putting the DoEvents as the last line before the end of the loop allowed other button presses to be detected.
Best wishes
Sorry Killer42, I did not notice you had already suggested this
Since you have the express version of VB 2008, did you also get the updated MSDN library?
It is very useful, as long as you understand the terminology they use (quite different form VB6).
I believe it has a large section on keypress events.
As killer pointed. It will be better to use the draw command in key press event.
You have to include System.Drawing in your Class
And - 'In Class Module Decleration part
-
private Pen blackPen = new Pen(Color.Black, 2)
-
'pnlDraw is the frame or panel control from tool box
-
private Graphic g = new pnlDraw.CreateGraphics()
-
-
'Move All your code in form_Paint() to Form_KeyDown() or form_keyPress()
-
g.DrawImage(letter(count), x, y, 150, 150)
...Sorry Killer42, I did not notice you had already suggested this
No problem. It's always good to get multiple viewpoints.
As killer pointed. It will be better to use the draw command in key press event.
Actually, I believe I recommended doing the drawing from a timer, not from the keypress.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Mike Maxwell |
last post by:
<vent> I see there has been traffic over the years on this gripe, so just
let me vent my frustration, and add another reason why 'exit' (or possibly
exit() or quit() or halt() or some such) should...
|
by: Aaron Ackerman |
last post by:
What is the sytax for exiting a for loop in C#?
|
by: orekin |
last post by:
Hi There
I have been trying to come to grips with Application.Run(),
Application.Exit() and the Message Pump and I would really appreciate
some feedback on the following questions .. There are...
|
by: PeterZ |
last post by:
Hi,
Back to basics!
My understanding is that the only way to exit a For-Next loop prematurely is
with the 'break' keyword. How are you supposed to do that if you're inside
a Switch...
|
by: Rudy Ray Moore |
last post by:
Visual studio c++ 7.1/.net/2003 got me in the habit of pressing ESC after
performing a search using CTRL-F.
I've always thought the extra step (pressing ESC) was dumb, but could not
come up with...
|
by: Bob Day |
last post by:
Using VS 2003, VB.net...
I am confused about the Application.Exit method, where the help states "This
method does not force the application to exit." Aside from the naming
confusion, how do I...
|
by: Tina |
last post by:
In vb.net to get out of a sub or a function we can say Exit Sub. How can I
get out of a void method in C#. I can't find that documented anywhere.
Thanks,
T
|
by: vostrixos |
last post by:
hi all.I'm new in this site so forgive me if i'm doing anything rong.
<Also forgive me for my english>
I working on a project that i don't want to look like an access aplication.
So i've made...
|
by: mathieu |
last post by:
Hi there,
I am trying to reuse a piece of code that was designed as an
application. The code is covered with 'exit' calls. I would like to
reuse it as a library. For that I renamed the 'main'...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |