473,327 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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

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
7 4716
GaryTexmo
1,501 Expert 1GB
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
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
1,501 Expert 1GB
Did you try setting the AllowDrop property to true?
Mar 9 '11 #4
yes i tried it.. but its still not working
Mar 10 '11 #5
GaryTexmo
1,501 Expert 1GB
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
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
1,501 Expert 1GB
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

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

Similar topics

1
by: Karsten Schramm | last post by:
Hi, if I drag an Outlook.MailItem to a Windows-Explorer window a <subject>.msg file will be created. Now I try to drag & drop a mail item to my own WinForm app. Unfortunately it doesn't work....
15
by: Wiktor Zychla | last post by:
today we've found a critical issue regarding the ListView from Windows.Forms. it was confirmed on several machines with Win2K and XP. here's the problem: create a ListView with about 50000 rows....
4
by: Qingdong Z. | last post by:
I have a VS.NET windows application which use drag-drop feather. It works fine when application start, but stops working after application run one or two days. The application is related to Video...
1
by: Brett Romero | last post by:
Is there an object similar to the treeview (or way to extend tree view) that will allow drag/drop functionality to reorder child nodes? For example: Parent1 -c1 -c2 -c3 Drag c3 above c1:
1
by: Nick | last post by:
Hi, I am developping an app using managed C++. I want to be able to do a drag drop from the app to windows explorer. The files to be dropped onto explorer do not exist yet, the files are to be...
4
by: shinichi81 | last post by:
Could anyone help me? After updating icon for a drive, for example C drive by specifying icon file at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\C\DefaultIcon,...
5
by: murrayatuptowngallery | last post by:
I would like to have a slider or mouse position click capture means of collecting a user input from a scale or ruler-like graphic. I found some code snippets for mouse position capture on...
0
by: Mukesh Agarwal | last post by:
Hi, I am developing a windows application, in which I am giving two options to the user for the file selection. 1. Open File Dialog 2. Drag/ Drop Now I want that the user can Drag/Drop...
1
by: jakki | last post by:
hi every one.. i am binding data in a tree view using windows C# .. auctually i finished most of the code.. but i did't know how to proceed further .. i attached the code hear.. as well as...
0
by: =?Utf-8?B?QW5kcmV3?= | last post by:
I have some code that I use to drag and drop from treeview to treeview, now I want to adapt the code to drag and drop from listview to treeview. I am having problems adapting the code nor can I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.