Solved

Drawing a triangle in a picture box

Posted on 2007-03-26
7
1,213 Views
Last Modified: 2012-06-21
Hi there

I am trying to draw a triangle in a picture box.  I don't seem to get an error but nothing is displaying in the picture box.  Does anyone know what I am doing wrong?  I have tried pic.refresh and that doesn't seem to do anything
     
  Dim poly(3) As PointF ', NumCoords As Long, hBrush As Long, hRgn As Long

                poly(1).X = 0
                poly(1).Y = 0
                poly(2).X = 0
                poly(2).Y = pic.Height
                poly(3).X = pic.Width
                poly(3).Y = pic.Height

            'Get PictureBox's Graphics Object
            Dim gr As Graphics = pic.CreateGraphics
            Dim pn As Pen = Pens.AliceBlue
            gr.DrawPolygon(pn, poly)
            pic.Refresh()
            gr.Dispose()

Thanks for your help in advance
0
Comment
Question by:DavidGreenfield
  • 2
  • 2
7 Comments
 
LVL 7

Accepted Solution

by:
dctuck earned 250 total points
ID: 18792030
It's because the image gets repainted with its default contents whenever a refresh occurs.

If you put the code inside the Paint event for your pic it should work. e.g:

Private Sub pic_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pic.Paint
     Dim poly(3) As PointF ', NumCoords As Long, hBrush As Long, hRgn As Long
    poly(1).X = 0
    poly(1).Y = 0
    poly(2).X = 0
    poly(2).Y = pic.Height
    poly(3).X = pic.Width
    poly(3).Y = pic.Height
    'Get PictureBox's Graphics Object
    Dim gr As Graphics = pic.CreateGraphics
    Dim pn As Pen = Pens.AliceBlue
    gr.DrawPolygon(pn, poly)
    pic.Refresh()
    gr.Dispose()
End Sub
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 250 total points
ID: 18793475
Lol dctuck...it isn't really any better to place the code into the Paint() event when you left the call to CreateGraphics() in there...

...also, the call to pic.Refresh() in the Paint() event will lead to an infinite refreshing loop and alot of flicker...

DavidGreenfield...Arrays start at index 0 (zero) in .Net.

(On a side note...with the default picturebox background color, Pens.AliceBlue was barely visible on my system so I switched to Pens.Black)

Use the graphics supplied in the "e" parameter of the Paint() event.  If you move the Point Array to the form level then you can update the points dynamically and cause the triangle to change.

For example:

Public Class Form1

    Private poly() As Point

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        poly = New Point() { _
            New Point(0, 0), _
            New Point(0, pic.Height - 1), _
            New Point(pic.Width - 1, pic.Height - 1)}
    End Sub

    Private Sub pic_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pic.Paint
        e.Graphics.DrawPolygon(Pens.Black, poly)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        poly(0).X = 25
        poly(2) = New Point(pic.Width / 2, pic.Height / 2)
        pic.Refresh()
    End Sub

End Class
0
 
LVL 7

Expert Comment

by:dctuck
ID: 18793509
Idle_Mind - I know, I forgot to adjust that: I checked the code in VB and then went and copy and pasted the original code without changing it.... d'oh!
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18793530
Hehe...I can't say I've never done a similar thing.   =)
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now