我可以制作一个强力键入的组合框吗? [英] Can I make a combobox that is stronly typed?

查看:61
本文介绍了我可以制作一个强力键入的组合框吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ComboBox来显示我已定义的类的对象,比如CQQ。


除了某种方式我偶尔将一个Item设置为一个String对象时工作得很好

而不是CQQ类型的对象。


看起来Text替换了一个项目或类似的东西。


这导致运行时错误,此时我得知

应该是CQQ的项目是一个字符串。


我找不到设置发生的地点或方式。


如果comboBox将它的项目定义为

CQQ而不是Object的对象,则会发生这种情况。它的使用会更容易出错,而且更多可能没有错误。


有什么方法可以生成一个强类型的组合框吗? br />

我知道我可以简单地继承一个组合框并尝试覆盖任何

更改一个项目,但这样就有可能我会错过一些方法

和没有真正拥有我想要的东西。我想一次更改物品

集合。


这可能吗?

谢谢

I''m using a ComboBox to display objects of a class I''ve defined, say CQQ.

Works great except somehow I occasionally set an Item to a String object
instead of an object of type CQQ.

It looks like Text replaces an item or something like that.

This results in a runtime error at which time I learn that the item that
should be CQQ is a String.

I can''t find out where or how the setting happens.

It occurs to me that if the comboBox defined it''s Items as objects of type
CQQ instead of Object it would the usage would be less error prone and more
likely to be bug free.

Is there some way I could generate a strongly typed combobox?

I know I could simply inherit a combobox and try to override anything the
changes an Item but that leaves the possibility that I''d miss some method
and not really have what I want. I''d like to just change the Items
collection once.

Is that possible?
Thanks

推荐答案

为什么不创建CQQ列表然后将组合框绑定到它。然后将任何内容添加到组合框中,你必须将它添加到列表中,如果你试图添加除了一个以外的对象,它将会爆炸CQQ吧。关闭

我的头顶,如下:


Dim myList As List(of CQQ)


''将条目添加到列表中

myList.Add(新CQQ(" field1"," field2")))

myList.Add(新CQQ(") ; field1"," field2"))

myList.Add(新CQQ(" field1"," field2"))


myComboBox。 DataSource = myList

myComboBox.DisplayMember = myList.field2

myComboBox.ValueMember = myList.field1


Robin S.

------------------------------------------

活性" < ac ********** @ a-znet.comwrote in message

news:uW ************* @ TK2MSFTNGP02.phx。 gbl ...
Why not create a list of CQQ and then bind the combobox to it. Then to add
anything to the combobox, you''ll have to add it to the list, which will
blow up if you try to add an object other than a CQQ to it. Off the top of
my head, something like this:

Dim myList As List(Of CQQ)

''add entries to the list
myList.Add(New CQQ("field1", "field2"))
myList.Add(New CQQ("field1", "field2"))
myList.Add(New CQQ("field1", "field2"))

myComboBox.DataSource = myList
myComboBox.DisplayMember = myList.field2
myComboBox.ValueMember = myList.field1

Robin S.
------------------------------------------
" active" <ac**********@a-znet.comwrote in message
news:uW*************@TK2MSFTNGP02.phx.gbl...

我正在使用ComboBox来显示我已定义的类的对象,比如CQQ。


工作得很好除了某种程度我偶尔将一个Item设置为一个String对象

而不是一个CQQ类型的对象。


看起来像Text替换一个项目或类似的东西。


这导致运行时错误,此时我得知

应该是CQQ的项目是一个字符串。


我无法找到设置发生的位置或方式。


如果comboBox定义了它,我会发现它是'项目作为

的对象类型CQQ而不是对象,它的使用会减少错误

,更有可能没有bug。


有什么方法可以生成一个强类型的组合框吗?


我知道我可以简单地继承一个组合框并尝试覆盖

更改项目的任何内容,但这样就有可能我会错过某些方法

而不是真正拥有我想要的东西。我想一次更改物品

集合。


这可能吗?


谢谢
I''m using a ComboBox to display objects of a class I''ve defined, say CQQ.

Works great except somehow I occasionally set an Item to a String object
instead of an object of type CQQ.

It looks like Text replaces an item or something like that.

This results in a runtime error at which time I learn that the item that
should be CQQ is a String.

