组合框如何知道键/值? [英] How does combobox know key/value?

查看:56
本文介绍了组合框如何知道键/值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用组合框的.Add()方法时,它如何知道我的对象中的哪个
元素是键,哪个是值?


说我的对象看起来像这样:


object1.field1 =" 2";

object1.field2 =" Number2";


和我这样做:

combobox1.Items.Add(object1);


我想要field2显示在组合框中,然后以某种方式将
引用其对应的值。我是否需要创建一个查询

表来实现这一点,因为field2将会被返回

组合?


DataSource()不会在这里使用。


谢谢,

Brett

When using the .Add() method of a combo box, how does it know which
element in my object is the key and which is the value?

Say my object looks like this:

object1.field1 = "2";
object1.field2 = "Number2";

and I do:
combobox1.Items.Add(object1);

I''d like field2 displayed in the combo box and then some way to
reference its corresponding value. Will I need to create a look up
table to acheive this because field2 is all that will ever be returned
by the combo?

DataSource() won''t be used here.

Thanks,
Brett

推荐答案

尝试combox1.DisplayMember =" field2" ;类似的领域存在价值。


/ LM


" Brett Romero" <交流***** @ cygen.com>在消息中写道

news:11 ********************** @ o13g2000cwo.googlegr oups.com ...
try combox1.DisplayMember = "field2" ; a similar field exist for value.

/LM

"Brett Romero" <ac*****@cygen.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
当使用组合框的.Add()方法时,它如何知道我的对象中的哪个元素是键,哪个是值?

说出我的对象看起来像这样:

object1.field1 =" 2" ;;
object1.field2 =" Number2";

我做:
combobox1.Items.Add(object1);

我想在组合框中显示field2然后某种方式来引用它的相应值。我是否需要创建一个查找表来实现这一点因为field2是所有将被返回的组合


DataSource()不会被使用在这里。

谢谢,
Brett
When using the .Add() method of a combo box, how does it know which
element in my object is the key and which is the value?

Say my object looks like this:

object1.field1 = "2";
object1.field2 = "Number2";

and I do:
combobox1.Items.Add(object1);

I''d like field2 displayed in the combo box and then some way to
reference its corresponding value. Will I need to create a look up
table to acheive this because field2 is all that will ever be returned
by the combo?

DataSource() won''t be used here.

Thanks,
Brett



我在问这个组合框是怎么知道的使用语法:


combobox1.Items.Add(object1);


或者它实际上是否有一个键/值?


Brett

I''m asking how does the combo box know when only this syntax is used:

combobox1.Items.Add(object1);

Or does it actually have a key/value at that point?

Brett


Combobox将对我们的对象使用ToString调用来确定
的值
显示。如果在对象中定义ToString方法以覆盖

默认的ToString方法,那么您可以确保Combobox将在您想要的字段中显示

。选择项目后,您可以使用选择

索引从组合框中检索实际对象。

HTH


-

--- Nick Malik [微软]

MCSD,CFPS,认证Scrummaster
http://blogs.msdn.com/nickmalik


免责声明:本论坛发表的意见均为我的意见拥有,而不是

代表我的雇主。

我不代表我的雇主回答问题。我只是一个帮助程序员的
程序员。

-

" Brett Romero" <交流***** @ cygen.com>在消息中写道

news:11 ********************** @ o13g2000cwo.googlegr oups.com ...
Combobox will use the ToString call on our object to determine what value to
display. If you define a ToString method in your object to override the
default ToString method, then you can make sure that Combobox will display
the field you''d like. When an item is selected, you can use the selection
index to retrieve the actual object from the combobox.
HTH

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I''m just a
programmer helping programmers.
--
"Brett Romero" <ac*****@cygen.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
当使用组合框的.Add()方法时,它如何知道我的对象中的哪个元素是键,哪个是值?

说出我的对象看起来像这样:

object1.field1 =" 2" ;;
object1.field2 =" Number2";

我做:
combobox1.Items.Add(object1);

我想在组合框中显示field2然后某种方式来引用它的相应值。我是否需要创建一个查找表来实现这一点因为field2是所有将被返回的组合


DataSource()不会被使用在这里。

谢谢,
Brett
When using the .Add() method of a combo box, how does it know which
element in my object is the key and which is the value?

Say my object looks like this:

object1.field1 = "2";
object1.field2 = "Number2";

and I do:
combobox1.Items.Add(object1);

I''d like field2 displayed in the combo box and then some way to
reference its corresponding value. Will I need to create a look up
table to acheive this because field2 is all that will ever be returned
by the combo?

DataSource() won''t be used here.

Thanks,
Brett



这篇关于组合框如何知道键/值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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