VB2005中的Combobox [英] Combobox in VB2005

查看:75
本文介绍了VB2005中的Combobox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!


在VB6.0中一个组合框有项目,基本上是描述和

ItemData可以用来创建一个链接记录,即如果

记录集有一个数字键。


我希望在VB2005中这可以改为字母数字

itemdata,因此不再需要在侧面创建数组。存储

的关键值。但是我甚至找不到itemdata了。


如何在组合框中存储每个项目的密钥?


Tia ,

Martin

Hi all!

In VB6.0 A combobox had items, which basically were the description and
ItemData which could be used to create a link to a record, that is if the
recordset had a numeric key.

I was hoping that in VB2005 this would have been changed to an alphanumeric
itemdata, so that no longer need to create an array "on the side" to store
the key values. But I can''t even find the itemdata anymore.

How do you store a key for each item in a combobox?

Tia,
Martin

推荐答案

" Martin" < x@y.com> schrieb
"Martin" <x@y.com> schrieb
大家好!

在VB6.0中一个组合框有项目,基本上是描述
和ItemData,可以用来创建链接到一个记录,
如果记录集有一个数字键。

我希望在VB2005中这将被改为
字母数字项数据,因此不再需要在侧面创建一个阵列存储键值。但是我甚至找不到
itemdata了。

如何在组合框中存储每个项目的键?
Hi all!

In VB6.0 A combobox had items, which basically were the description
and ItemData which could be used to create a link to a record, that
is if the recordset had a numeric key.

I was hoping that in VB2005 this would have been changed to an
alphanumeric itemdata, so that no longer need to create an array "on
the side" to store the key values. But I can''t even find the
itemdata anymore.

How do you store a key for each item in a combobox?



你现在可以将整个对象存储在组合框中。你不仅限于

text + itemdata了。对象的ToString方法返回组合框中显示的文本

。如果你不能覆盖ToString或者它确实如此,那么
不会返回要在组合中显示的文本,请写一个包装类:


class comboitem

公共readoly项目为MyObjectType

public sub new(byval item as MyObjectType)

me.item = item

end sub


public覆盖函数ToString()as string

return item.whateverYouWant

end function

结束班

添加项目到组合:


cbo.items.add(新comboitem(yourObject))


Armin


You can store whole objects now in a combobox. You are not limited to
text+itemdata anymore. The object''s ToString method returns the text to be
displayed in the combobox. If you can not override ToString or if it does
not return the text to be displayed in the combo, write a wrapper class:

class comboitem
public readoly item as MyObjectType
public sub new(byval item as MyObjectType)
me.item = item
end sub

public overrides function ToString() as string
return item.whateverYouWant
end function
end class
Add item to the combo:

cbo.items.add(new comboitem(yourObject))

Armin


Martin,

最简单的方法是为那个
\\\

dim dt as new datatable

dt.columns.add(" Names")

dt.Columns.add(" Keys")

dt.loaddatarow(new object(){" Martin"," 1"},true)

dt.loaddatarow(new object(){" Cor"," 2&qu ot;},true)

''这可能是其他所有方式,这就是我的方式。


Combobox1.datasource = dt

Combobox1.displaymember =" Names"

Combobox1.Valuemember =" Keys"

///


我希望这会有所帮助,


Cor
Martin,

The most simple way is to create a datatable for that
\\\
dim dt as new datatable
dt.columns.add("Names")
dt.Columns.add("Keys")
dt.loaddatarow(new object() {"Martin", "1"},true)
dt.loaddatarow(new object() {"Cor","2"},true)
''This can all in probably hundred other ways, this is the way I do it.

Combobox1.datasource = dt
Combobox1.displaymember = "Names"
Combobox1.Valuemember = "Keys"
///

I hope this helps,

Cor


" Martin" < x@y.com> schrieb:
"Martin" <x@y.com> schrieb:
如何为组合框中的每个项目存储一个键?
How do you store a key for each item in a combobox?




\\\

Me.ComboBox1.Items.Add(New Person(Pink Panther,22)


''测试。

MsgBox(DirectCast( Me.ComboBox1 * .Items(0),Person).ToString())

..

..

..

公共类人员

私有m_Name为字符串

私有m_Age为整数


公共子新(ByVal名称)作为字符串,ByVal年龄为整数)

Me.Name =姓名

Me.Age =年龄

结束子


公共属性名称()为字符串

获取

返回m_Name

结束获取

设置(ByVal值为字符串)

m_Name =价值

结束集

结束物业


公共财产年龄()为整数

获取

返回m_Age

结束获取

设置(ByVal值为整数)

m_Age = Val ub

结束集

结束属性


公共覆盖函数ToString()As String

返回我姓名& " (& Me.Age.ToString()&")"

结束功能

结束班级

///


或者您可以使用控件''''DataSource'',''DisplayMember''和

''ValueMember''属性。


-

MS Herfried K. Wagner

MVP< URL:http://dotnet.mvps.org/> ;

VB< URL:http://classicvb.org/petition/>



\\\
Me.ComboBox1.Items.Add(New Person("Pink Panther", 22)

'' Test.
MsgBox(DirectCast(Me.ComboBox1*.Items(0), Person).ToString())
..
..
..
Public Class Person
Private m_Name As String
Private m_Age As Integer

Public Sub New(ByVal Name As String, ByVal Age As Integer)
Me.Name = Name
Me.Age = Age
End Sub

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 could use the control''s ''DataSource'', ''DisplayMember'', and
''ValueMember'' properties.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>


这篇关于VB2005中的Combobox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