13 Replies - 4700 Views - Last Post: 09 February 2011 - 04:57 PM Rate Topic: -----

#1 hype2525   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-February 11

WinForm Drag Drop

Posted 09 February 2011 - 03:42 PM

I'm trying to store label1.text into textbox1.text by dragging the label, but I can't figure out how to store it. I know how to drag and drop the label to the textbox but I want click and move the label to the textbox. Here's what I have so far..

public partial class Form1 : Form
    {
        private bool isDragging = false;
        private int x;
        private int y;
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_MouseDown(object sender, MouseEventArgs e)
        {
            isDragging = true;
            x = e.X;
            y = e.Y;
           
        }

        private void textBox1_DragDrop(object sender, DragEventArgs e)
        {
            textBox1.Text = e.Data.GetData(DataFormats.Text).ToString();
        }

        private void label1_MouseUp(object sender, MouseEventArgs e)
        {
            isDragging = false;
        }

        private void label1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDragging)
            {
                label1.Left = e.X + label1.Left - x;
                label1.Top = e.Y + label1.Top - y;
                
            }
        }
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: WinForm Drag Drop

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: WinForm Drag Drop

Posted 09 February 2011 - 04:00 PM

Quote

I know how to drag and drop the label to the textbox but I want click and move the label to the textbox.


Huh? How are those two things different?

It sounds like you are saying "I know how to drag and drop but how do I drag and drop?"

Update:

        private void label1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDragging)
            {
                label1.Left = e.X + label1.Left - x;
                label1.Top = e.Y + label1.Top - y;
                
            }



WTF? You look like you are trying to move the actual label control. Literally dragging the control. Why are you trying to do this? On start of drag you copy the text of the label, on drop you process that text. You don't actually move the control.

Addition:
You know we have tutorials for this sort of thing on DIC, right?

This post has been edited by tlhIn'toq: 09 February 2011 - 04:01 PM

Was This Post Helpful? 0
  • +
  • -

#3 hype2525   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-February 11

Re: WinForm Drag Drop

Posted 09 February 2011 - 04:05 PM

say label1.text = 50; I want to drag 50 into the textbox and store it. Can I do that?

I'm not looking for a copy/paste. I want to see 50 move to the textbox....
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: WinForm Drag Drop

Posted 09 February 2011 - 04:09 PM

View Posthype2525, on 09 February 2011 - 04:05 PM, said:

say label1.text = 50; I want to drag 50 into the textbox and store it. Can I do that?


yes you can. Without a doubt. And it will be 100 times easier if you work through the tutorials for drag and drop, rather than try to figure it out through trial and error. You can learn from lessons or you can try to re-invent the wheel from scratch.
Was This Post Helpful? 0
  • +
  • -

#5 sk8ermeb   User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 133
  • Joined: 23-March 10

Re: WinForm Drag Drop

Posted 09 February 2011 - 04:17 PM

View PosttlhIn, on 09 February 2011 - 04:00 PM, said:

You know we have tutorials for this sort of thing on DIC, right?


I couldn't get the link to work? Could you repost the link to the tutorial, I am kind of interested as well. Thanks.
Was This Post Helpful? 0
  • +
  • -

#6 hype2525   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-February 11

Re: WinForm Drag Drop

Posted 09 February 2011 - 04:17 PM

I can't seem to find a drag and drop tutorial on this site.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: WinForm Drag Drop

Posted 09 February 2011 - 04:26 PM

Go to the C# tutorials
http://www.dreaminco...85-c-tutorials/

At the bottom search box, enter "drag and drop" and hit the 'go' button.


http://www.dreaminco...1&#entry1042072

http://www.dreaminco..._1&#entry878301
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: WinForm Drag Drop

Posted 09 February 2011 - 04:31 PM

Unfortunately, both of those tutorials are about dropping files onto forms. (I wrote one of them).

Dragging UI elements is going to be a bit different, I think.
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: WinForm Drag Drop

Posted 09 February 2011 - 04:34 PM

View PostinsertAlias, on 09 February 2011 - 04:31 PM, said:

Unfortunately, both of those tutorials are about dropping files onto forms. (I wrote one of them).

Dragging UI elements is going to be a bit different, I think.



It's not. Really, I do this all the time in a photo browser I wrote where a picturebox is dragged to a control and the path of the photo is transferred in the arguments. Gimme a few minutes and I'll open that project and find the relevant code for an example.
Was This Post Helpful? 0
  • +
  • -

#10 sk8ermeb   User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 133
  • Joined: 23-March 10

Re: WinForm Drag Drop

