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

 
製作可上下左右移動的 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();
    }


最後顯示如圖:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章