I can''t find out where or how the setting happens.

It occurs to me that if the comboBox defined it''s Items as objects of
type CQQ instead of Object it would the usage would be less error prone
and more likely to be bug free.

Is there some way I could generate a strongly typed combobox?

I know I could simply inherit a combobox and try to override anything the
changes an Item but that leaves the possibility that I''d miss some method
and not really have what I want. I''d like to just change the Items
collection once.

Is that possible?
Thanks



因为我对使用数据源一无所知。


我会调查你在下面给出了什么,看看我是否可以学到足够的东西来使用

吧。


还有我需要研究的SetItemCore和SetItemsCore 。

非常感谢


" RobinS" < Ro **** @ NoSpam.yah.nonewrote in message

news:3d ************************ ******@comcast.com。 ..
Because I know nothing about using a datasource.

I''ll look into what you''ve given below and see if I can learn enough to use
it.

There is also SetItemCore and SetItemsCore that I have to look into.
Thanks a lot

"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:3d******************************@comcast.com. ..

为什么不创建CQQ列表然后将组合框绑定到它。然后将任何内容添加到组合框中,你必须将它添加到列表中,如果你试图添加除了一个以外的对象,它将会爆炸CQQ吧。关闭

我的头顶,如下:


Dim myList As List(of CQQ)


''将条目添加到列表中

myList.Add(新CQQ(" field1"," field2")))

myList.Add(新CQQ(") ; field1"," field2"))

myList.Add(新CQQ(" field1"," field2"))


myComboBox。 DataSource = myList

myComboBox.DisplayMember = myList.field2

myComboBox.ValueMember = myList.field1


Robin S.

------------------------------------------

活性" < ac ********** @ a-znet.comwrote in message

news:uW ************* @ TK2MSFTNGP02.phx。 gbl ...
Why not create a list of CQQ and then bind the combobox to it. Then to add
anything to the combobox, you''ll have to add it to the list, which will
blow up if you try to add an object other than a CQQ to it. Off the top of
my head, something like this:

Dim myList As List(Of CQQ)

''add entries to the list
myList.Add(New CQQ("field1", "field2"))
myList.Add(New CQQ("field1", "field2"))
myList.Add(New CQQ("field1", "field2"))

myComboBox.DataSource = myList
myComboBox.DisplayMember = myList.field2
myComboBox.ValueMember = myList.field1

Robin S.
------------------------------------------
" active" <ac**********@a-znet.comwrote in message
news:uW*************@TK2MSFTNGP02.phx.gbl...

>我正在使用ComboBox来显示我已定义的类的对象,比如CQQ。

工作得很好,除了某种程度上我偶尔将一个Item设置为一个String对象而不是一个CQQ类型的对象。

它看起来像Text替换了一个类似的东西。

这导致运行时错误,此时我知道
应该是CQQ的项目是一个字符串。

我找不到在哪里或如何设置发生了。

如果comboBox将它的项目定义为
类型CQQ而不是Object的对象,那么它的使用会减少错误。
并且更有可能没有bug。

有什么方法可以生成一个强类型的组合框吗?

我知道我可以简单地继承一个组合框并试图覆盖任何东西
更改项目但是tha我留下了一种可能性,我会错过一些方法
而不是真正拥有我想要的东西。我想一次更改Items
集合。

这可能吗?

谢谢
>I''m using a ComboBox to display objects of a class I''ve defined, say CQQ.

Works great except somehow I occasionally set an Item to a String object
instead of an object of type CQQ.

It looks like Text replaces an item or something like that.

This results in a runtime error at which time I learn that the item that
should be CQQ is a String.

I can''t find out where or how the setting happens.

It occurs to me that if the comboBox defined it''s Items as objects of
type CQQ instead of Object it would the usage would be less error prone
and more likely to be bug free.

Is there some way I could generate a strongly typed combobox?

I know I could simply inherit a combobox and try to override anything the
changes an Item but that leaves the possibility that I''d miss some method
and not really have what I want. I''d like to just change the Items
collection once.

Is that possible?
Thanks




我已经使用了你的建议几乎没有任何变化。


以下就是我所说的尝试了,它似乎是存储数据,但不是显示它的



我已经尝试了所有我能想到的DisplayMember! />

