Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / September 2004

Tip: Looking for answers? Try searching our database.

Drag and Drop from one form to a 2nd form

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
AlBruAn - 29 Sep 2004 21:51 GMT
I've been trying to perform a drag and drop operation from a PictureBox
control in a standard C# form (with a FixedToolWindow border) to another
standard C# form, but cannot get it to work properly.  

In the FixedToolWindow, I have the following code:
   myClass c = new myClass();   // contains public variable m_MouseIsDown

   private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
   {
       if (! (pictureBox1.Image == null))
           c.m_MouseIsDown = true;
   }

   private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
   {
       if (c.m_MouseIsDown)
           pictureBox1.DoDragDrop(pictureBox1.Image, DragDropEffects.Copy);
   }

In the second form, I have the DropAllow property enabled and have the
following code in it:
   myClass c = new myClass();   // contains public variable m_MouseIsDown
   private int picboxCount;
   private PictureBox picbox;

   private void Form1_DragDrop(object sender, DragEventArgs e)
   {
       if (c.m_MouseIsDown)
       {
           int i = picboxCount;
           picbox = new PictureBox();

           this.Controls.Add(picbox);

           picbox.Name = "pictureBox" + i.ToString();
           picbox.BorderStyle = BorderStyle.FixedSingle;
           picbox.Size = new Size(24, 24);
           picbox.Image = (Image)e.Data.GetDataPresent(DataFormats.Bitmap);
           picbox.Location = this.PointToClient(new Point(e.X - 12, e.Y -
12));

           picboxCount++;
           c.m_MouseIsDown = false;
       }
   }

   private void Form1_DragEnter(object sender, DragEventArgs e)
   {
       e.Effect = DragDropEffects.Copy;
   }

One problem I've encountered is in the statement
   picbox.Image = (Image)e.Data.GetDataPresent(DataFormats.Bitmap);
as I get a compilation error stating that it cannot convert from type 'bool'
to 'System.Drawing.Image'.  Commenting it out and hardcoding a value for the
image, I still can't get the drag and drop operation to work.

Any help would be greatly appreciated!

Allen
AlBruAn - 29 Sep 2004 22:19 GMT
I earlier wrote:

> One problem I've encountered is in the statement
>     picbox.Image = (Image)e.Data.GetDataPresent(DataFormats.Bitmap);
> as I get a compilation error stating that it cannot convert from type 'bool'
> to 'System.Drawing.Image'.  Commenting it out and hardcoding a value for the
> image, I still can't get the drag and drop operation to work.

Replacing that line with one reading
   picbox.Image = System.Drawing.Image.FromFile("C:\\concatenate.bmp");
allows the project to be compiled and I am able to successfully perform the
drag and drop operation.

Any ideas on how I can use the bitmap from the control being "dragged" and
placed into the newly-created PictureBox on the primary form?  Thanks!
Jakob Christensen - 30 Sep 2004 10:09 GMT
GetDataPresent does not return the dragged data.  Use GetData instead for
this.  Try the following code:

if (e.Data.GetDataPresent(DataFormats.Bitma))
   picbox.Image = (Image) e.Data.GetData(DataFormats.Bitmap);

HTH, Jakob.

> I earlier wrote:
>
[quoted text clipped - 11 lines]
> Any ideas on how I can use the bitmap from the control being "dragged" and
> placed into the newly-created PictureBox on the primary form?  Thanks!

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.