ListBox控件实现上移、下移、循环上移、循环下移操作
2016-04-10 12:40
701 查看
一、先在前台页面中拖入一个listbox控件和四个button控件,可以对它们的样式进行一下修改。
<style type="text/css">
.left {
float:left;
width:120px;
}
.right {
float:right;
width:80px;
}
.all {
width:200px;
}
</style>
二,在后台相对应的button按钮的click方法中加入代码。
protected void Button1_Click(object sender, EventArgs e)
{
//ListBox1.Items.Remove(ListBox1.SelectedItem);
//上移
if (ListBox1.SelectedIndex > 0) {
int idx = ListBox1.SelectedIndex;
ListBox1.Items.Insert(ListBox1.SelectedIndex - 1, ListBox1.SelectedItem.ToString());
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex);
ListBox1.SelectedIndex = idx - 1;
}
}
protected void Button2_Click(object sender, EventArgs e)
{
//下移
if (ListBox1.SelectedIndex < ListBox1.Items.Count - 1)
{
ListBox1.Items.Insert(ListBox1.SelectedIndex, ListBox1.Items[ListBox1.SelectedIndex + 1].ToString());
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex + 1);
}
}
protected void Button3_Click(object sender, EventArgs e)
{
//循环上移
if (ListBox1.SelectedIndex == 0) {
ListBox1.Items.Insert(ListBox1.Items.Count,ListBox1.SelectedItem.ToString());
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex);
}
}
protected void Button4_Click(object sender, EventArgs e)
{
//循环下移
if (ListBox1.SelectedIndex == ListBox1.Items.Count-1) {
ListBox1.Items.Insert(0, ListBox1.SelectedItem.ToString());
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex);
}
}
<style type="text/css">
.left {
float:left;
width:120px;
}
.right {
float:right;
width:80px;
}
.all {
width:200px;
}
</style>
二,在后台相对应的button按钮的click方法中加入代码。
protected void Button1_Click(object sender, EventArgs e)
{
//ListBox1.Items.Remove(ListBox1.SelectedItem);
//上移
if (ListBox1.SelectedIndex > 0) {
int idx = ListBox1.SelectedIndex;
ListBox1.Items.Insert(ListBox1.SelectedIndex - 1, ListBox1.SelectedItem.ToString());
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex);
ListBox1.SelectedIndex = idx - 1;
}
}
protected void Button2_Click(object sender, EventArgs e)
{
//下移
if (ListBox1.SelectedIndex < ListBox1.Items.Count - 1)
{
ListBox1.Items.Insert(ListBox1.SelectedIndex, ListBox1.Items[ListBox1.SelectedIndex + 1].ToString());
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex + 1);
}
}
protected void Button3_Click(object sender, EventArgs e)
{
//循环上移
if (ListBox1.SelectedIndex == 0) {
ListBox1.Items.Insert(ListBox1.Items.Count,ListBox1.SelectedItem.ToString());
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex);
}
}
protected void Button4_Click(object sender, EventArgs e)
{
//循环下移
if (ListBox1.SelectedIndex == ListBox1.Items.Count-1) {
ListBox1.Items.Insert(0, ListBox1.SelectedItem.ToString());
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex);
}
}
相关文章推荐
- 分享微信开发Html5轻游戏中的几个坑
- 星外ASP.Net的安全设置相关说明
- flex 控件的重要属性
- C#、ASP.NET通用扩展工具类之TypeParse
- Delphi控件ListView的属性及使用方法详解
- web下载的ActiveX控件自动更新
- 实现ASP.NET无刷新下载并提示下载完成的开发思路
- C#、ASP.NET通用扩展工具类之LogicSugar
- WinForm实现按名称递归查找控件的方法
- C#中父窗口和子窗口之间控件互操作实例
- C#、ASP.NET通用工具类IsWhat?(可以判断数字、身份证、数据类型等等)
- 程序中常用的种代码
- Android编程之Button控件用法实例分析
- Android控件之CheckBox、RadioButton用法实例分析
- 在Android开发中使用自定义组合控件的例子
- ASP.NET、ASP、PHP、JSP之间有什么区别?
- ASP.NET页面间的传值的几种方法
- MFC中动态创建控件以及事件响应实现方法
- WinForm自定义函数FindControl实现按名称查找控件
- Android控件之ProgressBar用法实例分析