VB 2010 Graphics

Rumporheum 1 Reputation point
2021-02-07T22:23:21.617+00:00

I'm learning VB 2010 programming, using windows 10. I've added a windows form and added the following imports:

Imports System
Imports System.Drawing.Drawing2D

And these Dims:

Public Class MyForm

Dim myGraphics As Graphics = Me.CreateGraphics
Dim myPen As Pen

In the form load event is this code:

Private sub MyForm_load(...

    myPen = New Pen(Drawing.Color.Blue, 15)
    myGraphics.DrawLine(myPen, 50, 50, 50, 5)

When ran, nothing appears on the form. What am I doing wrong?

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,756 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ken Tucker 5,856 Reputation points
    2021-02-07T23:55:56.383+00:00

    Drawing in the form load event is the wrong event to use. You should be doing your drawing in the Paint event This way when the form needs to be repainted your graphics will be there

    Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
        Dim myPen = New Pen(Drawing.Color.Blue, 15)
        e.Graphics.DrawLine(myPen, 50, 50, 50, 5)
    End Sub
    
    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.