如何获取listbox中所选择项的值

sundyhuang 2005-12-05 09:37:04
如下为listbox中的内容
AAA
BBB
CCC
DDD
EEE
FFF
如果运行时选择了BBB,CCC两项,(当然用户在选择时也有可能选中了EEE,FFF等)
选中项的值放入一个数组myarray()中
请各位指教一下如何获取
...全文
1490 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
hamadou 2005-12-05
  • 打赏
  • 举报
回复
有了绑定就不能这么直接用了,要修改如下:

Dim i As Integer
Dim myarray(lst.SelectedItems.Count - 1) As String
For i = 0 To lst.SelectedItems.Count - 1
myarray(i) = CType(lst.SelectedItems.Item(i), datarowview).row.item("name")
Next
注意:这里的name,是假设的你的listbox的displaymember属性的值
sundyhuang 2005-12-05
  • 打赏
  • 举报
回复
因为listbox 中的内容是从一个表中获取的
mylisbox.datasource=dataset11.table(0)
mylisbox.displaymember="productName"
sundyhuang 2005-12-05
  • 打赏
  • 举报
回复
哪如何改啊
tiaoci 2005-12-05
  • 打赏
  • 举报
回复
你怎么会把 DataRowView 对象当作一个Item添加到 ListBox中的?

如果是这样你要

DataRowView view = list.SelectedItems[i] as DataRowView;

然后取得这个view对象中的字符串(看你自己的需要了)
tiaoci 2005-12-05
  • 打赏
  • 举报
回复
你 ListBox的Item 中添加的对象类型不对
sundyhuang 2005-12-05
  • 打赏
  • 举报
回复
我信你试过,是可以的,但是我不知我到底哪错了
sundyhuang 2005-12-05
  • 打赏
  • 举报
回复
Private Sub mylistbox_selectedchange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mylisbox.SelectedValueChanged

Dim myarray(mylisbox.SelectedItems.Count - 1) As String
Dim i As Integer
For i = 0 To mylisbox.Items.Count - 1
myarray(i) = CType(mylisbox.SelectedItems.Item(i), String)
Next


其他信息: 从类型“DataRowView”到类型“String”的强制转换无效。



End Sub
sundyhuang 2005-12-05
  • 打赏
  • 举报
回复
Dim myarray(mylisbox.SelectedItems.Count - 1) As String
Dim i As Integer
For i = 0 To mylisbox.Items.Count - 1
myarray(i) = CType(mylisbox.SelectedItems.Item(i), String)
Next


其他信息: 从类型“DataRowView”到类型“String”的强制转换无效。
fsdillon 2005-12-05
  • 打赏
  • 举报
回复
Dim i As Integer
Dim myarray(lst.SelectedItems.Count - 1) As String
For i = 0 To lst.SelectedItems.Count - 1
myarray(i) = CType(lst.SelectedItems.Item(i), String)
Next
试验过了, 可以准确的传递选中项的值。
sundyhuang 2005-12-05
  • 打赏
  • 举报
回复
谢谢各位,我都试过了,
未处理的“System.MissingMemberException”类型的异常出现在 microsoft.visualbasic.dll 中。

其他信息: 未找到类型“Char”的公共成员“text”。

或者
其他信息: 未找到类型“DataRowView”的公共成员“text”。
tiaoci 2005-12-05
  • 打赏
  • 举报
回复
ListBox list = xxx;
string [] result = new string[list.SelectedItems.Count];
for(int i = 0; i < result.Length; i++)
{
result[i] = list.SelectedItems[i] as string;
}
return result;
tiaoci 2005-12-05
  • 打赏
  • 举报
回复
ListBox也是一样的
gp01fb625 2005-12-05
  • 打赏
  • 举报
回复
listbox1.selectedvalue
sundyhuang 2005-12-05
  • 打赏
  • 举报
回复
我要的是listbox的
tiaoci 2005-12-05
  • 打赏
  • 举报
回复
string [] result = new string[ListView.SelectedItems.Count];
for(int i = 0; i < result.Length; i++)
{
result[i] = ListView.SelectedItems[i].Text;
}
return result;
tiaoci 2005-12-05
  • 打赏
  • 举报
回复
ListView.SelectItems

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