天天看點

listbox控件用法詳解

http://blog.sina.com.cn/s/blog_61e2b6280100svtp.html

1. 屬性清單:

    SelectionMode    元件中條目的選擇類型,即多選(Multiple)、單選(Single)

    Rows             清單框中顯示總共多少行

    Selected         檢測條目是否被選中

    SelectedItem     傳回的類型是ListItem,獲得清單框中被選擇的條目

    Count            清單框中條目的總數

    SelectedIndex    清單框中被選擇項的索引值

    Items            泛指清單框中的所有項,每一項的類型都是ListItem

2. 取清單框中被選中的值  

     ListBox.SelectedValue  

3. 動态的添加清單框中的項:

     ListBox.Items.Add("所要添加的項");

4. 移出指定項:

     //首先判斷清單框中的項是否大于0

     If(ListBox.Items.Count > 0 )

     {

//移出選擇的項

ListBox.Items.Remove(ListBox.SelectedItem);

     }

5. 清空所有項:

//清空所有項

ListBox.Items.Clear();

6. 清單框可以一次選擇多項:

     隻需設定清單框的屬性 SelectionMode="Multiple",按Ctrl可以多選

7. 兩個清單框關聯,即兩級關聯菜單

     //判斷第一個清單框中被選中的值

     switch(ListBox1.SelectValue)

//如果是"A",第二個清單框中就添加這些:

case "A"

      ListBox2.Items.Clear();

      ListBox2.Items.Add("A1");

      ListBox2.Items.Add("A2");

      ListBox2.Items.Add("A3");

//如果是"B",第二個清單框中就添加這些:

case "B"

      ListBox2.Items.Add("B1");

      ListBox2.Items.Add("B2");

      ListBox2.Items.Add("B3");

8. 實作清單框中項的移位

     即:向上移位、向下移位

     具體的思路為:建立一個ListBox對象,并把要移位的項先暫放在這個對象中。

     如果是向上移位,就是把目前標明項的的上一項的值賦給目前標明的項,然後

     把剛才新加入的對象的值,再附給目前標明項的前一項。

     具體代碼為:

      //定義一個變量,作移位用

      index = -1;

      //将目前條目的文本以及值都儲存到一個臨時變量裡面

      ListItem lt=new ListItem (ListBox.SelectedItem.Text,ListBox.SelectedValue);

      //被選中的項的值等于上一條或下一條的值

      ListBox.Items[ListBox.SelectedIndex].Text=ListBox.Items[ListBox.SelectedIndex + index].Text;

      ListBox.Items[ListBox.SelectedIndex].Value=ListBox.Items[ListBox.SelectedIndex + index].Value;

      //把被選中項的前一條或下一條的值用臨時變量中的取代

      ListBox.Items[ListBox.SelectedIndex].Test=lt.Test;

      ListBox.Items[ListBox.SelectedIndex].Value=lt.Value;

      //把滑鼠指針放到移動後的那項上

9. 移動指針到指定位置:

      (1).移至首條

          //将被選中項的索引設定為0就OK了

          ListBox.SelectIndex=0;

      (2).移至尾條

          //将被選中項的索引設定為ListBox.Items.Count-1就OK了

          ListBox.SelectIndex=ListBox.Items.Count-1;

      (3).上一條

          //用目前被選中的索引去減 1

          ListBox.SelectIndex=ListBox.SelectIndex - 1;

      (4).下一條

          //用目前被選中的索引去加 1

          ListBox.SelectIndex=ListBox.SelectIndex + 1;

出自 51CTO.COM部落格

this.ListBox1.Items.Insertat(3,new   ListItem("插入在第3行之後項",""));  

this.ListBox1.Items.Insertat(index,ListItem)

ListBox1.Items.Insert(0,new   ListItem("text","value"));

ASP.NET中添加控件ListBox , 屬性設為 Multiple , 則可進行多選.

就以兩個listbox之間多選添加項目為例.

兩個控件為listboxleft , listboxright 定義了一個動态數組用于中間存儲 arrRight .具體代碼如下:

//讀取右邊選中項目

   ArrayList arrRight = new ArrayList();

   foreach(ListItem item in this.ListBoxRight.Items) //按類型listitem讀取listbox中標明項

   {

    if(item.Selected) //判斷是否選中

    {

     arrRight.Add(item);

    }

   }  

   //右邊移除標明項目 左邊添加

   foreach(ListItem item in arrRight)

    this.ListBoxLeft.Items.Add(item);

    this.ListBoxRight.Items.Remove(item);

   }

不能将item的添加删除直接寫在if(item.Selected){}内,因為項目remove後會出現錯誤

 Move the Item of ListBox

.aspx

--------------------------------

<asp:ListBox id="ListBox1" style="Z-INDEX: 105; LEFT: 96px; POSITION: absolute; TOP: 344px" runat="server">

    <asp:ListItem Value="a1">a1</asp:ListItem>

    <asp:ListItem Value="a2">a2</asp:ListItem>

    <asp:ListItem Value="a3">a3</asp:ListItem>

   </asp:ListBox>

