By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,689 Members | 1,759 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,689 IT Pros & Developers. It's quick & easy.

C# - App - How to Drag and Drop from the desktop to a Panel in a Form?

P: 11
Hello,

If someone can help plz.

I have searched everything and all I can find is how to drag and drop within a form from panel to panel.

What I need to do is Drag an image from the desktop or from an open folder to a panel on my form.

This will be done for a few images and then on Buttonclik I need to save each image in its own folder.

Any help is much appreciated.

Here is what I have on the events - for d&d

This allows me to drag and drop within the form but trying to drag from desktop to panel1 doesn't work.
Expand|Select|Wrap|Line Numbers
  1.  private void panel_DragDrop(object sender, DragEventArgs e)
  2.             {
  3.                 //target control will accept data here 
  4.                 Panel destination = (Panel)sender;
  5.                 destination.BackgroundImage = (Bitmap)e.Data.GetData(typeof(Bitmap));
  6.  
  7.             }
  8.  
  9.  
  10.  
  11.  
  12.  private void panel_DragEnter(object sender, DragEventArgs e)
  13.             {
  14.                 //As we are interested in Image data only we will check this as follows
  15.                 if (e.Data.GetDataPresent(typeof(Bitmap)))
  16.                 {
  17.                     e.Effect = DragDropEffects.Copy;
  18.                 }
  19.                 else
  20.                 {
  21.                     e.Effect = DragDropEffects.None;
  22.                 }
  23.  
  24.             }
  25.  
  26.  
  27.  private void panel_MouseDown(object sender, MouseEventArgs e)
  28.             {
  29.                 //we will pass the data that user wants to drag DoDragDrop method is used for holding data
  30.                 //DoDragDrop accepts two paramete first paramter is data(image,file,text etc) and second paramter 
  31.                 //specify either user wants to copy the data or move data
  32.  
  33.                 Panel source = (Panel)sender;
  34.                 DoDragDrop(source.BackgroundImage, DragDropEffects.Copy);
  35.  
  36.  
  37.             }
  38.  
Aug 23 '07 #1
Share this Question
Share on Google+
6 Replies


P: 11
My code is not showing?
Aug 23 '07 #2

P: 11
I have added a panel and a open dialog that paints an image in the panel but I cannot drag the image to any other panels.

With what I have here I can drag and drop in the same form.

