Solved

Drawing a triangle in a picture box

Posted on 2007-03-26
7
1,247 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

919 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