📄 m28.htm
字号:
<html>
<head>
<title>VB教程</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<p align="center"><script src="../../1.js"></script></a>
<body bgcolor="#ffffff" leftmargin="5" topmargin="1" marginheight="5" marginwidth="5">
<div align=center>
<table border=0 cellpadding=0 cellspacing=0 width=680 align="center">
<tbody>
<tr>
<td bgcolor=#000000 height=14>
<div align=center class=H1> <font color="#FFFFFF" class="unnamed1">用VB在两个ListBox间互相移动项目元素</font> </font> </font></div>
</td>
</tr>
<tr valign=top>
<td class=H1 height=236>
<p align="center"> <font color="#000000">来源:www.ccidnet.com<br>
<br>
我们在VB编程过程中,经常遇到这种情况,需要从左边的ListBox中的项目移动到右边的ListBox中去,表示选择的内容;相反从右边的ListBox中的项目移动到左边的ListBox中去,表示把选择的内容放弃。一般如下图所示,有List1和List2两个ListBox,在List1中预先加载需要选择的项目。有四个按钮,"ADD"、"ADD
ALL"、"DELETE"、"DELETE ALL",单击"ADD"可以把左边的ListBox中的选择项目移动到右边的ListBox中去,单击"ADD
ALL"可以把左边的ListBox中的所有项目移动到右边的ListBox中去,单击"DELETE"可以把右边的ListBox中的选择项目移动到左边的ListBox中去,单击"DELETE
ALL"可以把右边的ListBox中的所有项目移动到左边的ListBox中去。 <br>
</font><br>
</span></span> </font></p>
<center>
<img
src="../../pic/010516vb1.jpg" width="361" height="251"> </span> </span>
</center>
<br>
这儿是实现上述功能的所有代码,同时根据判断ListBox中是否有选择项目来智能的确定按钮Enabled属性。 <br>
<br>
</span> </span>
<table border=0 cellpadding=0 cellspacing=0 width=580 align="center">
<tbody>
<tr>
<td bgcolor=#e6e6e6 class=code><strong>Private Sub CmdADD_Click()<br>
If List1.ListCount = 0 Then<br>
Screen.MousePointer = 0<br>
Exit Sub<br>
End If<br>
If List1.ListIndex = -1 Then '如果没有选择<br>
List1.SetFocus<br>
List1.Selected(0) = True '选择第一项为默认项<br>
End If<br>
DoEvents<br>
List2.AddItem List1.Text<br>
List1.RemoveItem List1.ListIndex<br>
If List1.ListCount > 0 Then<br>
CmdADDALL.Enabled = True<br>
CmdDELETE.Enabled = True<br>
Else<br>
CmdADD.Enabled = False<br>
CmdADDALL.Enabled = False<br>
CmdDELETE.Enabled = True<br>
CmdDELETEALL.Enabled = True<br>
End If<br>
End Sub<br>
</strong></td>
</tr>
</tbody>
</table>
<br>
</span> </span>
<table border=0 cellpadding=0 cellspacing=0 width=580 align="center">
<tbody>
<tr>
<td bgcolor=#e6e6e6 class=code><strong>Private Sub CmdADDALL_Click()<br>
If List1.ListCount = 0 Then<br>
Screen.MousePointer = 0<br>
Exit Sub<br>
End If<br>
If List1.ListIndex = -1 Then<br>
List1.SetFocus<br>
List1.Selected(0) = True<br>
End If<br>
DoEvents<br>
For i = 0 To (List1.ListCount - 1)<br>
List2.AddItem List1.List(i)<br>
DoEvents<br>
Next i<br>
List1.Clear<br>
CmdADD.Enabled = False<br>
CmdADDALL.Enabled = False<br>
CmdDELETE.Enabled = True<br>
CmdDELETEALL.Enabled = True <br>
End Sub<br>
</strong></td>
</tr>
</tbody>
</table>
<br>
</span> </span>
<table border=0 cellpadding=0 cellspacing=0 width=580 align="center">
<tbody>
<tr>
<td bgcolor=#e6e6e6 class=code><strong>Private Sub CmdDELETE_Click()<br>
If List2.ListCount = 0 Then<br>
Screen.MousePointer = 0<br>
Exit Sub<br>
End If<br>
If List2.ListCount = 0 Then Exit Sub<br>
If List2.ListIndex = -1 Then<br>
List2.SetFocus<br>
List2.Selected(0) = True<br>
End If<br>
List1.AddItem List2.Text<br>
List2.RemoveItem List2.ListIndex<br>
If List2.ListCount > 0 Then<br>
CmdADD.Enabled = True<br>
CmdADDALL.Enabled = True<br>
Else<br>
CmdADD.Enabled = True<br>
CmdADDALL.Enabled = True<br>
CmdDELETE.Enabled = False<br>
CmdDELETEALL.Enabled = False<br>
End If<br>
End Sub<br>
</strong></td>
</tr>
</tbody>
</table>
<br>
</span> </span>
<table border=0 cellpadding=0 cellspacing=0 width=580 align="center">
<tbody>
<tr>
<td bgcolor=#e6e6e6 class=code><strong>Private Sub CmdDELETEALL_Click()<br>
If List2.ListCount = 0 Then<br>
Screen.MousePointer = 0<br>
Exit Sub<br>
End If<br>
If List2.ListCount = 0 Then Exit Sub<br>
If List2.ListIndex = -1 Then '如果没有选择<br>
List2.SetFocus <br>
List2.Selected(0) = True '把第一项作为默认项<br>
End If<br>
For i = 0 To (List2.ListCount - 1)<br>
List1.AddItem List2.List(i)<br>
DoEvents<br>
Next i<br>
List2.Clear<br>
CmdADD.Enabled = True<br>
CmdADDALL.Enabled = True<br>
CmdDELETE.Enabled = False<br>
CmdDELETEALL.Enabled = False <br>
End Sub<br>
</strong></td>
</tr>
</tbody>
</table>
<p align="center"> <br>
</p>
</td>
</tr>
</table>
</div>
<p align="center"><a href="../../pian/vb.htm">回首页</a>
<p align="center"><script src="../../2.js"></script></a>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -