您的位置:首页 > 编程语言 > C#

在C#中实现listbox的项上下移动(winform)

2012-03-26 11:55 507 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)
{
try
{
string item = listBox1.SelectedItem.ToString();
int i = listBox1.SelectedIndex;
if (i == 0)
return;
listBox1.Items.Remove(listBox1.SelectedItem.ToString());
listBox1.Items.Insert(i - 1, item);
listBox1.SelectedIndex = i - 1;
}
catch (Exception)
{
MessageBox.Show("未选择项!");
}

}

private void button2_Click(object sender, EventArgs e)
{
try
{
string item = listBox1.SelectedItem.ToString();
int i = listBox1.SelectedIndex;
if (i == listBox1.Items.Count - 1)
return;
listBox1.Items.Remove(listBox1.SelectedItem.ToString());
listBox1.Items.Insert(i + 1, item);
listBox1.SelectedIndex = i + 1;
}
catch (Exception)
{

MessageBox.Show("未选择项!");
}

}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: