[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Need to update drawing after a key is pressed

Posted on 2004-11-29
8
Medium Priority
?
216 Views
Last Modified: 2010-04-23
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?
0
Comment
Question by:Todd_Anderson
  • 4
  • 4
8 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12698048
After you update the x and y coordinates, issue a

    Me.Refresh()

which will cause your Paint event to be fired.

~IM
0
 

Author Comment

by:Todd_Anderson
ID: 12698274
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
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 200 total points
ID: 12698312
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Todd_Anderson
ID: 12698420
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12698476
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
 

Author Comment

by:Todd_Anderson
ID: 12698668
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12698686
Set the background color using the BackColor property at design time.  Then you shouldn't need the Clear() call.
0
 

Author Comment

by:Todd_Anderson
ID: 12698713
Awesome!  It works great.  Thanks a lot Idle Mind.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question