StringWithInteger有两个属性:Str和Value

公共myList为List(Of StringWithInteger)=新列表(Of StringWithInteger)
< br $>



myList.Add(itemValue)''itemValue类型为StringWithInteger





Public Sub New()


MyBase.New()


InitializeComponent()


Me.DataSource = myList


Me.DisplayMember = myList.field2


Me.ValueMember = myList.field1


En d Sub



" RobinS" < Ro **** @ NoSpam.yah.nonewrote in message

news:3d ************************ ******@comcast.com。 ..
I''ve used what you suggested almost with no changes.

Below is what I''ve tried and it appears to be storing data but not
displaying it.

I''ve tried everything I can think of for the DisplayMember!

StringWithInteger has two properties: Str and Value

Public myList As List(Of StringWithInteger) = New List(Of StringWithInteger)

and

myList.Add(itemValue) ''itemValue is type StringWithInteger

and

Public Sub New()

MyBase.New()

InitializeComponent()

Me.DataSource = myList

Me.DisplayMember = myList.field2

Me.ValueMember = myList.field1

End Sub



"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:3d******************************@comcast.com. ..

为什么不创建CQQ列表然后将组合框绑定到它。然后将任何内容添加到组合框中,你必须将它添加到列表中,如果你试图添加除了一个以外的对象,它将会爆炸CQQ吧。关闭

我的头顶,如下:


Dim myList As List(of CQQ)


''将条目添加到列表中

myList.Add(新CQQ(" field1"," field2")))

myList.Add(新CQQ(") ; field1"," field2"))

myList.Add(新CQQ(" field1"," field2"))


myComboBox。 DataSource = myList

myComboBox.DisplayMember = myList.field2

myComboBox.ValueMember = myList.field1


Robin S.

------------------------------------------

活性" < ac ********** @ a-znet.comwrote in message

news:uW ************* @ TK2MSFTNGP02.phx。 gbl ...
Why not create a list of CQQ and then bind the combobox to it. Then to add
anything to the combobox, you''ll have to add it to the list, which will
blow up if you try to add an object other than a CQQ to it. Off the top of
my head, something like this:

Dim myList As List(Of CQQ)

''add entries to the list
myList.Add(New CQQ("field1", "field2"))
myList.Add(New CQQ("field1", "field2"))
myList.Add(New CQQ("field1", "field2"))

myComboBox.DataSource = myList
myComboBox.DisplayMember = myList.field2
myComboBox.ValueMember = myList.field1

Robin S.
------------------------------------------
" active" <ac**********@a-znet.comwrote in message
news:uW*************@TK2MSFTNGP02.phx.gbl...

>我正在使用ComboBox来显示我已定义的类的对象,比如CQQ。

工作得很好,除了某种程度上我偶尔将一个Item设置为一个String对象而不是一个CQQ类型的对象。

它看起来像Text替换了一个类似的东西。

这导致运行时错误,此时我知道
应该是CQQ的项目是一个字符串。

我找不到在哪里或如何设置发生了。

如果comboBox将它的项目定义为
类型CQQ而不是Object的对象,那么它的使用会减少错误。
并且更有可能没有bug。

有什么方法可以生成一个强类型的组合框吗?

我知道我可以简单地继承一个组合框并试图覆盖任何东西
更改项目但是tha我留下了一种可能性,我会错过一些方法
而不是真正拥有我想要的东西。我想一次更改Items
集合。

这可能吗?

谢谢
>I''m using a ComboBox to display objects of a class I''ve defined, say CQQ.

Works great except somehow I occasionally set an Item to a String object
instead of an object of type CQQ.

It looks like Text replaces an item or something like that.

This results in a runtime error at which time I learn that the item that
should be CQQ is a String.

I can''t find out where or how the setting happens.

It occurs to me that if the comboBox defined it''s Items as objects of
type CQQ instead of Object it would the usage would be less error prone
and more likely to be bug free.

Is there some way I could generate a strongly typed combobox?

I know I could simply inherit a combobox and try to override anything the
changes an Item but that leaves the possibility that I''d miss some method
and not really have what I want. I''d like to just change the Items
collection once.

Is that possible?
Thanks




这篇关于我可以制作一个强力键入的组合框吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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