【发布时间】:2012-11-12 19:38:49
【问题描述】:
由于我需要禁用(灰显)ListBox 中的某些项目,我正在使用可以在here 找到的自定义控件:
这是我当前的代码:
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
dtp.Columns.Add("key")
dtp.Columns.Add("value")
PopulateDataTable(dtp, "myTxt")
_dataView = New DataView(dtp)
'Custom ListBox
List1.ValueMember = "key"
List1.DisplayMember = "value"
List1.DataSource = _dataView
'Legacy ListBox
List2.ValueMember = "key"
List2.DisplayMember = "value"
List2.DataSource = _dataView
UpdateLanguageMenu()
End Sub
Private Function PopulateDataTable(dt As DataTable, resTxt As String)
Using sw As New StringReader(My.Resources.ResourceManager.GetObject(resTxt))
Do
Dim line As String = sw.ReadLine
If line Is Nothing OrElse line.Trim = String.Empty Then Exit Do
Dim strArr() As String
strArr = line.Split(",")
Dim row As DataRow = dt.NewRow()
row("key") = strArr(0)
row("value") = strArr(1)
dt.Rows.Add(row)
Loop
sw.Close()
End Using
End Function
List1是自定义ListBox,List2是VS2012E自带的ListBox。
我不需要List2,它只是用来测试的,
在运行时,在List2 中,我正确加载了所有值,而不是在List1 中,我在所有行中都获得了System.Data.DataRowView ..
奇怪的是,我正在加载的 txt 是这样的:
00A1,MyValue1
00A2,Myvalue2
00A3,MyValue3
我还有一个标签,当在 ListBox 上选择项目时,我有代码将 Label.Text 更改为 List.SelectedValue,这是逗号前的第一部分。
它会显示在标签中。只有自定义列表框内的项目不被显示。
手动填充List1,而不是使用数据表,正在工作。
由于我是初学者,我无法找到问题所在。
【问题讨论】:
标签: vb.net listbox custom-controls