zoukankan      html  css  js  c++  java
  • listBox的用法

    1. 属性列表:
        SelectionMode  
    组件中条目的选择类型:None-根本不允许任何选择;One-默认值,只选择单个选项;MultiSimple-简单的多项选择,单击一次鼠标就选中或取消选中列表中的一项;MultiExtended-扩展的多项选择,类似windows中的选择操作.
    SelectedItem   
    在单选的列表框里, SelectedItem返回的是一个对象,它的文本由Text属性表示.作用是获得列表框中被选择的条目.如果控件允许多项选择,被选中的条目就以SelectedItems属性表示,它是Item对象的一个集合.
        Count           
    列表框中条目的总数
        SelectedIndex /SelectedIndices/SelectedItem/SelectedItems
    ListBox.SelectedIndex属性获取单项选择ListBox中当前选定项的位置;ListBox.SelectedIndices属性获取一个集合,该集合包含ListBox中所有当前选定项的从零开始的索引;ListBox.SelectedItem属性获取ListBox中当前选定的项;ListBox.SelectedItems属性获取多重选择ListBox中所有选定的项,它是一集合。
    Public ReadOnly Property SelectedIndices As ListBox.SelectedIndexCollection
    ListBox.SelectedIndexCollection,包含控件中当前选定项的索引。如果当前没有选定的项,则返回空 ListBox.SelectedIndexCollection
    泛指列表框中的所有项
    2. 取列表框中被选中的值
         ListBox.SelectedValue
    3. 动态的添加列表框中的项:
         ListBox.Items.Add("所要添加的项");
    以下代码实现通过输入框向列表框中添加内容:
        Private Sub bttnAdd1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnAdd1.Click
            Dim ListItem As String
            ListItem = InputBox("Enter new item's name")
            If ListItem.Trim <> "" Then
                sourceList.Items.Add(ListItem)
            End If
        End Sub
         ListBox.Items.Insert(index,item)
         item是要添加到列表的对象,index是这个新项的索引。
    4. 移出指定项:
         //首先判断列表框中的项是否大于0
         If(ListBox.Items.Count > 0 )
         {
         //移出选择的项
          ListBox.Items.Remove(ListBox.SelectedItem);
         }
    以下代码实现从单项选择的列表框中删除被选中的条目:
        Private Sub bttnRemoveSelDest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnRemoveSelDest.Click
            ListBox.Items.Remove(ListBox.SelectedItem)
        End Sub
    以下代码实现从多项选择列表框中删除多个条目:
        Private Sub bttnRemoveSelSrc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnRemoveSelSrc.Click
            Dim i As Integer
            For i = 0 To ListBox.SelectedIndices.Count - 1
                LisBoxt.Items.RemoveAt(ListBox.SelectedIndices(0))
            Next
        End Sub
        备注:利用ListBox.Items.Remove方法,以要删除的对象作为参数,从列表中删除条目。而利用RemoveAt方法可以删除指定位置(索引)的列表项,它以索引作为参数:ListBox.Items.RemoveAt(index)
    5. 清空所有项:
         //首先判断列表框中的项是否大于0
         If(ListBox.Items.Count > 0 )
         {
         //清空所有项
         ListBox.Items.Clear();
         }
    6. 列表框可以一次选择多项:
         只需设置列表框的属性 SelectionMode="Multiple",按Ctrl可以多选
    7. 多列表框中搜索字符串:
    FindString和 FindStringExact方法可以迅速地找到条目(search word)在列表里的位置(wordIndex)。它们都接收字符串作为弟一个参数,第二个参数可选,用于指定搜索开始的位置。其中FindString找到与指定字符部分匹配的条目,而FindStringExact找到时完全匹配的。
    wordIndex=ListBox.FindStringExact("search word")
    wordIndex=ListBox.FindString("search word")
    8. Contains方法
    利用它可以避免在列表中插入相同的对象。此方法接收一个对象作为参数,返回Ture/False来表示Items集合中是否包含这个对象。比如,要实现以下功能:先检查插入的字符串是否已经存在于列表,只有当列表中还没有包含这个字符串时才插入它。其代码如下(VB.Net):
    Dim itm As String="Remote Computing"
    If Not ListBox.Items.Contains(itm) then
               ListBox1.Item.Add(itm)
    End If
    9. 两个列表框联动,即两级联动菜单
         //判断第一个列表框中被选中的值
         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");
         }
    10. 实现列表框中项的移位
         即:向上移位、向下移位
         具体的思路为:创建一个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;
    11. 移动指针到指定位置:
          (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;

  • 相关阅读:
    Python 7步机器学习
    STL容器-- map and multimap 用法
    STL容器-- forward_list 用法
    STL容器-- fixed-size array 用法
    STL容器-- deque 用法
    网站502与504错误分析
    git将一个分支的某个文件合并到当前分支
    阿里云RDS上用mysqldump导入导出
    mysqldump具体应用实例
    mysql的导入导出工具mysqldump命令详解
  • 原文地址:https://www.cnblogs.com/q101301/p/3701770.html
Copyright © 2011-2022 走看看