Solved

Drawing a triangle in a picture box

Posted on 2007-03-26
7
1,385 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
[X]
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
  • 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 86

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 86

Expert Comment

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

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

628 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