开发者

Simple Drag & Drop Event Handling problem

I am new to using E开发者_运维技巧vent Handling in C# .NET, and I am trying to understand the behavior behind some simple code that I am experimenting with. I am working with a more complicated example, but I am hoping I will get a more focused answer if I simplify the example.

I have the following code which defines a main window with a ListBox that is initialized with values, and a panel in the window. I am working with dragging the ListBox Items and dropping them in the panel. To signify that the panel is reading the DragDrop event, I am simply just changing the background color.

My problem is, it is not changing the background color when I drop the values, hence, the DragDrop is not working. I know this is a bit exaggerated, but I am trying to understand why its not working.

Here is the following code that I am using.

     public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        //Allow Panel to accept dropped values
        this.panel1.AllowDrop = true;

        //Initialize ListBox with sample values
        listBox1.Items.Add("First Name");
        listBox1.Items.Add("Last Name");
        listBox1.Items.Add("Phone");

        //Setup DragDrop Event Handler - is this correct, or even needed?
        this.panel1.DragDrop += new DragEventHandler(panel1_DragDrop);
    }

    private void listBox1_MouseDown(object sender, MouseEventArgs e)
    {
        ListBox box = (ListBox)sender;
        String selectedValue = box.Text;
        DoDragDrop(selectedValue.ToString(), DragDropEffects.Copy);
    }

    private void panel1_DragDrop(object sender, DragEventArgs e)
    {
        //Change Background color to signify value has been dropped
        ((Panel)sender).BackColor = Color.Black;
    }

}

I realize this is an oversimplified example. If you see what I am doing wrong, then please let me know.

EDIT To give an example of why I am confused, I change this example around to put the dragged ListBox Item text into a Textbox when the DragOver event was fired, and it worked fine, but when I tried to do the same thing when they dropped the values over the textbox, I could not get it to work.


Handle the panel's DragEnter event and set e.Effects to something other than None.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