我如何能... [英] How do I...
问题描述
我有一个简单的列表框,我想显示一个名称列表,我也希望
在列表框中存储一个id,只是一个字符串值,这样当用户
点击名称我可以使用id来获取有关该名称的信息。我知道如何在VB6中使用它来实现它(使用Tag属性)。在VB.Net中有更好的
方式吗?
-
TFWBWY ... A
I have a simple list box that I want to show a list of names and also I want
to store in the list box an id, just a string value, so that when the user
clicks on a name I can use the id to get information about that name. I
know how I would do it in VB6 (using the Tag property). Is there a better
way to do it in VB.Net?
--
TFWBWY...A
推荐答案
" Bryan Dickerson" < TX ****** @ netscape.net> schrieb:
"Bryan Dickerson" <tx******@netscape.net> schrieb:
我有一个简单的列表框,我想显示一个名单列表,而且我想在列表框中存储一个id,只是一个字符串值,这样当
用户点击名称我可以使用id获取有关该名称的信息。
I have a simple list box that I want to show a list of names and also I
want to store in the list box an id, just a string value, so that when the
user clicks on a name I can use the id to get information about that name.
\\\
Dim p As New Person()
p.Name =" Pink Panther
p.Age = 22
Me.ComboBox1 .Items.Add(p)
''测试。
MsgBox(DirectCast(Me.ComboBox1.Items(0),Person).ToString() )
..
..
..
公共级别人员
私有m_Name为字符串
私有m_Age为整数
公共属性名称()为字符串
获取
返回m_Name
结束获取
设置(ByVal值为字符串)
m_Name =值
结束集
结束物业
公共物业年龄()为整数
获取
返回m_Age
结束获取
设置(ByVal值为整数)
m_Age =值
结束集
结束属性
公共覆盖函数ToString()As String
返回Me.Name& " (& Me.Age.ToString()&")"
结束功能
结束班级
///
或者,您可以使用绑定的组合框:
\\\
和我在一起。 ListBox1
''这也可以是''ArrayList''或数组。
.DataSource = Database.FindPeople(...)
.DisplayMember =" Name"
.ValueMember =" Age"
结束
// /
-
MS Herfried K. Wagner
MVP< URL:http://dotnet.mvps.org />
VB< URL:http://classicvb.org/petition/>
\\\
Dim p As New Person()
p.Name = "Pink Panther"
p.Age = 22
Me.ComboBox1.Items.Add(p)
'' Test.
MsgBox(DirectCast(Me.ComboBox1.Items(0), Person).ToString())
..
..
..
Public Class Person
Private m_Name As String
Private m_Age As Integer
Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal Value As String)
m_Name = Value
End Set
End Property
Public Property Age() As Integer
Get
Return m_Age
End Get
Set(ByVal Value As Integer)
m_Age = Value
End Set
End Property
Public Overrides Function ToString() As String
Return Me.Name & " (" & Me.Age.ToString() & ")"
End Function
End Class
///
Alternatively, you can use a bound combobox:
\\\
With Me.ListBox1
'' This can be an ''ArrayList'' or array too, for example.
.DataSource = Database.FindPeople(...)
.DisplayMember = "Name"
.ValueMember = "Age"
End With
///
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
当我做的时候Items.Add(对象),Listbox / Combobox如何知道要显示什么
属性?
" Herfried K. Wagner [MVP]" ; <喜*************** @ gmx.at>在消息中写道
news:uW ************** @ TK2MSFTNGP09.phx.gbl ...
When I do the Items.Add(object), how does the Listbox/Combobox know what
property to display?
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:uW**************@TK2MSFTNGP09.phx.gbl...
" Bryan Dickerson" < TX ****** @ netscape.net> schrieb:
"Bryan Dickerson" <tx******@netscape.net> schrieb:
我有一个简单的列表框,我想显示一个名单列表,而且我想在列表框中存储一个id,只是一个字符串值,这样当
用户单击一个名称我可以使用id来获取有关该名称的信息。
I have a simple list box that I want to show a list of names and also I
want to store in the list box an id, just a string value, so that when the
user clicks on a name I can use the id to get information about that name.
\\\
Dim p As New Person( )
p.Name =" Pink Panther"
p.Age = 22
Me.ComboBox1.Items.Add(p)
''测试。
MsgBox(DirectCast(Me.ComboBox1.Items(0),Person).ToString())
。
。
。
公共类人员
私人m_Name As String
私有m_Age为整数
公共属性名称()为字符串
获取
返回m_Name
结束获取
设置(ByVal价值为字符串)
m_Name =价值
结束集
结束财产
公共财产年龄()作为整数
获取
返回m_Age <结束获取
设置(ByVal值为整数)
m_Age =值
结束集
结束属性
公共覆盖函数ToString()As String
返回Me.Name &安培; " (& Me.Age.ToString()&")"
结束功能
结束课程
///
或者,你可以使用绑定的组合框:
\\\
使用Me.ListBox1
''这也可以是''ArrayList''或数组,例如。
。DataSource = Database.FindPeople(...)
.DisplayMember =" Name"
.ValueMember =" Age"
结束
///
- MS Herfried K. Wagner
MVP< URL:http://dotnet.mvps.org/>
VB< URL:http://classicvb.org/petition/>
\\\
Dim p As New Person()
p.Name = "Pink Panther"
p.Age = 22
Me.ComboBox1.Items.Add(p)
'' Test.
MsgBox(DirectCast(Me.ComboBox1.Items(0), Person).ToString())
.
.
.
Public Class Person
Private m_Name As String
Private m_Age As Integer
Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal Value As String)
m_Name = Value
End Set
End Property
Public Property Age() As Integer
Get
Return m_Age
End Get
Set(ByVal Value As Integer)
m_Age = Value
End Set
End Property
Public Overrides Function ToString() As String
Return Me.Name & " (" & Me.Age.ToString() & ")"
End Function
End Class
///
Alternatively, you can use a bound combobox:
\\\
With Me.ListBox1
'' This can be an ''ArrayList'' or array too, for example.
.DataSource = Database.FindPeople(...)
.DisplayMember = "Name"
.ValueMember = "Age"
End With
///
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
" Bryan Dickerson" < TX ****** @ netscape.net> schrieb:
"Bryan Dickerson" <tx******@netscape.net> schrieb:
当我执行Items.Add(object)
如果要将对象添加到列表框中,请使用它。
Listbox / Combobox如何知道要显示的属性?
When I do the Items.Add(object)
Use it if you want to add an object to the listbox.
how does the Listbox/Combobox know what property to display?
列表框将显示对象'''ToString''返回的值br $>
方法。
-
MS Herfried K. Wagner
MVP< URL:http ://dotnet.mvps.org/>
VB< URL:http://classicvb.org/petition/>
The listbox will display the value returned by the object''s ''ToString''
method.
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
这篇关于我如何能...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!