Solved

Drawing to a panel in c#

Posted on 2009-04-14
7
877 Views
Last Modified: 2013-12-17
I have a form with a panel on it. In this panel there are 2 picture boxes. Autoscroll is on so you can scroll to see the images. I tried drawing a line connecting these pictures, however when i scroll to one picture and then scroll back the line has disappeared...... How do i keep it so that the line stays in this position???
This is using visual studio .NET 7.1
0
Comment
Question by:mike_pro
  • 3
  • 2
  • 2
7 Comments
 
LVL 16

Expert Comment

by:CuteBug
ID: 24139326
Override the OnPaint() method of the Panel and put your line drawing code there...
0
 
LVL 16

Expert Comment

by:CuteBug
ID: 24139345
Each time you scroll, the panel is repainted... thats why the line disappears...
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24139586
Just to clarify CuteBug's suggestion...use the graphics supplied for you in the Paint() event via "e.Graphics".

Do NOT use this.CreateGraphics() in the Paint() event.

A simple example:
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            int x = pictureBox1.Location.X + pictureBox1.Width / 2;
            e.Graphics.DrawLine(Pens.Black, x, pictureBox1.Location.Y, x, pictureBox2.Location.Y);
        }

Open in new window

PanelPaintEvent.jpg
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

Author Comment

by:mike_pro
ID: 24139867
Thanks. i tried this and i seemed to draw the lines on the form, but i want them drawn to a panel...
If you can show me for the simple example i have now included then i will be able to apply the concept to my program. Take a look at the code i have added, i have not altered this in any way, it is all generated when designing the form. It is just a panel with picture within scrolling range. Say i want to draw just a vertical line just off the screen to the right that when you scroll to the right it will then be visible. What code would i have to add to achieve this?

Thanks for your help
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
 
namespace WindowsApplication13
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.PictureBox pictureBox1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
 
		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
 
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}
 
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
 
		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.panel1 = new System.Windows.Forms.Panel();
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.panel1.SuspendLayout();
			this.SuspendLayout();
			// 
			// panel1
			// 
			this.panel1.AutoScroll = true;
			this.panel1.Controls.Add(this.pictureBox1);
			this.panel1.Location = new System.Drawing.Point(24, 16);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(240, 232);
			this.panel1.TabIndex = 0;
			// 
			// pictureBox1
			// 
			this.pictureBox1.Location = new System.Drawing.Point(300, 300);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(80, 64);
			this.pictureBox1.TabIndex = 0;
			this.pictureBox1.TabStop = false;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(292, 273);
			this.Controls.Add(this.panel1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.panel1.ResumeLayout(false);
			this.ResumeLayout(false);
 
		}
		#endregion
 
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}
	}
}

Open in new window

0
 
LVL 16

Accepted Solution

by:
CuteBug earned 125 total points
ID: 24144331
you have to add

this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(panel1_Paint);

Now use the code provided by Idle_Mind.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24144561
"Say i want to draw just a vertical line just off the screen to the right that when you scroll to the right it will then be visible."

The AutoScroll will only allow you to scroll where an actual control exists.  If you want a line off to the right somewhere then you could consider placing a 1 pixel wide PictureBox with a single Border set.  You can place an additional no border PictureBox further to the right to make a "margin".
0
 

Author Comment

by:mike_pro
ID: 24156594
Thanks guys!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

856 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