Very, very simple VB drawing example

A guy in the newsgroups who is new to .NET, coming from VB6, is having a hard time drawing on a Form. The problem with VB6 was it had the Shape controls which provided a crutch for a developer to never actually understand how drawing worked, and now that crutch has been removed.

Drawing is simple – you need a Graphics object to paint on. The easiest way to get that for your Form is to just override OnPaint – you’ll get it as an input parameter. The other nice effect is that you don’t have to do anything other than refresh the Form to get your code to run.

Now normally I don’t like to just give out the answer – no one learns much that way – but he seems to genuinely have spent several hours trying to get this, so I figured I’d throw him a bone so he doesn’t get frustrated and give up altogether. So the goal is to draw a “crosshair” on the Form that the user can move around with the D-Pad on the device. Again, this is a very, very basic example – it took me roughly 15 minutes to do (and that’s becasue my VB is very rusty).

Create a device WinForms app. Select the Form and make sure KeyPreview is True. Then add this code: