《WebForm开发系列之控件篇》Item2 ListBox
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. 清空所有項:
????? //首先判斷列表框中的項是否大于0
????? If(ListBox.Items.Count > 0 )
????? {
//清空所有項
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.Clear();
?????? 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;
?????? //把鼠標指針放到移動后的那項上
?????? 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;
實例:
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;
轉載于:https://www.cnblogs.com/Sue_/articles/1694914.html
總結
以上是生活随笔為你收集整理的《WebForm开发系列之控件篇》Item2 ListBox的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 可访问性不一致的原因与解决方法
- 下一篇: oracle中DECODE与CASE的用