You can try this approach.
Use generic event handlers and connect to your various controls.
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.
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.