Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF C# Drag and Drop

Basically i am doing a drag and drop using textbox and labels , by dragging a label to a textbox . Textboxes and Labels are created in same for loop .

I have dynamically created textboxes ( textbox is the drop target) like this :

  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.Drop += 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()); 

            }

I have also dynanmically create labels ( label is the drag target) like this :

  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);
     //       lbl.MouseUp +=new MouseButtonEventHandler(lbl_MouseUp);
           dockPanel1.Children.Add(lbl);
        }

I have 2 issues here .

1st . I am using tbox.drop event to display MessageBox.Show( something ) ; to display a messagebox when the drag target is being drop but it doesn't work .

here is my code :

       private void tbox_Drop(object sender, DragEventArgs e)
    {
        MessageBox.Show("Are you sure?");
    }

Second , i also want to clear the tbox.Text when the drag target is dropped because i might have other drag target dropped into the tbox previously. So i want to clear the tbox.Text and drop the drag target everytime when i drag the target to textbox .

How do i do that? i am stucked at which event i should use for this and how do i access tbox from those event handlers ?

like image 842
user2376998 Avatar asked Oct 03 '22 10:10

user2376998


1 Answers

It worked for me.

private void lbl_MouseDown(object sender, MouseButtonEventArgs e)
{
    Label _lbl = sender as Label;
    DragDrop.DoDragDrop(_lbl, _lbl.Content, DragDropEffects.Move);
}

You do not need MouseMove and DragEnter events for Label if you are using them just for Drag purpose.

Replace Drop event with PreviewDrop for TextBox as shown below:

tbox.Drop += new DragEventHandler(tbox_Drop);

with this

tbox.PreviewDrop += new DragEventHandler(tbox_PreviewDrop);

private void tbox_PreviewDrop(object sender, DragEventArgs e)
{
    (sender as TextBox).Text = string.Empty;
}
like image 135
Nitesh Avatar answered Oct 06 '22 00:10

Nitesh