473,467 Members | 2,063 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

4 New Member
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 4734
GaryTexmo
1,501 Recognized Expert Top Contributor
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
chaitrabharadwa
4 New Member
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 Recognized Expert Top Contributor
Did you try setting the AllowDrop property to true?
Mar 9 '11 #4
chaitrabharadwa
4 New Member
yes i tried it.. but its still not working
Mar 10 '11 #5
GaryTexmo
1,501 Recognized Expert Top Contributor
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
chaitrabharadwa
4 New Member
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 Recognized Expert Top Contributor
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.