drawing on form load
drawing on form load
drawing on form load
drawing on form load
drawing on form load
drawing on form load drawing on form load drawing on form load drawing on form load drawing on form load drawing on form load drawing on form load drawing on form load
drawing on form load drawing on form load
drawing on form load
Go Back  Xtreme Visual Basic Talk > > > drawing on form load


Reply
 
Thread Tools Display Modes
  #1  
Old 02-01-2013, 10:00 AM
Ktech22 Ktech22 is offline
Newcomer
 
Join Date: Jun 2012
Posts: 20
Default drawing on form load


hi I am trying to draw object in the picture box on form load event
the code I am creating more object in picturebox but just to make it simpler
I will just use a circle
Code:
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim gra As Graphics = PictureBox1.CreateGraphics
        gra.FillEllipse(Brushes.Black, 50, 50, 50, 50)
    End Sub
Now I know the code works as I put the code behind the button and then it works. I tried changing the handler to shown and activated but it still doesn't
seen to work. please help
thankyou
KTech22
Reply With Quote
  #2  
Old 02-01-2013, 03:44 PM
AtmaWeapon's Avatar
AtmaWeapondrawing on form load AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

That's not really the best way to draw to a control. It will work temporarily, but won't stick around.

The way Windows Forms controls are drawn is simple. Windows raises the Paint event when it's time to draw the control and provides a Graphics object for the control to use. The control erases itself, does its drawing, then calls your event handlers for the Paint event if there are any. If you participate in this cycle, then any time the control is redrawn you will get a chance to add your drawing.

When you use CreateGraphics(), it's like you're making a one-time scribble on top of whatever the control has drawn. The next time a Paint event happens, the control erases everything (including what you drew) and draws itself. Your scribbles are gone. You can do them again after the control redraws itself, but the only way to know when that happens is to handle the Paint event. Which already provides you with a Graphics object.

In 10 years of working with Windows Forms, I have never found a good reason to call a control's CreateGraphics() method. Consider any examples that use it suspect. Just because code's on the internet doesn't mean it's smart.

When it comes to picture boxes, you have two choices for drawing.
  1. Handle the picture box's Paint event, and use the Graphics object from the PaintEventArgs parameter to do the drawing.
  2. Draw to a bitmap that the control is displaying.
#1 is the easiest to get to from where you are. Set up an event handler for the picture box. (You likely cannot copy and paste this and have it work. Make the event handler yourself and type this code into it.)
Code:
Sub YourPictureBox_Paint(sender As Object, e As PaintEventArgs) Handles YourPictureBox.Paint
    e.Graphics.FillEllipse(Brushes.Black, 50, 50, 50, 50)
End Sub
#2 requires a little bit more knowledge because you have to manage a reference to a bitmap. I guess you can sort of get away without it but I like being explicit.
Code:
Class YourForm
    Inherits Form

    Private _displayBitmap As Bitmap

    ' Call this from a button click or something.
    Sub Draw()
        _displayBitmap = New Bitmap(' Use parameters for the size and such here)
        Using g As Graphics = Graphics.FromImage(_displayBitmap)
            g.FillEllipse(Brushes.Black, 50, 50, 50, 50)
        End Using

        yourPictureBox.Image = _displayBitmap
    End Sub

End Class
The picture box is designed to display this image, so it'll look right all the time.

There's not really a "better" method, though if you're doing #1 and not using the Image property then there's really no reason to use a PictureBox control.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #3  
Old 02-02-2013, 09:14 AM
Ktech22 Ktech22 is offline
Newcomer
 
Join Date: Jun 2012
Posts: 20
Default

thanks a lot Atmaweapon. this code first option works perfectly for me
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
drawing on form load
drawing on form load
drawing on form load drawing on form load
drawing on form load
drawing on form load
drawing on form load drawing on form load drawing on form load drawing on form load drawing on form load drawing on form load drawing on form load
drawing on form load
drawing on form load
 
drawing on form load
drawing on form load
 
-->