编程基础 软件工程 JAVA .NET Delphi Visual C/C++ C++BuilderVisual Basic C/C++ PowerBuilder 汇编语言 数据结构&算法 破解与加脱壳 移动开发 编程开发技巧与经验
返回首页

在vb.net中使用ComboBox.Items的Add方法添加列表项

时间:2017-05-08来源: 作者: 点击:
.
在使用Visual Basic.net中经常需要给Combox添加项目,有这样一个类,可以专门用来向列表控件添加列表项,这个类就是 DictionaryEntry,定义可设置或检索的字典键/值对。

例如:

        Do While Reader.Read

                    ComboBox1.Items.Add(New DictionaryEntry(Reader.Item("显示字段"), Reader.Item("值字段")))

        Loop

        ComboBox1.DisplayMember = "Key"

        ComboBox1.ValueMember = "Value"


在使用DictionaryEntry对象向列表控件添加数据后, 如果要得到被选定项的“显示的文本”或“项的值”,那么,请使用如下的写法,

Dim strValue As String = CType(ComboBox1.SelectedItem, DictionaryEntry).Value

Dim strKey As String = CType(ComboBox1.SelectedItem, DictionaryEntry).Key


DictionaryEntry 的其它使用方法,请参考以下代码:
'A simple example for the DictionaryEntry structure.
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Module Example

    Sub Main()

        ' Create a new hash table.
        '
        Dim openWith As New Hashtable()

        ' Add some elements to the hash table. There are no
        ' duplicate keys, but some of the values are duplicates.
        openWith.Add("txt", "notepad.exe")
        openWith.Add("bmp", "paint.exe")
        openWith.Add("dib", "paint.exe")
        openWith.Add("rtf", "wordpad.exe")

        ' When you use For Each to enumerate hash table elements,
        ' the elements are retrieved as DictionaryEntry objects.
        Console.WriteLine()
        For Each de As DictionaryEntry In openWith
            Console.WriteLine("Key = {0}, Value = {1}", _
                de.Key, de.Value)
        Next de

    End Sub

End Module

' This code example produces output similar to the following:
'
'Key = rtf, Value = wordpad.exe
'Key = txt, Value = notepad.exe
'Key = dib, Value = paint.exe
'Key = bmp, Value = paint.exe
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
验证码:点击我更换图片
推荐内容