Click here to Skip to main content
15,885,941 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i used listbox1,10 textBox1,textbox2...............textbox10.
i applied drag n drop from listbox1 to textbox1
it fired ok.
C#
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
    try
    {
        listBox1.DoDragDrop(listBox1.Items[listBox1.SelectedIndex].ToString(),
                            DragDropEffects.Move);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

private void textBox1_DragEnter(object sender, DragEventArgs e)
{
    try
    {
        if (e.Data.GetDataPresent(DataFormats.Text))
        {
            e.Effect = DragDropEffects.Move;
        }
        else
        {
            e.Effect = DragDropEffects.None;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

private void txtwpAM1_DragDrop(object sender, DragEventArgs e)
{
    txtwpAM1.Text = System.Convert.ToString(e.Data.GetData(DataFormats.Text).ToString());
}

but i auto select an event handler to textbox2 from textbox1
but its not fired when drag n drop to textbox2 its fired textbox1 only not textbiox2
i have one solution if i write code manually everty textboxes.but it will take more time,
any soulution idea for all textboxes auto event handler using above one code it will fired for all textboxes
Posted
Updated 6-Sep-14 5:05am
v3

Simple: every event handler is passed two parameters - sender and e
The sender parameter if the control (or other class) instance that raise the event.
So if you cast the parameter to a textbox:
C#
private void MyTextBox_DragEnter(object sender, DragEventArgs e)
   {
   TextBox source = sender as TextBox;
   if (source != null)
      {
      try
The source will then be textBox1 for it's event and textBox2 for it's events.
You can then use the same code to handle the drag / drop in all the boxes.
 
Share this answer
 
You can try this approach.
Use generic event handlers and connect to your various controls.
C#
listBox1.MosueDown += listBox_Generic_MouseDown;
listBox2.MosueDown += listBox_Generic_MouseDown;
etc.

textBox1.DragEnter += textBox_Generic_DragEnter;
textBox2.DragEnter += textBox_Generic_DragEnter;
etc

textBox1.DragDrop += textBox_Generic_DragDrop;
textBox2.DragDrop += textBox_Generic_DragDrop;
etc


Then use the sender argument to get the instance of the calling object.
C#
private void listBox_Generic_MouseDown(object sender, MouseEventArgs e)
{
    try
    {
        ListBox listBox = (sender as ListBox);
        if (listBox != null)
            listBox.DoDragDrop(listBox.Items[listBox1.SelectedIndex].ToString(),
                            DragDropEffects.Move);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
 
private void textBox_Generic_DragEnter(object sender, DragEventArgs e)
{
    try
    {
        if (e.Data.GetDataPresent(DataFormats.Text))
        {
            e.Effect = DragDropEffects.Move;
        }
        else
        {
            e.Effect = DragDropEffects.None;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
 
private void textBox_Generic_DragDrop(object sender, DragEventArgs e)
{
    TextBox textBox = (sender as TextBox);
    if (textBox != null)
        textBox.Text = System.Convert.ToString(e.Data.GetData(DataFormats.Text).ToString());
}


In this fashion you only have one implementation of each event handler.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900