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

How to drag and drop an icon from the tree view on to an image in windows forms

P: 4
hi all,

I have a treeview and the picturebox in the form. i have a tree view which have icons for each node. i have imagelist which loads icons for nodes in the treeview. some image will be loaded in the picturebox. the user has to drag and drop the icon from the treeview on to the image in the picturebox. when the form is closed, the icons that are dragged and dropped on to the image sud be save.the co-ordinates of the icons on the image sud be saved in a xml file.

i have tried the drap and drop from treeview. but it is not working. i have attached the code below. pls have a look at it and let me why its not working... pls help me to do this...
Expand|Select|Wrap|Line Numbers
  1.  // This part so you can drag to picture to the control...
  2.         private void pictureBox1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
  3.         {
  4.             if (e.Data.GetDataPresent(DataFormats.FileDrop))
  5.                 e.Effect = DragDropEffects.Copy;
  6.             else
  7.                 e.Effect = DragDropEffects.None;
  8.         }
  9.  
  10.         // And now to load the picture in the picturebox control...
  11.         private void pictureBox1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
  12.         {
  13.             string[] filenames = (string[])e.Data.GetData(DataFormats.FileDrop);
  14.             pictureBox1.Image = Image.FromFile(filenames[0]);
  15.         }
  16.  
  17.         private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  18.         {
  19.             isDragging = true;
  20.             currentX = e.X;
  21.             currentY = e.Y;
  22.         }
  23.  
  24.         private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  25.         {
  26.             if (isDragging)
  27.             {
  28.                 pictureBox1.Top = pictureBox1.Top + (e.Y - currentY);
  29.                 pictureBox1.Left = pictureBox1.Left + (e.X - currentX);
  30.             }
  31.         }
  32.  
  33.         private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
  34.         {
  35.             isDragging = false;
  36.         }
  37.  
  38. //code to generate tree view
  39.  
  40.  private void PopulateTreeView()
  41.         {
  42.             //string userpath;
  43.             //userpath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  44.             //userpath += @"C:\\Documents and Settings\\chr6kor\\My Documents";
  45.  
  46.             DirectoryInfo startFolder = new DirectoryInfo(@"C:\Documents and Settings\chr6kor\My Documents");
  47.             //DirectoryInfo startFolder = new DirectoryInfo(userpath);
  48.             TreeNode rootNode = treeView1.Nodes.Add(startFolder.Name, startFolder.Name,0,0);
  49.  
  50.             CreateFileSystemNodes(rootNode, startFolder);
  51.         }
  52.  
  53.         private void CreateFileSystemNodes(TreeNode currentNode, DirectoryInfo currentFolder)
  54.         {
  55.  
  56.             // add all sub-folders within the current folder
  57.  
  58.             foreach (DirectoryInfo subfolder in currentFolder.GetDirectories())
  59.             {
  60.  
  61.                 TreeNode subFolderNode = currentNode.Nodes.Add(subfolder.Name, subfolder.Name,1,1);
  62.  
  63.                 CreateFileSystemNodes(subFolderNode, subfolder);
  64.  
  65.             }
  66.  
  67.             // add the files within the folder
  68.  
  69.             foreach (FileInfo file in currentFolder.GetFiles())
  70.             {
  71.  
  72.                 currentNode.Nodes.Add(file.Name, file.Name, 2, 2);
  73.  
  74.             }
  75.  
  76.         }
  77.  
  78.  
  79.         private string GetPathFromNode(TreeNode node)
  80.         {
  81.             if (node.Parent == null)
  82.             {
  83.                 return node.Text;
  84.             }
  85.             return Path.Combine(GetPathFromNode(node.Parent), node.Text);
  86.         }
  87.  
  88.  private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
  89.         {
  90.             if (bitmap != null)
  91.             {
  92.                 bitmap.Dispose();
  93.             }
  94.             try
  95.             {
  96.                 string fileName = GetPathFromNode(e.Node);
  97.                 bitmap = new Bitmap(fileName);
  98.                 FileInfo f = new FileInfo(fileName);
  99.                 if (bitmap.Width > bitmap.Height)
  100.                 {
  101.                     pictureBox1.Width = panel1.Width;
  102.                     pictureBox1.Height =
  103.                         (int)((double)bitmap.Height * panel1.Width / bitmap.Width);
  104.                 }
  105.                 else
  106.                 {
  107.                     pictureBox1.Height = panel1.Height;
  108.                     pictureBox1.Width =
  109.                         (int)((double)bitmap.Width * panel1.Height / bitmap.Height);
  110.                 }
  111.                 pictureBox1.Image = bitmap;
  112.                 //this.Text = "pic" + f.Name;
  113.             }
  114.             catch
  115.             {
  116.                 //pictureBox1.Width = panel1.Width;
  117.                 //pictureBox1.Height = panel1.Height;
  118.             }
  119.         }
  120.  
  121.  
pls help me sort out the problem.

thanks a lot in advance.

regards
chaitra
Mar 8 '11 #1
Share this Question
Share on Google+
7 Replies


GaryTexmo
Expert 100+
P: 1,501
Is the AllowDrop property on the appropriate controls set to true?

That's the only thing I can think of outright, if that's not it can you describe how it's not working? Like, drag/drop doesn't work at all or are you getting unexpected behaviour?
Mar 8 '11 #2

P: 4
hi Texmo,
there is no error actually.. but drag and drop is not working at all... dont know how to do..
Mar 9 '11 #3

GaryTexmo
Expert 100+
P: 1,501
Did you try setting the AllowDrop property to true?
Mar 9 '11 #4

P: 4
yes i tried it.. but its still not working
Mar 10 '11 #5

GaryTexmo
Expert 100+
P: 1,501
Ok, I looked further into this. Apparently, even though a PictureBox has the drag and drop methods, it doesn't allow drag and drop... which is really strange because it's got the methods for it. I did some digging around and couldn't find a reason for it; however, others have encountered the same issue.

http://social.msdn.microsoft.com/for...4-0af75de893fd

At that Microsoft link they recommend doing the drag/drop on the form, which may not suit your purposes. I copied/pasted the code you wrote into a new project and linked everything up. It seems as though your PictureBox is contained within a Panel, so I just enabled drag/drop on the panel and used the same events you already had created.

Everything worked perfectly :)
Mar 10 '11 #6

P: 4
hello Texmo,

i dont want to drag the image. consider for examlpe, i have a treeview of c drive. all the folders and files in that will be displayed in the treeview. for each node, folder icon will be displayed, for each file some other icon (.ico extension) will be added and displayed connectin an image list to the treeview. once the tree view is displayed, the picturebox will contain some jpeg image. from the tree view i want to drag n drop that folder or some other icon in the treeview on to the jpeg image. that icon sud be placed on the image in the picturebox.

pls can u help me to do this.....

regards,
chaitra
Mar 11 '11 #7

GaryTexmo
Expert 100+
P: 1,501
I didn't say anything about dragging the image, I was talking about dragging to the image. The code you have set up, the drag and drop methods go on the image. The link I provided you says that this isn't possible, you have to put these methods on the Panel containing the PictureBox because, for whatever reason, Microsoft hasn't exposed the AllowDrop method on the PictureBox Object.

If it's just that you're unable to get the item to drag from the tree view, I think you're missing the call to DoDragDrop in the TreeView's ItemDrag event.

Read here for more information: http://support.microsoft.com/kb/307968
Mar 11 '11 #8

Post your reply

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