拖动n drop auto事件处理程序以获取更多文本框 [英] drag n drop auto event handler for more textboxes

查看:48
本文介绍了拖动n drop auto事件处理程序以获取更多文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了listbox1,10 textBox1,textbox2 ............... textbox10。

i应用了从listbox1拖放到textbox1

它被解雇了。

i used listbox1,10 textBox1,textbox2...............textbox10.
i applied drag n drop from listbox1 to textbox1
it fired ok.

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());
}



但我自动从textbox1中选择一个事件处理程序到textbox2

但是当拖放到textbox2时它没有被触发解雇textbox1只有textbiox2

i有一个解决方案,如果我手动编写代码everty textboxes.but它将需要更多时间,

所有文本框自动事件处理程序使用上面的任何解决方案的想法将为所有文本框触发的一个代码


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

推荐答案

简单:每个事件处理程序都传递两个参数 - sender e

sender 参数如果引发的控件(或其他类)实例事件。

因此,如果您将参数转换为文本框:

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:
private void MyTextBox_DragEnter(object sender, DragEventArgs e)
   {
   TextBox source = sender as TextBox;
   if (source != null)
      {
      try

source 将为其事件提供textBox1,为事件提供textBox2。

然后您可以使用相同的代码来处理所有框中的拖放。

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.


您可以尝试这种方法。

使用通用事件处理程序并连接到各种控件。

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





然后使用 sender 参数获取实例调用对象。



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.


这篇关于拖动n drop auto事件处理程序以获取更多文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