Draw line on panel

How can I draw a line on panel

Page 1 of 1

2 Replies - 15670 Views - Last Post: 21 August 2009 - 02:20 PM Rate Topic: -----

#1 mgroses  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 31-July 09

Draw line on panel

Posted 21 August 2009 - 07:18 AM

Hi!

I have tried to solve this problem myself but I started programing only one months ago and I guess this problem is over me.

I wrote a small application. Yesterday I send a message about it (topic: XY cordinates). I try to explain shortly again:

I have a button and a panel on my form. Firstly, I want to take some coordinates from MS Access. With this coordinates, I want to put some pictures on panel and I want to draw line between center of panel and my points. My code under button_click:

private void button1_Click(object sender, EventArgs e)
		{
			TakeValues();

			PictureBox PictureBox2 = new PictureBox();
			int x1 = panel1.Width / 2;
			int y1 = panel1.Height / 2;
			PictureBox2.Location = new Point(x1, y1);
			PictureBox2.Size = new System.Drawing.Size(50, 30);
			PictureBox2.SizeMode = PictureBoxSizeMode.CenterImage;
			panel1.Controls.Add(PictureBox2);

			PictureBox2.Image = Image.FromFile(@"C:\warehouse.bmp");

		   
			for (int i = 0; i < MaxRows; i++)
			{
			   PictureBox PictureBox1 = new PictureBox();
			   PictureBox1.Image = Image.FromFile(@"C:\retailer.bmp");
				int x = coordinates[i, 0];
				int y = coordinates[i, 1];

				PictureBox1.Size = new System.Drawing.Size(25, 25);
				PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
				PictureBox1.Location = new Point(x1+x, y1-y);
				panel1.Controls.Add(PictureBox1);

				Pen myPen = new System.Drawing.Pen(Color.Black, 1);
				Graphics formGraphics;
				formGraphics = panel1.CreateGraphics();
				formGraphics.DrawLine(myPen, x1, y1, x1+x, y1-y);
				
				myPen.Dispose();
				formGraphics.Dispose();
				
			}
			
		}




with TakeValues() method I take the coordinates from database and I put them into coordinates array.

The lines are drawn but I minimize the form and maximize again, all lines disappear. I have tried solve problem but I couldn't. I guess I have to use panel1_paint event, but I don't know how. I said I am too new in C#.

Do you have any idea about it?

Mod Edit:
Please use full code tags :code: not just brackets [ ]

Is This A Good Question/Topic? 0
  • +

Replies To: Draw line on panel

#2 mgroses  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 31-July 09

Re: Draw line on panel

Posted 21 August 2009 - 01:55 PM

I modified my code but problem go on (drawn lines disappear). Maybe this codes give some idea:


private void panel1_Paint(object sender, PaintEventArgs e)
		{
			int x1 = panel1.Width / 2;
			int y1 = panel1.Height / 2;
			PictureBox PictureBox2 = new PictureBox();

			PictureBox2.Location = new Point(x1, y1);
			PictureBox2.Size = new System.Drawing.Size(50, 30);
			PictureBox2.SizeMode = PictureBoxSizeMode.CenterImage;
			panel1.Controls.Add(PictureBox2);

			PictureBox2.Image = Image.FromFile(@"C:\warehouse.bmp");

			if (drawLine == true)
			{
				Pen myPen = new Pen(Color.Black, 1);

				for (int i = 0; i < MaxRows; i++)
				{

					PictureBox PictureBox1 = new PictureBox();
					PictureBox1.Image = Image.FromFile(@"C:\retailer.bmp");
					int x = coordinates[i, 0];
					int y = coordinates[i, 1];

					PictureBox1.Size = new System.Drawing.Size(25, 25);
					PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
					PictureBox1.Location = new Point(x1 + x, y1 - y);
					panel1.Controls.Add(PictureBox1);

					Graphics g = e.Graphics;	 
					g.DrawLine(myPen, x1, y1, x1+x, y1-y);
				   
				}
				myPen.Dispose();
				drawLine = false;
			}   
		}



		private void button1_Click(object sender, EventArgs e)
		{
			TakeValues();
				
			drawLine = true;
			panel1.Invalidate();
		}




TakeValues() method is for taking values from database.

This post has been edited by mgroses: 21 August 2009 - 01:57 PM

Was This Post Helpful? 0
  • +
  • -

#3 debjit625  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 51
  • View blog
  • Posts: 446
  • Joined: 06-September 08

Re: Draw line on panel

Posted 21 August 2009 - 02:20 PM

drawLine = false;


In panel1_paint() event stopes the drawing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1