Posted 09 February 2011 - 04:35 PM

Quote

say label1.text = 50; I want to drag 50 into the textbox and store it. Can I do that?

Do you want to actually see the graphics of 50 being moved?.. I see what you are saying this is opposed to the copy and past! I dont beleive any of the tutorials will address that you will most likely need to dive into the graphics class in figure out how to paint text onto the screen while in drag and drop mode
Was This Post Helpful? 0
  • +
  • -

#11 hype2525   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-February 11

Re: WinForm Drag Drop

Posted 09 February 2011 - 04:39 PM

Yes i see that. Everything i find is about drop files...

sk8ereb, yes thats what I'm looking to do. I'm kinda stumped on this.
Was This Post Helpful? 0
  • +
  • -

#12 sk8ermeb   User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 133
  • Joined: 23-March 10

Re: WinForm Drag Drop

Posted 09 February 2011 - 04:46 PM

Ok so I havn't actually done it before but you need to get the desktop handle so you can draw directly to the screen then get that graphics and draw the text...You going to have to code this one from scratch. To get this you will need a dll import

http://bytes.com/top...directly-screen

is one post I found on this
Was This Post Helpful? 0
  • +
  • -

#13 sk8ermeb   User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 133
  • Joined: 23-March 10

Re: WinForm Drag Drop

Posted 09 February 2011 - 04:54 PM

http://www.neowin.ne...ing-on-desktop/

found it!!!! this link has an example of how to draw to screen spelled out about half way down

ill just add the code

Quote

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;

namespace DrawToScreenTestApp
{

  #region UnmanagedMethods
  internal class UnmanagedMethods
  {

  [DllImport("user32")]
  internal static extern IntPtr GetDC(IntPtr hwnd);

  [DllImport("User32.dll")]
  internal static extern void ReleaseDC(IntPtr dc);

}
#endregion

#region DrawToScreen

public class DrawToScreen
{
  public void PaintRectangleToScreen()
  {
    IntPtr deskDC = UnmanagedMethods.GetDC(IntPtr.Zero);

    Graphics g = Graphics.FromHdc(deskDC);

    g.FillRectangle(new SolidBrush(Color.FromArgb(100, Color.CadetBlue)), 0,0, 300, 300);

    g.EndContainer(cont);

    g.Dispose();

    UnmanagedMethods.ReleaseDC(deskDC);
    }
  }
  #endregion
} 



This post has been edited by sk8ermeb: 09 February 2011 - 04:57 PM

Was This Post Helpful? 1
  • +
  • -

#14 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: WinForm Drag Drop

Posted 09 February 2011 - 04:57 PM

This is built on strings of file paths where you have a single string of "50" but you can adjust.

On line 14 where it says 'this.tag.tostring' you would get the text of your textbox
Sender... dragging from
        protected override void onmousemove(MouseEventArgs e)
        {
            if (!_isDragging)
            {
                // This is a check to see if the mouse is moving while pressed.
                // Without this, the DragDrop is fired directly when the control is clicked, now you have to drag a few pixels first.
                if (e.Button == MouseButtons.Left && _DDradius > 0 && this.AllowDrag)
                {
                    int num1 = _mX - e.X;
                    int num2 = _mY - e.Y;
                    if (((num1 * num1) + (num2 * num2)) > _DDradius)
                    {
                        DoDragDrop(this.Tag.ToString(), DragDropEffects.All);// Works with 1 path
                         _isDragging = true;
                        return;
                    }
                }
                //base.onmousemove(e);
            }
        }





receiver... dropped on
This is designed to accept a file from the desktop, or a string from a control in my same form. You can see the #regions that handle each.
        void ReceivingControl_DragDrop(object sender, DragEventArgs e)
        {
            try
            {
                // Each region does its own return so they are self contained

                #region Windows file drop

                // If we drop a file (Such as Windows explorer)
                if (e.Data.GetDataPresent(DataFormats.FileDrop))
                {
                    // Extract the data from the DataObject-Container into a string list
                    var FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false);

                    if (FileList != null)
                    {
                        var temp = new List<string>();
                        temp.AddRange(FileList);
                        // Do something with the data...

                        foreach (string s in temp)
                        {
                            ReceiveNewImagePath(s);
                        }
                    }
                    return;
                }

                #endregion

                #region Single string like a single path

                if (e.Data.GetDataPresent(DataFormats.StringFormat))
                {
                    ReceiveNewImagePath((string)e.Data.GetData(DataFormats.StringFormat));
                    return;
                }

                #endregion

            }
            catch (Exception exc) { }
        }


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1