I cannot drag from the image opened in panel1 from the loadbutton.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8.  
  9. namespace OpeninPanel
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         public Form1()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.  
  18.         private Bitmap m_Bitmap = null;
  19.  
  20.         private void m_LoadButton_Click( object sender, EventArgs e )
  21.         {
  22.             if( m_OpenFileDialog.ShowDialog( this ) == DialogResult.OK )
  23.             {
  24.                 m_Bitmap = new Bitmap( m_OpenFileDialog.FileName );
  25.  
  26.                 m_PaintPanel.SuspendLayout();
  27.                 m_PaintPanel.Controls.Clear();
  28.  
  29.                 Panel panel = new Panel();
  30.                 //int left = 0, top = 0;
  31.                 //if( m_Bitmap.Width < m_PaintPanel.Width )
  32.                 //{
  33.                  //   left = ( m_PaintPanel.Width - m_Bitmap.Width ) / 2;
  34.                // }
  35.                // if( m_Bitmap.Height < m_PaintPanel.Height )
  36.               //  {
  37.                //     top = (m_PaintPanel.Height - m_Bitmap.Height) / 2;
  38.               //  }
  39.                 panel.Size = m_Bitmap.Size;
  40.                // panel.Location = new Point( left, top );
  41.                 panel.Paint += new System.Windows.Forms.PaintEventHandler( this.m_PaintPanel_Paint );
  42.                 m_PaintPanel.Controls.Add( panel );
  43.  
  44.                 m_PaintPanel.ResumeLayout();
  45.  
  46.                 Panel source = (Panel)sender;
  47.                 DoDragDrop(source.BackgroundImage, DragDropEffects.Copy);
  48.             }
  49.         }
  50.  
  51.         private void m_PaintPanel_Paint( object sender, PaintEventArgs e )
  52.         {
  53.             if( m_Bitmap != null )
  54.             {
  55.                 e.Graphics.DrawImageUnscaled( m_Bitmap, 0, 0 );
  56.             }
  57.         }
  58.  
  59.         /// <summary>
  60.         /// Required designer variable.
  61.         /// </summary>
  62.         private System.ComponentModel.IContainer components = null;
  63.  
  64.         /// <summary>
  65.         /// Clean up any resources being used.
  66.         /// </summary>
  67.         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  68.  
  69.  
  70.         #region Windows Form Designer generated code
  71.  
  72.         /// <summary>
  73.         /// Required method for Designer support - do not modify
  74.         /// the contents of this method with the code editor.
  75.         /// </summary>
  76.         private void InitializeComponent()
  77.         {
  78.             this.m_PaintPanel = new System.Windows.Forms.Panel();
  79.             this.m_LoadButton = new System.Windows.Forms.Button();
  80.             this.m_OpenFileDialog = new System.Windows.Forms.OpenFileDialog();
  81.             this.panel1 = new System.Windows.Forms.Panel();
  82.             this.panel2 = new System.Windows.Forms.Panel();
  83.             this.panel3 = new System.Windows.Forms.Panel();
  84.             this.panel2.SuspendLayout();
  85.             this.SuspendLayout();
  86.             // 
  87.             // m_PaintPanel
  88.             // 
  89.             this.m_PaintPanel.AllowDrop = true;
  90.             this.m_PaintPanel.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  91.                         | System.Windows.Forms.AnchorStyles.Left)
  92.                         | System.Windows.Forms.AnchorStyles.Right)));
  93.             this.m_PaintPanel.BackColor = System.Drawing.Color.Transparent;
  94.             this.m_PaintPanel.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
  95.             this.m_PaintPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  96.             this.m_PaintPanel.Location = new System.Drawing.Point(273, 107);
  97.             this.m_PaintPanel.Name = "m_PaintPanel";
  98.             this.m_PaintPanel.Size = new System.Drawing.Size(121, 190);
  99.             this.m_PaintPanel.TabIndex = 0;
  100.             this.m_PaintPanel.MouseDown += new System.Windows.Forms.MouseEventHandler(this.m_PaintPanel_MouseDown);
  101.             this.m_PaintPanel.DragDrop += new System.Windows.Forms.DragEventHandler(this.m_PaintPanel_DragDrop);
  102.             this.m_PaintPanel.DragEnter += new System.Windows.Forms.DragEventHandler(this.m_PaintPanel_DragEnter);
  103.             this.m_PaintPanel.Paint += new System.Windows.Forms.PaintEventHandler(this.m_PaintPanel_Paint_1);
  104.             // 
  105.             // m_LoadButton
  106.             // 
  107.             this.m_LoadButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
  108.             this.m_LoadButton.Location = new System.Drawing.Point(299, 318);
  109.             this.m_LoadButton.Name = "m_LoadButton";
  110.             this.m_LoadButton.Size = new System.Drawing.Size(75, 23);
  111.             this.m_LoadButton.TabIndex = 1;
  112.             this.m_LoadButton.Text = "Load image";
  113.             this.m_LoadButton.UseVisualStyleBackColor = true;
  114.             this.m_LoadButton.Click += new System.EventHandler(this.m_LoadButton_Click);
  115.             // 
  116.             // m_OpenFileDialog
  117.             // 
  118.             this.m_OpenFileDialog.FileName = "openFileDialog1";
  119.             // 
  120.             // panel1
  121.             // 
  122.             this.panel1.AllowDrop = true;
  123.             this.panel1.BackgroundImage = global::CenterPictureBox.Properties.Resources.icon;
  124.             this.panel1.Location = new System.Drawing.Point(23, 135);
  125.             this.panel1.Name = "panel1";
  126.             this.panel1.Size = new System.Drawing.Size(60, 60);
  127.             this.panel1.TabIndex = 4;
  128.             this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseDown);
  129.             this.panel1.DragDrop += new System.Windows.Forms.DragEventHandler(this.panel1_DragDrop);
  130.             this.panel1.DragEnter += new System.Windows.Forms.DragEventHandler(this.panel1_DragEnter);
  131.             // 
  132.             // panel2
  133.             // 
  134.             this.panel2.AllowDrop = true;
  135.             this.panel2.BackgroundImage = global::CenterPictureBox.Properties.Resources.darktower;
  136.             this.panel2.Controls.Add(this.panel3);
  137.             this.panel2.Location = new System.Drawing.Point(429, 12);
  138.             this.panel2.Name = "panel2";
  139.             this.panel2.Size = new System.Drawing.Size(320, 480);
  140.             this.panel2.TabIndex = 3;
  141.             this.panel2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel2_MouseDown);
  142.             this.panel2.DragDrop += new System.Windows.Forms.DragEventHandler(this.panel2_DragDrop);
  143.             this.panel2.DragEnter += new System.Windows.Forms.DragEventHandler(this.panel2_DragEnter);
  144.             // 
  145.             // panel3
  146.             // 
  147.             this.panel3.AllowDrop = true;
  148.             this.panel3.BackColor = System.Drawing.Color.Transparent;
  149.             this.panel3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  150.             this.panel3.Location = new System.Drawing.Point(26, 21);
  151.             this.panel3.Name = "panel3";
  152.             this.panel3.Size = new System.Drawing.Size(60, 60);
  153.             this.panel3.TabIndex = 0;
  154.             this.panel3.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel3_MouseDown);
  155.             this.panel3.DragDrop += new System.Windows.Forms.DragEventHandler(this.panel3_DragDrop);
  156.             this.panel3.DragEnter += new System.Windows.Forms.DragEventHandler(this.panel3_DragEnter);
  157.             // 
  158.             // Form1
  159.             // 
  160.             this.AllowDrop = true;
  161.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  162.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  163.             this.ClientSize = new System.Drawing.Size(761, 525);
  164.             this.Controls.Add(this.panel1);
  165.             this.Controls.Add(this.panel2);
  166.             this.Controls.Add(this.m_LoadButton);
  167.             this.Controls.Add(this.m_PaintPanel);
  168.             this.Name = "Form1";
  169.             this.Text = "Form1";
  170.             this.Load += new System.EventHandler(this.Form1_Load);
  171.             this.panel2.ResumeLayout(false);
  172.             this.ResumeLayout(false);
  173.  
  174.         }
  175.  
  176.         #endregion
  177.  
  178.         private System.Windows.Forms.Panel m_PaintPanel;
  179.         private System.Windows.Forms.Button m_LoadButton;
  180.         private System.Windows.Forms.OpenFileDialog m_OpenFileDialog;
  181.  
  182.         private void m_PaintPanel_Paint_1(object sender, PaintEventArgs e)
  183.         {
  184.  
  185.         }
  186.  
  187.         private void Form1_Load(object sender, EventArgs e)
  188.         {
  189.  
  190.         }
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.         private void panel2_DragDrop(object sender, DragEventArgs e)
  202.         {
  203.             //target control will accept data here 
  204.             Panel destination = (Panel)sender;
  205.             destination.BackgroundImage = (Bitmap)e.Data.GetData(typeof(Bitmap));
  206.         }
  207.  
  208.         private void panel2_DragEnter(object sender, DragEventArgs e)
  209.         {
  210.             //As we are interested in Image data only we will check this as follows
  211.             if (e.Data.GetDataPresent(typeof(Bitmap)))
  212.             {
  213.                 e.Effect = DragDropEffects.Copy;
  214.             }
  215.             else
  216.             {
  217.                 e.Effect = DragDropEffects.None;
  218.             }
  219.         }
  220.  
  221.         private void panel2_MouseDown(object sender, MouseEventArgs e)
  222.         {
  223.             //we will pass the data that user wants to drag DoDragDrop method is used for holding data
  224.             //DoDragDrop accepts two paramete first paramter is data(image,file,text etc) and second paramter 
  225.             //specify either user wants to copy the data or move data
  226.  
  227.             Panel source = (Panel)sender;
  228.             DoDragDrop(source.BackgroundImage, DragDropEffects.Copy);
  229.         }
  230.  
  231.         private void m_PaintPanel_MouseDown(object sender, MouseEventArgs e)
  232.         {
  233.             //we will pass the data that user wants to drag DoDragDrop method is used for holding data
  234.             //DoDragDrop accepts two paramete first paramter is data(image,file,text etc) and second paramter 
  235.             //specify either user wants to copy the data or move data
  236.  
  237.             Panel source = (Panel)sender;
  238.             DoDragDrop(source.BackgroundImage, DragDropEffects.Copy);
  239.         }
  240.  
  241.         private void m_PaintPanel_DragDrop(object sender, DragEventArgs e)
  242.         {
  243.             //target control will accept data here 
  244.             Panel destination = (Panel)sender;
  245.             destination.BackgroundImage = (Bitmap)e.Data.GetData(typeof(Bitmap));
  246.         }
  247.  
  248.         private void m_PaintPanel_DragEnter(object sender, DragEventArgs e)
  249.         {
  250.             //As we are interested in Image data only we will check this as follows
  251.             if (e.Data.GetDataPresent(typeof(Bitmap)))
  252.             {
  253.                 e.Effect = DragDropEffects.Copy;
  254.             }
  255.             else
  256.             {
  257.                 e.Effect = DragDropEffects.None;
  258.             }
  259.         }
  260.  
  261.         private void panel3_DragDrop(object sender, DragEventArgs e)
  262.         {
  263.             //target control will accept data here 
  264.             Panel destination = (Panel)sender;
  265.             destination.BackgroundImage = (Bitmap)e.Data.GetData(typeof(Bitmap));
  266.         }
  267.  
  268.         private void panel3_DragEnter(object sender, DragEventArgs e)
  269.         {
  270.             //As we are interested in Image data only we will check this as follows
  271.             if (e.Data.GetDataPresent(typeof(Bitmap)))
  272.             {
  273.                 e.Effect = DragDropEffects.Copy;
  274.             }
  275.             else
  276.             {
  277.                 e.Effect = DragDropEffects.None;
  278.             }
  279.         }
  280.  
  281.         private void panel3_MouseDown(object sender, MouseEventArgs e)
  282.         {
  283.             //we will pass the data that user wants to drag DoDragDrop method is used for holding data
  284.             //DoDragDrop accepts two paramete first paramter is data(image,file,text etc) and second paramter 
  285.             //specify either user wants to copy the data or move data
  286.  
  287.             Panel source = (Panel)sender;
  288.             DoDragDrop(source.BackgroundImage, DragDropEffects.Copy);
  289.         }
  290.  
  291.         private void panel1_MouseDown(object sender, MouseEventArgs e)
  292.         {
  293.             //we will pass the data that user wants to drag DoDragDrop method is used for holding data
  294.             //DoDragDrop accepts two paramete first paramter is data(image,file,text etc) and second paramter 
  295.             //specify either user wants to copy the data or move data
  296.  
  297.             Panel source = (Panel)sender;
  298.             DoDragDrop(source.BackgroundImage, DragDropEffects.Copy);
  299.         }
  300.  
  301.         private void panel1_DragDrop(object sender, DragEventArgs e)
  302.         {
  303.             //target control will accept data here 
  304.             Panel destination = (Panel)sender;
  305.             destination.BackgroundImage = (Bitmap)e.Data.GetData(typeof(Bitmap));
  306.         }
  307.  
  308.         private void panel1_DragEnter(object sender, DragEventArgs e)
  309.         {
  310.             //As we are interested in Image data only we will check this as follows
  311.             if (e.Data.GetDataPresent(typeof(Bitmap)))
  312.             {
  313.                 e.Effect = DragDropEffects.Copy;
  314.             }
  315.             else
  316.             {
  317.                 e.Effect = DragDropEffects.None;
  318.             }
  319.         }
  320.     }
  321. }
  322.  
