110,538
社区成员
发帖
与我相关
我的任务
分享
001 张三
002 李四
003 王五
当我点击李四上升的时候应该显示为
001 李四
002 张三
003 王五
private void button1_Click_1(object sender, EventArgs e)
{
listBox1.Items.Add("001\t张三");
listBox1.Items.Add("002\t李四");
listBox1.Items.Add("003\t王五");
}
/* 001 张三
* 002 李四
* 003 王五
* 每行间隔是以制表位(Tab)"\t"为分隔符
*/
//向上移动
private void button2_Click(object sender, EventArgs e)
{
int index = listBox1.SelectedIndex;
if (index == -1) return;
if (index == 0) return;
string []strs = listBox1.Items[index].ToString().Split('\t');
string[] strs2 = listBox1.Items[index-1].ToString().Split('\t');
string temp = string.Empty;
temp = strs[1];
strs[1] = strs2[1];
strs2[1] = temp;
listBox1.Items[index] = strs[0] + "\t" + strs[1];
listBox1.Items[index-1] = strs2[0] + "\t" + strs2[1];
listBox1.SelectedIndex = index - 1;
}
//向下移动
private void button3_Click(object sender, EventArgs e)
{
int index = listBox1.SelectedIndex;
if (index == -1) return;
if (index == listBox1.Items.Count-1) return;
string[] strs = listBox1.Items[index].ToString().Split('\t');
string[] strs2 = listBox1.Items[index + 1].ToString().Split('\t');
string temp = string.Empty;
temp = strs[1];
strs[1] = strs2[1];
strs2[1] = temp;
listBox1.Items[index] = strs[0] + "\t" + strs[1];
listBox1.Items[index + 1] = strs2[0] + "\t" + strs2[1];
listBox1.SelectedIndex = index+ 1;
}