dhj Posted March 4, 2005 Posted March 4, 2005 In one form of my VB.NET Windows application, I have a picturebox. I need to draw a line (using known coordinates) on picturebox when the form is loading. Following is the code I wrote in my Form_Load event. Private Sub frm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load PictureBox1.Refresh() Dim g As Graphics = CType( PictureBox1, PictureBox).CreateGraphics Dim myPen As New Pen(Color.Red) myPen.Width = 2 g.DrawLine(myPen, 0, 0, 100, 100) myPen.Dispose() g.Dispose() End Sub But it's not displaying the Line when the Form is loading. I cannot figure out why. Thank you Quote
Talyrond Posted March 4, 2005 Posted March 4, 2005 I tried you code and as you say it does not work, I suspect this is because you are using the load event to draw and the timing is such that the line is rubbed out before the form is properly loaded. You should do all you drawing in the paint event so: Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint Dim myPen As New Pen(Color.Red) myPen.Width = 2 e.Graphics.DrawLine(myPen, 0, 0, 100, 100) myPen.Dispose() End Sub Notice I am using the graphics object supplied by the paint event, do not use CreateGraphics! Hope this helps Quote
Leaders snarfblam Posted March 4, 2005 Leaders Posted March 4, 2005 Quite right, the .Load event is called immediately BEFORE the form is shown for the first time. Any drawing you do before the form is shown will never show up. Also, FYI, the pen constructor has an overload like this (color As System.Drawing.Color, width As Single). Quote [sIGPIC]e[/sIGPIC]
dhj Posted March 7, 2005 Author Posted March 7, 2005 Thank you very much... It works with paint method. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.