俩个Button事件的代码(实现播放列表中的某项上移一位),两个事件只有listBox的Name不同,我想同过写一个通用方法来提供两个事件调用,不知这个通用方法的参数怎么写?
其中一个事件代码如下
private void upmovebnt_Click(object sender, EventArgs e)
        {
            try
            {
                int selectindex = this.movelistBox.SelectedIndex;
                //MessageBox.Show(selectindex.ToString());
                if (selectindex == -1)
                    MessageBox.Show("请选择上移项!");
                if (selectindex > 0)
                {
                    string selectItem = this.movelistBox.Items[this.movelistBox.SelectedIndex].ToString();
                    this.movelistBox.Items.Remove(this.movelistBox.Items[this.movelistBox.SelectedIndex]);
                    this.movelistBox.Items.Insert(selectindex - 1, selectItem);
                    this.movelistBox.SelectedIndex = selectindex - 1;
                }
                else if (selectindex != -1)
                {
                    MessageBox.Show("已经是第一条了!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }另一个事件代码:
try
            {
                int selectindex = this.movelistBox.SelectedIndex;
                if (selectindex == -1)
                    MessageBox.Show("请选择上移项!");
                if (selectindex > 0)
                {
                    string selectItem = this.movelistBox.Items[this.movelistBox.SelectedIndex].ToString();
                    this.movelistBox.Items.Remove(this.movelistBox.Items[this.movelistBox.SelectedIndex]);
                    this.movelistBox.Items.Insert(selectindex - 1, selectItem);
                    this.movelistBox.SelectedIndex = selectindex - 1;
                }
                else if (selectindex != -1)
                {
                    MessageBox.Show("已经是第一条了!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

解决方案 »

  1.   

    写错了,第一个事件的代码应该是:
    try
                {
                    int selectindex = this.picturlist.SelectedIndex;
                    if (selectindex == -1)
                        MessageBox.Show("请选择上移项!");
                    if (selectindex > 0)
                    {
                        string selectItem = this.picturlist.Items[this.picturlist.SelectedIndex].ToString();
                        this.picturlist.Items.Remove(this.picturlist.Items[this.picturlist.SelectedIndex]);
                        this.picturlist.Items.Insert(selectindex - 1, selectItem);
                        this.picturlist.SelectedIndex = selectindex - 1;
                    }
                    else if (selectindex != -1)
                    {
                        MessageBox.Show("已经是第一条了!");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
      

  2.   

    两个控件的事件指向同一个方法。方法里:
    ....
    ListBox listBox=(ListBox)sender;
    listBox.XXXXXX
    ......
      

  3.   


     public void Move(ListBox lb)
            {
                try
                {
                    int selectindex = this.lb.SelectedIndex;
                    //MessageBox.Show(selectindex.ToString());
                    if (selectindex == -1)
                        MessageBox.Show("请选择上移项!");
                    if (selectindex > 0)
                    {
                        string selectItem = this.lb.Items[this.lb.SelectedIndex].ToString();
                        this.lb.Items.Remove(this.lb.Items[this.lb.SelectedIndex]);
                        this.lb.Items.Insert(selectindex - 1, selectItem);
                        this.lb.SelectedIndex = selectindex - 1;
                    }
                    else if (selectindex != -1)
                    {
                        MessageBox.Show("已经是第一条了!");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
      

  4.   


    不好意思,没有发现2楼就回复了,3楼:这个事件是button触发的,不是listbox,所以你这个sender貌似不对
      

  5.   

                Button btn = (Button)sender;
                ListBox lb = null;            if (btn.Text == "upmovebnt按钮上的Text")  //upmovebnt按钮上的Text
                {
                    lb = this.movelistBox;
                }
                else
                {
                    lb = this.picturlist;
                }
                try
                {
                    int selectindex = lb.SelectedIndex;
                    if (selectindex == -1)
                        MessageBox.Show("请选择上移项!");
                    if (selectindex > 0)
                    {
                        string selectItem = lb.Items[lb.SelectedIndex].ToString();
                        lb.Items.Remove(lb.Items[lb.SelectedIndex]);
                        lb.Items.Insert(selectindex - 1, selectItem);
                        lb.SelectedIndex = selectindex - 1;
                    }
                    else if (selectindex != -1)
                    {
                        MessageBox.Show("已经是第一条了!");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
      

  6.   


                ListBox lb = null;            if (sender == upmovebtn)  
                {
                    lb = this.movelistBox;
                }
                else
                {
                    lb = this.picturlist;
                }
                try
                {
                    int selectindex = lb.SelectedIndex;
                    if (selectindex == -1)
                        MessageBox.Show("请选择上移项!");
                    if (selectindex > 0)
                    {
                        string selectItem = lb.Items[lb.SelectedIndex].ToString();
                        lb.Items.Remove(lb.Items[lb.SelectedIndex]);
                        lb.Items.Insert(selectindex - 1, selectItem);
                        lb.SelectedIndex = selectindex - 1;
                    }
                    else if (selectindex != -1)
                    {
                        MessageBox.Show("已经是第一条了!");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
      

  7.   

    那你咋知道你要移动哪个lixtbox的行?你得有个地方知道你选中了哪一个LixtBox
      

  8.   

    我问之前就是过这样写,会报错,Move中的lb没定义
      

  9.   

    通用方法如下:
     public void  Move(object sender, EventArgs e)
            {
                //Button btn = (Button)sender;
                ListBox lb = null;            if (sender == upmovebnt)
                {
                    lb = this.movelistBox;
                }
                else 
                {
                    lb = this.picturlist;
                }
               try
                {
                    int selectindex = lb.SelectedIndex;
                    if (selectindex == -1)
                        MessageBox.Show("请选择上移项!");
                    if (selectindex > 0)
                    {
                        string selectItem = lb.Items[lb.SelectedIndex].ToString();
                        lb.Items.Remove(lb.Items[lb.SelectedIndex]);
                        lb.Items.Insert(selectindex - 1, selectItem);
                        lb.SelectedIndex = selectindex - 1;
                    }
                    else if (selectindex != -1)
                    {
                        MessageBox.Show("已经是第一条了!");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }两个Button事件调用
    this.Move(sender,e);
      

  10.   

    这涉及到了方法重构,你可以抽取出可以共用的方法,例如你的例子中,如果只是sender不同,那么你可以把事件处理程序抽象成一个方法,这个方法的sender在调用时,由对应控件的相应事件程序调用该方法是,动态地指定,然后你抽象的这个方法,需要做一个判断,以便处理不同类型的sender。楼上各位基本都给出了具体的做法,你可以参考了。
      

  11.   

    -.- 你LB是 move()的参数, 调用干啥还加一个 this.lb??? 很奇怪也...楼上给出思路了啊. 你当然有办法知道当前应该操作哪一个... 然后按照情况相应吗.
      

  12.   


    是应该把所有的this去掉,调用this.Move(this.movelistBox)
    public void Move(ListBox lb)
            {
                try
                {
                    int selectindex = lb.SelectedIndex;
                    //MessageBox.Show(selectindex.ToString());
                    if (selectindex == -1)
                        MessageBox.Show("请选择上移项!");
                    if (selectindex > 0)
                    {
                        string selectItem = lb.Items[lb.SelectedIndex].ToString();
                        lb.Items.Remove(lb.Items[lb.SelectedIndex]);
                        lb.Items.Insert(selectindex - 1, selectItem);
                        lb.SelectedIndex = selectindex - 1;
                    }
                    else if (selectindex != -1)
                    {
                        MessageBox.Show("已经是第一条了!");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }这样就可以了。
      

  13.   

    有类似的这种,都可以先取sender得到控件的
      

  14.   

    谢谢xiongxyt2,你的两种方法均可以。