473,473 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

11 New Member
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
6 6429
Alfonso2968
11 New Member
My code is not showing?
Aug 23 '07 #2
Alfonso2968
11 New Member
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
Alfonso2968
11 New Member
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
1,435 Recognized Expert Top Contributor
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
7,872 Recognized Expert Expert
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
Alfonso2968
11 New Member
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

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

Similar topics

2
by: SamSpade | last post by:
There seems to be two ways to put things on the clipboard ( I don't mean different formats): SetClipboardData and OleSetClipboard If I want to get data off the clipboard do I care how it was put...
3
by: Ajay Krishnan Thampi | last post by:
I have a slight problem implementing 'drag and drop' from a datagrid to a tree-view. I have pasted my code below. Someone please advice me on what to do...pretty blur right now. ==code== ...
4
by: zav | last post by:
Hi all i`m having a small problem with windows forms, i`m attempting to provide the following functionality to a form. Ability to drag and drop another form onto a form and then to dock this...
2
by: rob | last post by:
I'm trying to create a UI which allows to move objects. I would like to give reall drag and drop feature.. I do not want to give fake drag and drop. When the user selects it should create a...
1
by: Kevin L | last post by:
I have a Panel control that I currently allow the user to drag and reposition on a form at runtime. This Panel control contains a Label control. I would like to allow the user to drag the PANEL...
3
by: petermichaux | last post by:
Hi, I am trying to put together the last major pieces of my project's puzzle. This is more website/client-side architecture than JavaScript syntax but I hope this is a good place to ask. I'm a...
3
by: JeffDotNet | last post by:
I wrote a small data processing application that writes a summary of several hundred files. I use drag and drop on a panel (Panel1) to grab the absolute path to each of these files. Then I begin...
6
by: Blkpower | last post by:
Hi to all, I'm writing a windows form application that has a panel with some labels in it. I need to let the user move these labels with drag and drop inside the panel. How can I do it? I now...
0
by: jawilson | last post by:
Hello, I am trying to use drag-n-drop for a listview control in my program. I created a new listview control class (just call it MyListView) that inherits from ListView, and creates a few new...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.