Need to update drawing after a key is pressed

I have a simple program (Visual Basic.NET 2003) that draws a crosshair.  I need to be able to move the crosshair up, down, left and right when the user pushes the standard arrow keys on the keyboard.  I am drawing the crosshair in the Form1_Paint event using variables (x and y) with form-wide scope.  I am updating x and y in the Form1_KeyDown event.  Everything is working fine except that the drawing is not updating.  It updates correctly when I drag another window over it.

All this is as expected.  My question is, how do I refresh, updated or redraw my drawing at the end of my Form1_KeyDown event after I have incremented my x and y variables based on the user's input?
Todd_AndersonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
After you update the x and y coordinates, issue a

    Me.Refresh()

which will cause your Paint event to be fired.

~IM
0
Todd_AndersonAuthor Commented:
Thanks Idle Mind.  It works as I hoped except for one thing.  I am getting a lot of flashing each time the refresh occurs.  Quick bars of 20 or so horizontal lines show up briefly every few keystrokes.  Anyway to fix that?
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Try drawing in a Panel or PictureBox instead of directly on the form.  You would move your drawing code to the Paint event for the Panel or Picturebox and then issue a Panel1.Refresh() or PictureBox1.Refresh().

~IM
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Todd_AndersonAuthor Commented:
Thanks Idle Mind.  I'll give those two ideas a try.  What about manually erasing the crosshair and then redrwing it in the Form1_KeyDown event?
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
If your backcolor is the same everywhere then you could certainly erase it and then redraw it from the KeyDown event.

The only problem with that approach is that the crosshair won't be persistent then since it is not in the Paint event.  If you minimize the window or pass another window over your form, then the crosshair will be gone.

~IM
0
Todd_AndersonAuthor Commented:
Ah, very true.  I just did an experiment and it stopped the flashing.  I had a g.Clear(Color.Black) line in my Form1_Paint event.  When I commented it out the flashing stopped.  Now I just need to figure out how to do the clear and set the background to black outside of the Paint event.  I'll also try your other suggestions.
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Set the background color using the BackColor property at design time.  Then you shouldn't need the Clear() call.
0
Todd_AndersonAuthor Commented:
Awesome!  It works great.  Thanks a lot Idle Mind.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.