Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Drawing a triangle in a picture box

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

829 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