----------------------------------

.cs

//for 個

private void Button1_Click(object sender, System.EventArgs e){

   if (this.ListBox1.SelectedIndex>=0)

    int i = ListBox1.SelectedIndex;

    if(i>0)

     string values = this.ListBox1.Items[i].Text ;

     this.ListBox1.Items[i].Text = this.ListBox1.Items[i-1].Text ;

     this.ListBox1.Items[i-1].Text = values;

}

ASP.NET中ListBox實作Double Click事件

2007-06-24 13:18

在ASP.NET中的ListBox控件的使用中很多人都發現沒有了WINFORM中ListBox的滑鼠輕按兩下事件

這樣就給我們開發帶來很多不友善的地方

也看了很多CSDN上用JS來實作輕按兩下事件的方法,都是不完整的,最後發現以下方法是最有效的

首先在WEB頁面上加入JS腳本和存放ListBox事件的隐藏輸入框

再将ASP.NET控件ListBox中加入輕按兩下事件聲明

<html>

<head>

     <script language="javascript">

     function ListBox1_DoubleClick() {

        document.forms[0].ListBox1Hidden.value = "doubleclicked";

        document.forms[0].submit();

</script>

</head>

<body>

     <form runat="server">

         <div>Double click on Listbox

             <br />

             <asp:ListBox id="ListBox1"

                     οndblclick="ListBox1_DoubleClick()" runat="server">

                 <asp:ListItem Value="1">One</asp:ListItem>

                 <asp:ListItem Value="2">Two</asp:ListItem>

                 <asp:ListItem Value="3">Three</asp:ListItem>

                 <asp:ListItem Value="4">Four</asp:ListItem>

             </asp:ListBox>

             <input type="hidden" name="ListBox1Hidden" />

         </div>

         <div>click on button

             <asp:Button id="Button1" οnclick="Button1_Click"

                 runat="server" Text="Button"/>

     </form>

</body>

</html>

最後在WEB窗體加載時候執行下列代碼就能實作輕按兩下ListBox中Item執行一些操作

void Page_Load(Object sender, EventArgs e){

   if(Request.Params["ListBox1Hidden"] != null

     && (string)Request.Params["ListBox1Hidden"] == "doubleclicked" {

    //This means It was double click

     Response.Write("Double Click was fired selected item is "

     + ListBox1.SelectedItem.Text);

     //可以在這裡加要運作的代碼

希望這篇文章能對大家有幫助,不要再受CSDN上文章的誤導了

ListBox基本功能首先是清單項的添加,用戶端實作代碼添加在listbox執行個體化代碼中間,例如:

<asp:ListItem Value="value" Selected=True>Text</asp:ListItem>

若在伺服器端實作,為避免每次加載時執行添加清單項,上述代碼包含在下面代碼中:

if(!IsPostBack)

{

WebForm頁面上須添加2個listbox(listbox1和lixtbox2)和2個指令按鈕,listbox1不為空。清單項從listbox1添加到listbox2須在Button1單擊事件中調用Add方法:

ListBox2.Items.Add(ListBox1.SelectedValue);

若要從listbox2中删除清單項的話須在Button2單擊事件中調用Remove方法:

ListBox2.Items.Remove(ListBox2.SelectedValue);

清單項從listbox1添加到listbox2後,清單項從listbox1中删除:

int i=0;

while(i<ListBox1.Items.Count)

if(ListBox1.Items[i].Selected==true)

ListBox2.Items.Add(ListBox1.Items[i]);

ListBox1.Items.Remove(ListBox1.Items[i]);

else

i+=1;

這樣隻能實作單項添加,想要實作多項添加,首先設定ListBox1的SelectionMode屬性值Multiple,ListBox1允許多項選中。

在Button1單擊事件中添加

foreach(ListItem MyItem in ListBox1.Items)

if(MyItem.Selected==true)

ListBox2.Items.Add(MyItem);

想要一次清空ListBox2中所有選項可在Button2單擊事件中調用clear方法,

ListBox2.Items.Clear();

若清單項已經添加,不允許二次添加,Button1單擊事件中的代碼包含在:

if(ListBox2.Items.FindByValue(ListBox1.SelectedValue)==null)

ListBox與資料庫綁定就是指定他的DataSource和DataTextField屬性,

ListBox2.DataSource=資料源;

ListBox2.DataTextField="字段名";

ListBox2.DataBind();

如果是在背景做的話

AutoPostBack =true

然後給第一個listbox加事件(SelectedIndexChanged)

然後一個周遊就可以出來了

[Copy to clipboard] [ - ]

CODE:

private void ListBox1_SelectedIndexChanged(object sender, System.EventArgs e)

        {

            for(int i=0;i<ListBox1.Items.Count;i++){

                    ListBox2.Items[i].Selected=ListBox1.Items[i].Selected;

            }

        }

如果是前台js的話,這裡讨論過很多,找找看,思路是一樣的。

繼續閱讀