0
votes

i am new to programming and i am trying to do a drag and drop here , i can drag and drop now but the custom cursor to drag and drop is ugly , how do i drag the element that i am draging as the cursor? i search online and found some mentioning about adorner but i don't even understand the codes . Is there any simple or simplified and better way to do this?

I have this code here that i can drag and drop ( i dynamically created textbox and label in a for loop , i retrieve the text and append it to label from a database :

                TextBox tbox = new TextBox();
                tbox.Width = 250;
                tbox.Height = 50;
                tbox.AllowDrop = true;
                tbox.FontSize = 24;
                tbox.BorderThickness = new Thickness(2);
                tbox.BorderBrush = Brushes.BlanchedAlmond;     
                tbox.PreviewDrop += new DragEventHandler(tbox_Drop);

                if (lstQuestion[i].Answer.Trim().Length > 0)
                {

                    wrapPanel2.Children.Add(tbox);
                    answers.Add(lbl.Content.ToString());
                    MatchWords.Add(question.Content.ToString(), lbl.Content.ToString());

                }

 Dictionary<string, string> shuffled = Shuffle(MatchWords);
        foreach (KeyValuePair<string, string> s in shuffled)
        {
            Label lbl = new Label();
            lbl.Content = s.Value;
            lbl.Width = 100;
            lbl.Height = 50;
            lbl.FontSize = 24;              
            lbl.DragEnter += new DragEventHandler(lbl_DragEnter);
          //  lbl.MouseMove += new MouseEventHandler(lbl_MouseMove);
            lbl.MouseDown +=new MouseButtonEventHandler(lbl_MouseDown);

           dockPanel1.Children.Add(lbl);
        }

I am dragging labels(drag target) into textbox( drop target ) , which event should i use and how i write the events to set the drag cursor to the labels that i am dragging?

Here are the events i have used atm :

        private void tbox_Drop(object sender, DragEventArgs e)
    {
        MessageBox.Show("Are you sure ? Wrong don't blame me ");
        (sender as TextBox).Text = string.Empty;

    }

    private void lbl_DragEnter(object sender, DragEventArgs e)
    {
        if (sender == e.Source)
        {
            e.Effects = DragDropEffects.None;
        }
    } 

Any solutions or help is appreciated , I've seen adorner , its way too complicated for me to understand to implement it . Looking for a simple and simplified way to do this .

1
Adorners are how you do what you want to do. IF you want to do what you want to do, I suggest you knuckle down and learn about Adorners. It's not very difficult. Think of Adorners as being a layer above your control on which you can draw stuff. Google "DragDropAdorner" and go from there.Mark Green

1 Answers