Question

I want to be able to drag list items from a list box onto labels so that when you drop the list item, that becomes the text for the label.

I think I've got the mouse down part correct:

private void listPlayers_MouseDown(object sender, MouseEventArgs e)
        {
            DoDragDrop(listPlayers.SelectedItem.ToString(), DragDropEffects.Copy);
        }

I also believe this is correct for the dragEnter event for the label:

private void posLB_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Text))
                e.Effect = DragDropEffects.Copy;
            else
                e.Effect = DragDropEffects.None;
        }

However, I have no idea how to get the DragDrop event working for the label. I thought it would be something like this:

private void posLB_DragDrop(object sender, DragEventArgs e)
        {
            posLB.text(e.Data.GetData(DataFormats.Text);
        }

But that has errors.

Was it helpful?

Solution

Test for correct type and then grab it:

    private void posLB_DragDrop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.Text))
        {
            String s = e.Data.GetData(DataFormats.Text) as String;
            if (!String.IsNullOrEmpty(s))
                posLB.Text = s;
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top