⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vb0102.htm

📁 VB6.0全称为VisualBasic 6.0
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<HTML>
<head><title>洪恩在线 - 简单的列表框ListBox的例子</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<META HTTP-EQUIV="Pragma" CONTENT="no_cache">

<link rel="stylesheet" href="/pub/css/home.css">
<link rel="stylesheet" href="/pc/program/program.css">
<meta name="keywords" content="程序设计,编程入门,源码,源程序,源码讲解,TextBox控件,VB,教程,电脑,教育,电脑乐园,交互">
<script language="javascript">
<!--
function change(myform)
{
        var url="www.hongen.com/pc/program/index.htm";
        url=myform.select.options[myform.select.options.selectedIndex].value;
        top.location.href=url;
}
//-->
</script></head>
<body bgcolor="#FFFFFF">
<!--顶部开始-->
<script language=javascript src=/pub/js/head.js></script>
<!--顶部结束--> 
<table width="760" height="20" border=0 cellspacing="0" cellpadding="0" align="center">
  <tr valign="middle" align="left"> 
    <td height=20 class="p1" width="297" bgcolor="#D3E7FF" align=center><div align="left">   <a href="/pc/program/index.htm" class=under>程序设计栏目首页</a></div></td>
    <td align=left class="p1" height="20" width="58" valign="top"><img src="/pc/program/img/leftnew-0.gif" width="52" height="20"></td>
    <td align="right" class="p1" height="20" width="405"><font color="#330099">当前位置:<a href="/default.htm" class="under">洪恩在线</a> 
      -&gt; <a href="/pc/index.htm" class="under">电脑乐园</a> -&gt; 简单的列表框ListBox的例子</font><img name="image2" src="/pc/img/blank.gif" width="8" height="8"> 
    </td>
  </tr>
</table>
<table width=760 border=0 align="center" cellspacing="0" cellpadding="0">
  <tr>
    <td height=15><img src="/pc/img/blank.gif" width="1" height="1" border=0></td>
  </tr>
</table>
<table width="760" border="0" cellspacing="0" cellpadding="0" align="center" height="909">
  <tr> 
    <th width="150" bgcolor="6082CF" align="center" height="910" valign="top">      <!-- 左边条开始 --> 
      <script language="Javascript" src="/pc/program/js/proleft.js"></script>
      <!-- 左边条结束 -->
      <br>
    </th>
    <td bgcolor="#000000" width="1" height="910"><img src="/pc/img/blank.gif" width="1" height="1"></td>
    <td valign="top" align="center"> <br>
      <table width="95%" border="0" cellspacing="0" cellpadding="0">
        <tr> 
          <td>&nbsp;</td>
          <td width="13" valign="middle"><img src="/pc/program/img/dot.gif" width="13" height="13"></td>
          <td class="p2" width="150" align="center" valign="middle"><font color="B85959"><b>编 程 入 门 </b></font></td>
          <td width="179"><img src="/pc/program/img/logo2.gif" width="179" height="52"></td>
        </tr>
      </table>
      <table width="95%" border="0" cellspacing="0" cellpadding="0" class="p2">
        <tr> 
          <td height="15" valign="bottom" class="p2">--看实例学VB6.0--<b>简单的列表框ListBox的例子--</b></td>
          <td height="15" valign="bottom" class="p2"> 
            <div align="right" class="p1"><a href="/pc/program/tutors/vb/vb0103.htm">下一节</a></div>
          </td>
        </tr>
        <tr> 
          <td bgcolor="FF9700" height="1" ><img src="img/blank.gif" width="1" height="2"></td>
          <td bgcolor="FF9700" height="1" ><img src="img/blank.gif" width="1" height="2"></td>
        </tr>
