Bahar Jalali

Bahar Jalali

  • NA
  • 185
  • 305k

add several panels into a picturebox in runtime and try to move panels every place in picturebox

Dec 6 2009 6:29 AM
hi

i want add panels to a picturebox in runtime and i want can move them to every place in picturebox

i wirte this code but when i put a panel control and test the code it run ok but when i add panel in run time it seems that some code not performance, for example

 private void panel1_MouseDown(object sender, MouseEventArgs e)
{
Panel mypic = (Panel)sender;
mypic.DoDragDrop(mypic.BackgroundImage, DragDropEffects.Move);
panel1.Location = loc;
}

or like this that in design mode i set that on the control events;

now i put my page code completely

how can i add panels in run time and set this code for every panel??

my code is here for 1 panel:
 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ContrloCircuit
{
public partial class Form1 : Form
{
protected Point loc;
private PictureBox selectpic;
protected Panel panel1 = new Panel();

public Form1()
{
InitializeComponent();
Image img = Image.FromFile(@"E:dog.jpg");
this.picBox.AllowDrop = true;
this.picBox.DragDrop += new System.Windows.Forms.DragEventHandler(this.picBox_DragDrop);
this.picBox.DragEnter += new System.Windows.Forms.DragEventHandler(this.picBox_DragEnter);
}

private void pictureBox1_Click(object sender, EventArgs e)
{


picBox.Controls.Add(panel1);
panel1.BackgroundImage = pictureBox1.Image;
panel1.Height = pictureBox1.Height;
panel1.Width = pictureBox1.Width;
panel1.AllowDrop = true;

// panel1.BackgroundImage = pictureBox1.Image;
// panel1.AllowDrop = true;

}

private void picBox_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap))
{
e.Effect = DragDropEffects.Move;
}
else
{
e.Effect = DragDropEffects.None;
}
}

private void picBox_DragDrop(object sender, DragEventArgs e)
{
// PictureBox picbox = (PictureBox)sender;
// Graphics g = picbox.CreateGraphics();
// g.DrawImage((Image)e.Data.GetData(DataFormats.Bitmap), myloc);
// label1.Text = myloc.ToString();
PictureBox destination = (PictureBox)sender;

destination.Image = (Bitmap)e.Data.GetData(typeof(Bitmap));
}

private void panel1_MouseDown(object sender, MouseEventArgs e)
{
Panel mypic = (Panel)sender;
mypic.DoDragDrop(mypic.BackgroundImage, DragDropEffects.Move);
panel1.Location = loc;
}

private void panel1_DragOver(object sender, DragEventArgs e)
{
// myloc.X = e.X;
// myloc.Y = e.Y;
panel1.Location = loc;
}


private void timer1_Tick(object sender, EventArgs e)
{
loc = Cursor.Position;
label1.Text = loc.ToString();
}

private void panel1_DragEnter(object sender, DragEventArgs e)
{
panel1.Location = loc;
}
}
}