俩个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);
}
其中一个事件代码如下
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);
}
解决方案 »
- 求教 2个二维矩阵取交集的精简算法
- C#关于委托的问题。 C#的代码真心难搞,搞了一天没搞出来。
- .net读取txt问题
- C#中有对基础类型的封装类吗?
- 如何把类似 \ue2b7\ue9b1\uf09f\uf7bd\ufea7\u05b7\u0ca5 这种unicode编码转成汉字输出?
- 如何进行加法运算?
- 如何用index特性来删除列表框的值
- 新任务 做天猫数据接口,目前不知道如何入手,有搞过的求共享资料和方法,谢谢,N久没发帖了
- 在winform中dataGrid排序后怎么取出对应的DataTable?
- WPF中MainWindow..xaml.cs如何调用App.xaml.cs中的方法
- 求读取文字到combobox的函数
- 我能不能指定一些cookie值,然后用ie打开特定网址?
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);
}
....
ListBox listBox=(ListBox)sender;
listBox.XXXXXX
......
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);
}
}
不好意思,没有发现2楼就回复了,3楼:这个事件是button触发的,不是listbox,所以你这个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);
}
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);
}
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);
是应该把所有的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);
}
}这样就可以了。