Aug 23 '07 #3

P: 11
I have added a panel and a open dialog that paints an image in the panel but I cannot drag the image to any other panels.

With what I have here I can drag and drop in the same form.

I cannot drag from the image opened in panel1 from the load button.

Expand|Select|Wrap|Line Numbers
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace OpeninPanel
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.  
  19.         private Bitmap m_Bitmap = null;
  20.  
  21.         private void m_LoadButton_Click( object sender, EventArgs e )
  22.         {
  23.             if( m_OpenFileDialog.ShowDialog( this ) == DialogResult.OK )
  24.             {
  25.                 m_Bitmap = new Bitmap( m_OpenFileDialog.FileName );
  26.  
  27.                 m_PaintPanel.SuspendLayout();
  28.                 m_PaintPanel.Controls.Clear();
  29.  
  30.                 Panel panel = new Panel();
  31.                 //int left = 0, top = 0;
  32.                 //if( m_Bitmap.Width < m_PaintPanel.Width )
  33.                 //{
  34.                  //   left = ( m_PaintPanel.Width - m_Bitmap.Width ) / 2;
  35.                // }
  36.                // if( m_Bitmap.Height < m_PaintPanel.Height )
  37.               //  {
  38.                //     top = (m_PaintPanel.Height - m_Bitmap.Height) / 2;
  39.               //  }
  40.                 panel.Size = m_Bitmap.Size;
  41.                // panel.Location = new Point( left, top );
  42.                 panel.Paint += new System.Windows.Forms.PaintEventHandler( this.m_PaintPanel_Paint );
  43.                 m_PaintPanel.Controls.Add( panel );
  44.  
  45.                 m_PaintPanel.ResumeLayout();
  46.  
  47.                 Panel source = (Panel)sender;
  48.                 DoDragDrop(source.BackgroundImage, DragDropEffects.Copy);
  49.             }
  50.         }
  51.  
  52.         private void m_PaintPanel_Paint( object sender, PaintEventArgs e )
  53.         {
  54.             if( m_Bitmap != null )
  55.             {
  56.                 e.Graphics.DrawImageUnscaled( m_Bitmap, 0, 0 );
  57.             }
  58.         }
  59.  
  60.         /// <summary>
  61.         /// Required designer variable.
  62.         /// </summary>
  63.         private System.ComponentModel.IContainer components = null;
  64.  
  65.         /// <summary>
  66.         /// Clean up any resources being used.
  67.         /// </summary>
  68.         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  69.  
  70.  
  71.         #region Windows Form Designer generated code
  72.  
  73.         /// <summary>
  74.         /// Required method for Designer support - do not modify
  75.         /// the contents of this method with the code editor.
  76.         /// </summary>
  77.         private void InitializeComponent()
  78.         {
  79.             this.m_PaintPanel = new System.Windows.Forms.Panel();
  80.             this.m_LoadButton = new System.Windows.Forms.Button();
  81.             this.m_OpenFileDialog = new System.Windows.Forms.OpenFileDialog();
  82.             this.panel1 = new System.Windows.Forms.Panel();
  83.             this.panel2 = new System.Windows.Forms.Panel();
  84.             this.panel3 = new System.Windows.Forms.Panel();
  85.             this.panel2.SuspendLayout();
  86.             this.SuspendLayout();
  87.             // 
  88.             // m_PaintPanel
  89.             // 
  90.             this.m_PaintPanel.AllowDrop = true;
  91.             this.m_PaintPanel.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  92.                         | System.Windows.Forms.AnchorStyles.Left)
  93.                         | System.Windows.Forms.AnchorStyles.Right)));
  94.             this.m_PaintPanel.BackColor = System.Drawing.Color.Transparent;
  95.             this.m_PaintPanel.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
  96.             this.m_PaintPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  97.             this.m_PaintPanel.Location = new System.Drawing.Point(273, 107);
  98.             this.m_PaintPanel.Name = "m_PaintPanel";
  99.             this.m_PaintPanel.Size = new System.Drawing.Size(121, 190);
  100.             this.m_PaintPanel.TabIndex = 0;
  101.             this.m_PaintPanel.MouseDown += new System.Windows.Forms.MouseEventHandler(this.m_PaintPanel_MouseDown);
  102.             this.m_PaintPanel.DragDrop += new System.Windows.Forms.DragEventHandler(this.m_PaintPanel_DragDrop);
  103.             this.m_PaintPanel.DragEnter += new System.Windows.Forms.DragEventHandler(this.m_PaintPanel_DragEnter);
  104.             this.m_PaintPanel.Paint += new System.Windows.Forms.PaintEventHandler(this.m_PaintPanel_Paint_1);
  105.             // 
  106.             // m_LoadButton
  107.             // 
  108.             this.m_LoadButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
  109.             this.m_LoadButton.Location = new System.Drawing.Point(299, 318);
  110.             this.m_LoadButton.Name = "m_LoadButton";
  111.             this.m_LoadButton.Size = new System.Drawing.Size(75, 23);
  112.             this.m_LoadButton.TabIndex = 1;
  113.             this.m_LoadButton.Text = "Load image";
  114.             this.m_LoadButton.UseVisualStyleBackColor = true;
  115.             this.m_LoadButton.Click += new System.EventHandler(this.m_LoadButton_Click);
  116.             // 
  117.             // m_OpenFileDialog
  118.             // 
  119.             this.m_OpenFileDialog.FileName = "openFileDialog1";
  120.             // 
  121.             // panel1
  122.             // 
  123.             this.panel1.AllowDrop = true;
  124.             this.panel1.BackgroundImage = global::CenterPictureBox.Properties.Resources.icon;
  125.             this.panel1.Location = new System.Drawing.Point(23, 135);
  126.             this.panel1.Name = "panel1";
  127.             this.panel1.Size = new System.Drawing.Size(60, 60);
  128.             this.panel1.TabIndex = 4;
  129.             this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseDown);
  130.             this.panel1.DragDrop += new System.Windows.Forms.DragEventHandler(this.panel1_DragDrop);
  131.             this.panel1.DragEnter += new System.Windows.Forms.DragEventHandler(this.panel1_DragEnter);
  132.             // 
  133.             // panel2
  134.             // 
  135.             this.panel2.AllowDrop = true;
  136.             this.panel2.BackgroundImage = global::CenterPictureBox.Properties.Resources.darktower;
  137.             this.panel2.Controls.Add(this.panel3);
  138.             this.panel2.Location = new System.Drawing.Point(429, 12);
  139.             this.panel2.Name = "panel2";
  140.             this.panel2.Size = new System.Drawing.Size(320, 480);
  141.             this.panel2.TabIndex = 3;
  142.             this.panel2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel2_MouseDown);
  143.             this.panel2.DragDrop += new System.Windows.Forms.DragEventHandler(this.panel2_DragDrop);
  144.             this.panel2.DragEnter += new System.Windows.Forms.DragEventHandler(this.panel2_DragEnter);
  145.             // 
  146.             // panel3
  147.             // 
  148.             this.panel3.AllowDrop = true;
  149.             this.panel3.BackColor = System.Drawing.Color.Transparent;
  150.             this.panel3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  151.             this.panel3.Location = new System.Drawing.Point(26, 21);
  152.             this.panel3.Name = "panel3";
  153.             this.panel3.Size = new System.Drawing.Size(60, 60);
  154.             this.panel3.TabIndex = 0;
  155.             this.panel3.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel3_MouseDown);
  156.             this.panel3.DragDrop += new System.Windows.Forms.DragEventHandler(this.panel3_DragDrop);
  157.             this.panel3.DragEnter += new System.Windows.Forms.DragEventHandler(this.panel3_DragEnter);
  158.             // 
  159.             // Form1
  160.             // 
  161.             this.AllowDrop = true;
  162.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  163.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  164.             this.ClientSize = new System.Drawing.Size(761, 525);
  165.             this.Controls.Add(this.panel1);
  166.             this.Controls.Add(this.panel2);
  167.             this.Controls.Add(this.m_LoadButton);
  168.             this.Controls.Add(this.m_PaintPanel);
  169.             this.Name = "Form1";
  170.             this.Text = "Form1";
  171.             this.Load += new System.EventHandler(this.Form1_Load);
  172.             this.panel2.ResumeLayout(false);
  173.             this.ResumeLayout(false);
  174.  
  175.         }
  176.  
  177.         #endregion
  178.  
  179.         private System.Windows.Forms.Panel m_PaintPanel;
  180.         private System.Windows.Forms.Button m_LoadButton;
  181.         private System.Windows.Forms.OpenFileDialog m_OpenFileDialog;
  182.  
  183.         private void m_PaintPanel_Paint_1(object sender, PaintEventArgs e)
  184.         {
  185.  
  186.         }
  187.  
  188.         private void Form1_Load(object sender, EventArgs e)
  189.         {
  190.  
  191.         }
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.         private void panel2_DragDrop(object sender, DragEventArgs e)
  203.         {
  204.             //target control will accept data here 
  205.             Panel destination = (Panel)sender;
  206.             destination.BackgroundImage = (Bitmap)e.Data.GetData(typeof(Bitmap));
  207.         }
  208.  
  209.         private void panel2_DragEnter(object sender, DragEventArgs e)
  210.         {
  211.             //As we are interested in Image data only we will check this as follows
  212.             if (e.Data.GetDataPresent(typeof(Bitmap)))
  213.             {
  214.                 e.Effect = DragDropEffects.Copy;
  215.             }
  216.             else
  217.             {
  218.                 e.Effect = DragDropEffects.None;
  219.             }
  220.         }
  221.  
  222.         private void panel2_MouseDown(object sender, MouseEventArgs e)
  223.         {
  224.             //we will pass the data that user wants to drag DoDragDrop method is used for holding data
  225.             //DoDragDrop accepts two paramete first paramter is data(image,file,text etc) and second paramter 
  226.             //specify either user wants to copy the data or move data
  227.  
  228.             Panel source = (Panel)sender;
  229.             DoDragDrop(source.BackgroundImage, DragDropEffects.Copy);
  230.         }
  231.  
  232.         private void m_PaintPanel_MouseDown(object sender, MouseEventArgs e)
  233.         {
  234.             //we will pass the data that user wants to drag DoDragDrop method is used for holding data
  235.             //DoDragDrop accepts two paramete first paramter is data(image,file,text etc) and second paramter 
  236.             //specify either user wants to copy the data or move data
  237.  
  238.             Panel source = (Panel)sender;
  239.             DoDragDrop(source.BackgroundImage, DragDropEffects.Copy);
  240.         }
  241.  
  242.         private void m_PaintPanel_DragDrop(object sender, DragEventArgs e)
  243.         {
  244.             //target control will accept data here 
  245.             Panel destination = (Panel)sender;
  246.             destination.BackgroundImage = (Bitmap)e.Data.GetData(typeof(Bitmap));
  247.         }
  248.  
  249.         private void m_PaintPanel_DragEnter(object sender, DragEventArgs e)
  250.         {
  251.             //As we are interested in Image data only we will check this as follows
  252.             if (e.Data.GetDataPresent(typeof(Bitmap)))
  253.             {
  254.                 e.Effect = DragDropEffects.Copy;
  255.             }
  256.             else
  257.             {
  258.                 e.Effect = DragDropEffects.None;
  259.             }
  260.         }
  261.  
  262.         private void panel3_DragDrop(object sender, DragEventArgs e)
  263.         {
  264.             //target control will accept data here 
  265.             Panel destination = (Panel)sender;
  266.             destination.BackgroundImage = (Bitmap)e.Data.GetData(typeof(Bitmap));
  267.         }
  268.  
  269.         private void panel3_DragEnter(object sender, DragEventArgs e)
  270.         {
  271.             //As we are interested in Image data only we will check this as follows
  272.             if (e.Data.GetDataPresent(typeof(Bitmap)))
  273.             {
  274.                 e.Effect = DragDropEffects.Copy;
  275.             }
  276.             else
  277.             {
  278.                 e.Effect = DragDropEffects.None;
  279.             }
  280.         }
  281.  
  282.         private void panel3_MouseDown(object sender, MouseEventArgs e)
  283.         {
  284.             //we will pass the data that user wants to drag DoDragDrop method is used for holding data
  285.             //DoDragDrop accepts two paramete first paramter is data(image,file,text etc) and second paramter 
  286.             //specify either user wants to copy the data or move data
  287.  
  288.             Panel source = (Panel)sender;
  289.             DoDragDrop(source.BackgroundImage, DragDropEffects.Copy);
  290.         }
  291.  
  292.         private void panel1_MouseDown(object sender, MouseEventArgs e)
  293.         {
  294.             //we will pass the data that user wants to drag DoDragDrop method is used for holding data
  295.             //DoDragDrop accepts two paramete first paramter is data(image,file,text etc) and second paramter 
  296.             //specify either user wants to copy the data or move data
  297.  
  298.             Panel source = (Panel)sender;
  299.             DoDragDrop(source.BackgroundImage, DragDropEffects.Copy);
  300.         }
  301.  
  302.         private void panel1_DragDrop(object sender, DragEventArgs e)
  303.         {
  304.             //target control will accept data here 
  305.             Panel destination = (Panel)sender;
  306.             destination.BackgroundImage = (Bitmap)e.Data.GetData(typeof(Bitmap));
  307.         }
  308.  
  309.         private void panel1_DragEnter(object sender, DragEventArgs e)
  310.         {
  311.             //As we are interested in Image data only we will check this as follows
  312.             if (e.Data.GetDataPresent(typeof(Bitmap)))
  313.             {
  314.                 e.Effect = DragDropEffects.Copy;
  315.             }
  316.             else
  317.             {
  318.                 e.Effect = DragDropEffects.None;
  319.             }
  320.         }
  321.     }
  322. }
  323.  