</table>
      <table width="95%" border="0" cellspacing="0" cellpadding="0" class="p2" background="/pc/img/line.gif">
        <tr>
          <td class="p2" height="120" colspan="4"> 
            <p align="left"><span class="p2" style="line-height:17pt"><br>
                列表框(ListBox)控件表示一个选项清单,用户可能用鼠标选择其中一个或者几个选项。</span></p>
            <p align="left"><span class="p2" style="line-height:17pt">  列表框的特点是:列表框中的项目是通过程序插入到其中的,用户无法向清单中输入数据,当选择其中的项目,并在用户单击一个按钮或者执行某个操作时,由应用程序完成对指定项目的具体操作。下面这个例子充分说明了列表框的这个特点,并介绍了列表框(ListBox)的常用属性。</span></p>
            <p align="left"><span class="p2" style="line-height:17pt">  为了学习方便,提供的源码已经作了详细的中文注释,看看源码框中的代码:</span></p>
            <div align="center"> 
              <p> 
                <textarea name="textfield" cols="80" rows="7" onMouseOver="this.focus()" onFocus="this.select()" readonly>Option Explicit
'--------------------------------------
'           简单的列表框ListBox的例子
'--------------------------------------
'           洪恩在线 求知无限
'--------------------------------------
'------名称-------------作用------------
'       List1、List2    列表框
'       LefttoRightCmd  移动所选项目到另一个列表框(左到右)
'       RighttoLeftCmd  移动所选项目到另一个列表框(右到左)
'       List1MoveCmd    删除列表框List1中所选项目
'       List2MoveCmd    删除列表框List2中所选项目
'       List1AddCmd     给列表框List1增加新项目
'       List2AddCmd     给列表框List2增加新项目
'       List1ClearCmd   清空列表框List1
'       List2ClearCmd   清空列表框List2
'--------------------------------------
'--------------------------------------
'————名称————属性设置———
'
'       List1列表框     Sorted属性设为“False”
'       List2列表框     Sorted属性设为“True”
'                       MultiSelect属性设置为2
'       即List2控件中的项目总是排序的
'--------------------------------------



Private Sub Form_Load()
    
    '装载窗体时,给列表中的元素赋值
    List1.AddItem "China"
    List1.AddItem "America"
    List1.AddItem "Japan"
    List2.AddItem "Soccer"
    List2.AddItem "Tennis"
    List2.AddItem "Basketball"
    
End Sub

Private Sub LefttoRightCmd_Click()
    '将左边列表中所选元素移动到右边列表中
    If List1.ListIndex >= 0 Then
        
        '在右边列表中增加新元素
        List2.AddItem List1.Text
        
        '删除左边列表中的所选元素
        List1.RemoveItem List1.ListIndex
                 
    End If
    
End Sub

Private Sub List1AddCmd_Click()
    
    Dim ListItem As String
    '
    ListItem = InputBox("在列表中输入新项目")
    '向列表中加入新项目
    If Trim(ListItem) <> "" Then
    'Trim()作用是清除ListItem字符串内的空格
    
        List1.AddItem ListItem
    End If
    
    
End Sub

Private Sub List1ClearCmd_Click()
    
    '清空列表
    List1.Clear
    
End Sub

Private Sub List1MoveCmd_Click()
    
    Dim i As Integer
    '判断列表框是否只有一个项目被选中
    If List1.SelCount = 1 Then
    
        List1.RemoveItem List1.ListIndex
        
    ElseIf List1.ListCount > 1 Then
    
        '删除列表框中的所选中的多个项目
        For i = List1.ListCount - 1 To 0 Step -1
        'ListCount返回列表框中的项目总数
        'ListCount-1是列表框中最后一个项目的索引号
            
            '判断该项目是否被选中,Selected()返回布尔值
            If List1.Selected(i) Then
                '删除索引号为i的项目
                List1.RemoveItem i
                
            End If
        Next
    End If
    
    
End Sub


Private Sub List2AddCmd_Click()
    
    '定义变量,以接收输入的字符串
    Dim ListItem As String
    ListItem = InputBox("在列表中输入新项目")
    
    If Trim(ListItem) <> "" Then
        '将字符串增加到列表中
        List2.AddItem ListItem
            
    End If
    
    
End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -