From: Shawn on 23 Feb 2010 09:46 Here's a solution to your problem. I assumed 'NodesLimit' was supposed to be some constant that decides how many picture boxes there were. Most importantly, I noticed you merged the MouseUp, MouseDown, and MouseMove event handlers. They're separate for a reason. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; namespace PicBoxApp { public partial class Form1 : Form { bool isDragging = false; int currentX; int currentY; Control dragThing; public const int NODES_LIMIT = 8; private System.Windows.Forms.PictureBox[] PicBoxes; Rectangle dropRect = new Rectangle(180, 180, 60, 60); private PictureBox myPictureBox; public Form1() { InitializeComponent(); CenterToScreen(); PicBoxes = new System.Windows.Forms.PictureBox[8]; myPictureBox = new PictureBox(); myPictureBox.SizeMode = PictureBoxSizeMode.StretchImage; myPictureBox.Location = new System.Drawing.Point(64, 32); myPictureBox.Size = new System.Drawing.Size(60, 80); myPictureBox.Image = new Bitmap(@"D:\Documents and Settings \All Users\Documents\My Pictures\Sample Pictures\winter.jpg"); //initialise each picture box for (int i = 0; i < NODES_LIMIT; i++) { //allocate the picture box PicBoxes[i] = new PictureBox(); PicBoxes[i].Name = i.ToString(); //set up the initial properties PicBoxes[i].Top = (i + 1) * 30; PicBoxes[i].Left = (i + 1) * 20; PicBoxes[i].Width = 60; PicBoxes[i].Height = 80; PicBoxes[i].SizeMode = PictureBoxSizeMode.StretchImage; PicBoxes[i].Image = Image.FromFile(@"D:\Documents and Settings \All Users\Documents\My Pictures\Sample Pictures\winter.jpg"); //we are putting the index into the Tag property so we can access this like any other array PicBoxes[i].Tag = i; //Here we are wiring this picture boxes click to our new common click handler. PicBoxes[i].Click += new System.EventHandler(ClickHandler); PicBoxes[i].MouseDown += new System.Windows.Forms.MouseEventHandler(MouseDown); PicBoxes[i].MouseUp += new System.Windows.Forms.MouseEventHandler(MouseUp); PicBoxes[i].MouseMove += new System.Windows.Forms.MouseEventHandler(MouseMove); } this.Controls.AddRange(PicBoxes); Controls.Add(myPictureBox); } //This is the new click handling function for our array of boxes public void ClickHandler(Object sender, System.EventArgs e) { } private void MouseDown(object sender, MouseEventArgs e) { dragThing = ((Control)sender); currentX = e.X; currentY = e.Y; isDragging = true; dragThing.BringToFront(); } private void MouseMove(object sender, MouseEventArgs e) { if (isDragging) { dragThing.Top = dragThing.Top + (e.Y - currentY); dragThing.Left = dragThing.Left + (e.X - currentX); } } private void MouseUp(object sender, MouseEventArgs e) { isDragging = false; } private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { } } } weird0 wrote: how to drag and drop an array of picturebox 19-Oct-07 I have been successful in making an array of picturebox and writing event handlers for them, after hours of hard work. I want to drag and drop all of the pictureboxes. And they dont.!!!! :-( Can somebody help me this? What code should i write in the event handlers so that i make all the picture boxes to drag and drop ? using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; namespace PicBoxApp { public partial class Form1 : Form { private System.Windows.Forms.PictureBox[] PicBoxes; Rectangle dropRect = new Rectangle(180, 180, 60, 60); private PictureBox myPictureBox; public Form1() { InitializeComponent(); CenterToScreen(); PicBoxes = new System.Windows.Forms.PictureBox[8]; myPictureBox = new PictureBox(); myPictureBox.SizeMode = PictureBoxSizeMode.StretchImage; myPictureBox.Location = new System.Drawing.Point(64, 32); myPictureBox.Size = new System.Drawing.Size(50, 50); myPictureBox.Image = new Bitmap(@"D:\Documents and Settings \All Users\Documents\My Pictures\Sample Pictures\winter.jpg"); //myPictureBox.MouseDown += new MouseEventHandler(myPictureBox_MouseDown); //myPictureBox.MouseUp += new MouseEventHandler(myPictureBox_MouseUp); //myPictureBox.MouseMove += new MouseEventHandler(myPictureBox_MouseMove); //myPictureBox.Cursor = Cursors.Hand; //initialise each picture box for (i = 0; i < NodesLimit; i++) { //allocate the picture box PicBoxes[i] = new PictureBox(); PicBoxes[i].Name = i.ToString(); //set up the initial properties PicBoxes[i].Top = (i + 1) * 30; PicBoxes[i].Left = (i + 1) * 20; PicBoxes[i].Width = 150; PicBoxes[i].Height = 100; PicBoxes[i].SizeMode = PictureBoxSizeMode.StretchImage; PicBoxes[i].Image = Image.FromFile(@"D:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures \Winter.jpg"); //we are putting the index into the Tag property so we can access this like any other array PicBoxes[i].Tag = i; //Here we are wiring this picture boxes click to our new common click handler. PicBoxes[i].Click += new System.EventHandler(ClickHandler); PicBoxes[i].MouseDown += new System.Windows.Forms.MouseEventHandler(MouseDown); PicBoxes[i].MouseDown += new System.Windows.Forms.MouseEventHandler(MouseUp); PicBoxes[i].MouseDown += new System.Windows.Forms.MouseEventHandler(MouseMove); } this.Controls.AddRange(PicBoxes); Controls.Add(myPictureBox); } //This is the new click handling function for our array of boxes public void ClickHandler(Object sender, System.EventArgs e) { } private void MouseDown(object sender, MouseEventArgs e) { } private void MouseMove(object sender, MouseEventArgs e) { } private void MouseUp(object sender, MouseEventArgs e) { } private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { } } } Here are the links, where i took help: http://pages.cpsc.ucalgary.ca/~carman/481/examples/csharp/controlarra... http://www.java2s.com/Code/CSharp/GUI-Windows-Form/DraganddropthePict... Need help Regards Previous Posts In This Thread: On Friday, October 19, 2007 3:13 PM weird0 wrote: how to drag and drop an array of picturebox I have been successful in making an array of picturebox and writing event handlers for them, after hours of hard work. I want to drag and drop all of the pictureboxes. And they dont.!!!! :-( Can somebody help me this? What code should i write in the event handlers so that i make all the picture boxes to drag and drop ? using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; namespace PicBoxApp { public partial class Form1 : Form { private System.Windows.Forms.PictureBox[] PicBoxes; Rectangle dropRect = new Rectangle(180, 180, 60, 60); private PictureBox myPictureBox; public Form1() { InitializeComponent(); CenterToScreen(); PicBoxes = new System.Windows.Forms.PictureBox[8]; myPictureBox = new PictureBox(); myPictureBox.SizeMode = PictureBoxSizeMode.StretchImage; myPictureBox.Location = new System.Drawing.Point(64, 32); myPictureBox.Size = new System.Drawing.Size(50, 50); myPictureBox.Image = new Bitmap(@"D:\Documents and Settings \All Users\Documents\My Pictures\Sample Pictures\winter.jpg"); //myPictureBox.MouseDown += new MouseEventHandler(myPictureBox_MouseDown); //myPictureBox.MouseUp += new MouseEventHandler(myPictureBox_MouseUp); //myPictureBox.MouseMove += new MouseEventHandler(myPictureBox_MouseMove); //myPictureBox.Cursor = Cursors.Hand; //initialise each picture box for (i = 0; i < NodesLimit; i++) { //allocate the picture box PicBoxes[i] = new PictureBox(); PicBoxes[i].Name = i.ToString(); //set up the initial properties PicBoxes[i].Top = (i + 1) * 30; PicBoxes[i].Left = (i + 1) * 20; PicBoxes[i].Width = 150; PicBoxes[i].Height = 100; PicBoxes[i].SizeMode = PictureBoxSizeMode.StretchImage; PicBoxes[i].Image = Image.FromFile(@"D:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures \Winter.jpg"); //we are putting the index into the Tag property so we can access this like any other array PicBoxes[i].Tag = i; //Here we are wiring this picture boxes click to our new common click handler. PicBoxes[i].Click += new System.EventHandler(ClickHandler); PicBoxes[i].MouseDown += new System.Windows.Forms.MouseEventHandler(MouseDown); PicBoxes[i].MouseDown += new System.Windows.Forms.MouseEventHandler(MouseUp); PicBoxes[i].MouseDown += new System.Windows.Forms.MouseEventHandler(MouseMove); } this.Controls.AddRange(PicBoxes); Controls.Add(myPictureBox); } //This is the new click handling function for our array of boxes public void ClickHandler(Object sender, System.EventArgs e) { } private void MouseDown(object sender, MouseEventArgs e) { } private void MouseMove(object sender, MouseEventArgs e) { } private void MouseUp(object sender, MouseEventArgs e) { } private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { } } } Here are the links, where i took help: http://pages.cpsc.ucalgary.ca/~carman/481/examples/csharp/controlarra... http://www.java2s.com/Code/CSharp/GUI-Windows-Form/DraganddropthePict... Need help Regards Submitted via EggHeadCafe - Software Developer Portal of Choice EggHeadCafe Chat Chaos in Silverlight Released Today http://www.eggheadcafe.com/tutorials/aspnet/325ea67e-d6c4-4811-b096-54f31bdede5d/eggheadcafe-chat-chaos-in.aspx
|
Pages: 1 Prev: How to get the connection string? Next: Collection UserControls/Windows Controls |