Aug 23 '07 #4

Shashi Sadasivan
Expert 100+
P: 1,435
hi.
I have never tried dragging and dropping with applications
Found 2 links which should help u out, one is in vb, hope u transalet that to c# if it is so
http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.allowdrop(VS.80).aspx
http://www.ondotnet.com/pub/a/dotnet/2002/11/11/dragdrop.htm

and please keep ure comments tidy
Aug 23 '07 #5

Plater
Expert 5K+
P: 7,872
If you are dragging a file, it will NOT show up as bitmap data.
You should debug and look at what DataTypes are sent when you drag and drop.
Aug 23 '07 #6

P: 11
Thanks for your responses I have it all sorted out now.

Here is the solution for anyone else looking to do this.

Drag and drop images from outside the form into panels.

Expand|Select|Wrap|Line Numbers
  1.  
  2. private void OpenFile(string ImageFile)
  3.         {
  4.             Bitmap bmp;
  5.  
  6.             try
  7.             {
  8.                 // Load bitmap file
  9.                 bmp = (Bitmap)Bitmap.FromFile(ImageFile, false);
  10.  
  11.                 if (bmp != null)
  12.                 {
  13.                     // Keep bitmap in class member
  14.                     m_Bitmap = (Bitmap)bmp.Clone();
  15.  
  16.                     this.AutoScroll = true;
  17.                     this.AutoScrollMinSize = new Size(
  18.                             m_Bitmap.Width,
  19.                             m_Bitmap.Height);
  20.  
  21.                     Invalidate();
  22.                 }
  23.             }
  24.             catch (Exception ex)
  25.             {
  26.                 MessageBox.Show(this, ex.Message, "Error loading from file");
  27.             }
  28.  
  29.         }
  30.  
  31. -------------------------------------------------------------------------------------------------------------------
  32.  
  33.  
  34.  
  35. private void panel6_DragDrop(object sender, DragEventArgs e)
  36.         {
  37.             try
  38.             {
  39.                 if (e.Data.GetDataPresent(DataFormats.FileDrop))
  40.                 {
  41.                     Array a = (Array)e.Data.GetData(DataFormats.FileDrop);
  42.                     if (a != null)
  43.                     {
  44.                         string s = a.GetValue(0).ToString();
  45.                         this.OpenFile(s);
  46.                         this.panel6.BackgroundImage = m_Bitmap;
  47.                     }
  48.                 }
  49.                 else
  50.                     if (e.Data.GetDataPresent(typeof(Bitmap)))
  51.                     {
  52.                         Panel dich = (Panel)sender;
  53.                         dich.BackgroundImage = (Bitmap)e.Data.GetData(typeof(Bitmap));
  54.                     }
  55.  
  56.             }
  57.             catch (Exception ex)
  58.             {
  59.                 Trace.WriteLine("Error in DragDrop function: " + ex.Message);
  60.             }
  61.         }
  62.  
  63.  
  64.         private void panel6_DragEnter(object sender, DragEventArgs e)
  65.         {
  66.             // If file is dragged, show cursor "Drop allowed"
  67.  
  68.             if (e.Data.GetDataPresent(DataFormats.FileDrop))
  69.                 e.Effect = DragDropEffects.Copy;
  70.             else
  71.                 if (e.Data.GetDataPresent(typeof(Bitmap)))
  72.                     e.Effect = DragDropEffects.Move;
  73.                 else
  74.                     e.Effect = DragDropEffects.None;
  75.  
  76.  
  77.  
  78.         }
  79.  
  80.         private void panel6_MouseDown(object sender, MouseEventArgs e)
  81.         {
  82.             Panel nguon = (Panel)sender;
  83.             DoDragDrop(nguon.BackgroundImage, DragDropEffects.Move);
  84.         }
  85.  
  86.  
  87.  
Aug 31 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.