《WebForm开发系列之控件篇》Item2 ListBox
1. 屬性列表:
???? SelectionMode???? 組件中條目的選擇類型,即多選(Multiple)、單選(Single)
???? Rows????????????? 列表框中顯示總共多少行
???? Selected????????? 檢測(cè)條目是否被選中
???? SelectedItem????? 返回的類型是ListItem,獲得列表框中被選擇的條目
???? Count???????????? 列表框中條目的總數(shù)
???? SelectedIndex???? 列表框中被選擇項(xiàng)的索引值
???? Items???????????? 泛指列表框中的所有項(xiàng),每一項(xiàng)的類型都是ListItem
2. 取列表框中被選中的值?
????? ListBox.SelectedValue?
3. 動(dòng)態(tài)的添加列表框中的項(xiàng):
????? ListBox.Items.Add("所要添加的項(xiàng)");
4. 移出指定項(xiàng):
????? //首先判斷列表框中的項(xiàng)是否大于0
????? If(ListBox.Items.Count > 0 )
????? {
//移出選擇的項(xiàng)
ListBox.Items.Remove(ListBox.SelectedItem);
????? }
5. 清空所有項(xiàng):
????? //首先判斷列表框中的項(xiàng)是否大于0
????? If(ListBox.Items.Count > 0 )
????? {
//清空所有項(xiàng)
ListBox.Items.Clear();
????? }
6. 列表框可以一次選擇多項(xiàng):
??
????? 只需設(shè)置列表框的屬性 SelectionMode="Multiple",按Ctrl可以多選
7. 兩個(gè)列表框聯(lián)動(dòng),即兩級(jí)聯(lián)動(dòng)菜單
????? //判斷第一個(gè)列表框中被選中的值
????? switch(ListBox1.SelectValue)
????? {
//如果是"A",第二個(gè)列表框中就添加這些:
case "A"
?????? ListBox2.Items.Clear();
?????? ListBox2.Items.Add("A1");
?????? ListBox2.Items.Add("A2");
?????? ListBox2.Items.Add("A3");
//如果是"B",第二個(gè)列表框中就添加這些:
case "B"
?????? ListBox2.Items.Clear();
?????? ListBox2.Items.Add("B1");
?????? ListBox2.Items.Add("B2");
?????? ListBox2.Items.Add("B3");
????? }
8. 實(shí)現(xiàn)列表框中項(xiàng)的移位
????? 即:向上移位、向下移位
????? 具體的思路為:創(chuàng)建一個(gè)ListBox對(duì)象,并把要移位的項(xiàng)先暫放在這個(gè)對(duì)象中。
????? 如果是向上移位,就是把當(dāng)前選定項(xiàng)的的上一項(xiàng)的值賦給當(dāng)前選定的項(xiàng),然后
????? 把剛才新加入的對(duì)象的值,再附給當(dāng)前選定項(xiàng)的前一項(xiàng)。
????? 具體代碼為:
?????? //定義一個(gè)變量,作移位用
?????? index = -1;
?????? //將當(dāng)前條目的文本以及值都保存到一個(gè)臨時(shí)變量里面
?????? ListItem lt=new ListItem (ListBox.SelectedItem.Text,ListBox.SelectedValue);
?????? //被選中的項(xiàng)的值等于上一條或下一條的值
?????? ListBox.Items[ListBox.SelectedIndex].Text=ListBox.Items[ListBox.SelectedIndex + index].Text;
?????? //被選中的項(xiàng)的值等于上一條或下一條的值
?????? ListBox.Items[ListBox.SelectedIndex].Value=ListBox.Items[ListBox.SelectedIndex + index].Value;
?????? //把被選中項(xiàng)的前一條或下一條的值用臨時(shí)變量中的取代
?????? ListBox.Items[ListBox.SelectedIndex].Test=lt.Test;
?????? //把被選中項(xiàng)的前一條或下一條的值用臨時(shí)變量中的取代
?????? ListBox.Items[ListBox.SelectedIndex].Value=lt.Value;
?????? //把鼠標(biāo)指針?lè)诺揭苿?dòng)后的那項(xiàng)上
?????? ListBox.Items[ListBox.SelectedIndex].Value=lt.Value;
9. 移動(dòng)指針到指定位置:
?????? (1).移至首條
?????????? //將被選中項(xiàng)的索引設(shè)置為0就OK了
?????????? ListBox.SelectIndex=0;
?????? (2).移至尾條
?????????? //將被選中項(xiàng)的索引設(shè)置為L(zhǎng)istBox.Items.Count-1就OK了
?????????? ListBox.SelectIndex=ListBox.Items.Count-1;
?????? (3).上一條
?????????? //用當(dāng)前被選中的索引去減 1
?????????? ListBox.SelectIndex=ListBox.SelectIndex - 1;
?????? (4).下一條
?????????? //用當(dāng)前被選中的索引去加 1
?????????? ListBox.SelectIndex=ListBox.SelectIndex + 1;
實(shí)例:
ListBox2.Items.Add(ListBox1.Items[ListBox1.SelectedIndex]); ?
? ListBox1.Items.Delete(ListBox1.SelectedIndex);
//多選 ?
? var ?
? i,j:integer; ?
? begin ?
? ? ? j:=Listbox1.items.count; ?
? ? ? i:=0; ?
? ? ? while ? i<j ? do ?
? ? ? begin ?
? ? ? ? ? if ? Listbox1.items[i].sellected ? then ?
? ? ? ? ? begin ?
? ? ? ? ? ? ? Listbox2.items.add(Listbox1.items[i].string); ?
? ? ? ? ? ? ? Listbox1.items.delete; ?
? ? ? ? ? ? ? Dec(j); ?
? ? ? ? ? ? ? dec(i); ?
? ? ? ? ? end; ?
? ? ? inc(i); ?
? ? ? end; ?
? end;
轉(zhuǎn)載于:https://www.cnblogs.com/Sue_/articles/1694914.html
總結(jié)
以上是生活随笔為你收集整理的《WebForm开发系列之控件篇》Item2 ListBox的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 可访问性不一致的原因与解决方法
- 下一篇: oracle中DECODE与CASE的用