vs2005制做左右上下移动的Listbox ( asp.net , C#)

来源:互联网 发布:人本帆布鞋 知乎 编辑:程序博客网 时间:2024/04/28 07:49
制作可上下左右移动的 ListBox:

方法:

1.新建页面,拖放如下控件: 两个ListBox ,6个button,样式如下

 

各个button的功能

>> 左移

<< 右移

↑上移

↓下移

C全部清空

>> 代码如下:

protected void Button1_Click(object sender, EventArgs e)
    
{
        
try
        
{
            
this.ListBox2.Items.Add(this.ListBox1.SelectedItem.Text.ToString());
        }

        
catch (Exception)
        
{
           
// this.Response.Write("<script language=javascript>alert('Please select some items before you click it!');history.go(-1)</script>");
        }

    }


<<代码如下:

 
protected void Button2_Click(object sender, EventArgs e)
    
{
        
try
        
{
            
this.ListBox2.Items.Remove(this.ListBox2.SelectedItem.Text.ToString());
        }

        
catch (Exception)
        
{
            
//this.Response.Write("<script language=javascript>alert('Please select some items before you click it!');history.go(-1)</script>");
        }

    }


↑代码如下:

 
protected void Button4_Click(object sender, EventArgs e)
    
{
        
try
        
{
            
int i = this.ListBox2.SelectedIndex;
            
int j = this.ListBox2.Items.Count;
            
if ((j > i) && (i != 0))
            
{
                
string sitem = this.ListBox2.SelectedItem.Text.ToString();
                
this.ListBox2.Items.RemoveAt(i);
                
this.ListBox2.Items.Insert(i - 1, sitem);
                
this.ListBox2.SelectedIndex = i - 1;

            }

        }

        
catch (Exception)
        
{
            
//this.Response.Write("<script language=javascript>alert('Please select some items before you click it!');history.go(-1)</script>");
        }

    }


↓代码如下:

 
protected void Button3_Click(object sender, EventArgs e)
    
{
        
try
        
{
            
int i = this.ListBox2.SelectedIndex;
            
int j = this.ListBox2.Items.Count;
            
if ((j > i) && (i != j - 1))
            
{
                
string sitem = this.ListBox2.SelectedItem.Text.ToString();
                
this.ListBox2.Items.RemoveAt(i);
                
this.ListBox2.Items.Insert(i + 1, sitem);
                
this.ListBox2.SelectedIndex = i + 1;

            }

        }

        
catch (Exception)
        
{
            
//this.Response.Write("<script language=javascript>alert('Please select some items before you click it!');history.go(-1)</script>");

        }

    }


C代码如下:

protected void Button6_Click(object sender, EventArgs e)
    
{
        
this.ListBox2.Items.Clear();
    }


最后显示如图:
 
原创粉丝点击