Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have Windows 7 (64Bit)

I have a form named Form1.

I also have a Procedure called DrawGid

VB
Public Sub DrawGrid()
        Dim g As Graphics = Me.CreateGraphics
        Dim XN, YN As Integer
        Dim midx, midy As Single
        Dim UnitX, UnitY, OffsetX, OffsetY As Single
        Dim P As New Pen(Color.Black, 4)

        XN = 3 : YN = 3
        OffsetX = 30 : OffsetY = 30
        XN = 3 : YN = 3

        With Me
            midx = (.Width / 2)
            midy = (.Height / 2)
            UnitX = (.Width - OffsetX) / (2 * XN)
            UnitY = (.Height - OffsetY) / (2 * YN)
        End With

        'Draw Horizondal Lines of the grid
        '---------------------------------
        g.DrawLine(Pens.Orchid, 0, midy, Me.Width, midy)
        For i = 1 To YN
            g.DrawLine(Pens.Black, 0, midy + UnitY * i, Me.Width, midy + UnitY * i)
            g.DrawLine(Pens.Black, 0, midy - UnitY * i, Me.Width, midy - UnitY * i)
        Next
        'Draw Vertical Lines of the grid
        '---------------------------------
        g.DrawLine(Pens.Orchid, midx, 0, midx, Me.Height)
        For i = 1 To XN
            g.DrawLine(Pens.PaleGoldenrod, midx + UnitX * i, 0, midx + UnitX * i, Me.Height)
            g.DrawLine(Pens.PaleGoldenrod, midx - UnitX * i, 0, midx - UnitX * i, Me.Height)
        Next
    End Sub
End Class


When I load Form1 I write the following code:


VB
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      DrawGrid()
  End Sub


When I run the programe no lines are drawn. Could anybody please
help me?
Posted
Updated 20-Jul-10 0:20am
v2

For this you'll have to override the forms 'OnPaint' method and add the drawing code there, like so:

VB
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)

        Dim xn, yn, i As Integer
        Dim midx, midy As Single
        Dim Unitx, UnitY, OffsetX, OffsetY As Single
        Dim p As New Pen(Color.Black, 4)
        xn = 3
        yn = 3
        OffsetX = 30
        OffsetY = 30
        With Me
            midx = (.Width / 2)
            midy = (.Height / 2)
            Unitx = (.Width - OffsetX) / (2 * xn)
            UnitY = (.Height - OffsetY) / (2 * yn)
        End With

        e.Graphics.DrawLine(Pens.Orchid, 0, midy, Me.Width, midy)
        For i = 1 To yn
            e.Graphics.DrawLine(Pens.Black, 0, midy + UnitY * i, Me.Width, midy + UnitY * i)
            e.Graphics.DrawLine(Pens.Black, 0, midy - UnitY * i, Me.Width, midy - UnitY * i)
        Next i
        e.Graphics.DrawLine(Pens.Orchid, midx, 0, midx, Me.Height)
        For i = 1 To xn
            e.Graphics.DrawLine(Pens.PaleGoldenrod, midx + Unitx * i, 0, midx + Unitx * i, Me.Height)
            e.Graphics.DrawLine(Pens.PaleGoldenrod, midx - Unitx * i, 0, midx - Unitx * i, Me.Height)
        Next i

    End Sub
 
Share this answer
 
Comments
MJK2010 20-Jul-10 16:48pm    
Thank you very much. You solve my problem. Exactly what I needed :)
Try using a picture box.

See: Drawing with Graphics in WinForms using VB[^]

Hope it helps.

Ahmed Djobby
 
Share this answer
 
v2
Comments
MJK2010 20-Jul-10 16:53pm    
Thank you very much for the help. The sites information is very enlightning.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900