设置ComboBox的选定索引 [英] set selected index of a ComboBox

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

问题描述

嗨组,

我有一个问题,我已经发布到德国C#新闻组。

因为没有人可以帮助我,我想再试一次:


我设置了这样一个组合框的选定项目:


myCombo.SelectedItem = 3;


但是之后ComboBox只显示列表的第一个条目。

当我添加行


myCombo.DroppedDown = true;


之前,然后通过代码,我可以看到组合列表

下降,然后是索引(在这种情况下) 3)被选中。之后,

方法完成,没有其他代码被处理。然后,当表格

出现时,会有第一个项目被选中!


好​​笑,几周前相同的代码可以运行 - 在这个地方什么没有

改变了。有人知道会发生什么吗?或者可能是,那个是否需要一个MS补丁或者类似的问题?


问候,

Steffen 。

解决方案

嗨Steffen,

您应该使用SelectedIndex属性!


示例

[Visual Basic,C#,C ++]以下示例说明了FindString方法和SelectedIndex属性的用法。该示例是ComboBox类概述中可运行代码示例的一部分。


[Visual Basic]

Private Sub findButton_Click(ByVal sender As Object,ByVal e作为System.EventArgs)

Dim index As Integer

index = comboBox1.FindString(textBox2.Text)

comboBox1.SelectedIndex = index

End Sub

[C#]

private void findButton_Click(object sender,System.EventArgs e){

int index = comboBox1.FindString(textBox2.Text);

comboBox1.SelectedIndex = index;

}" Steffen Laser" < SG ***** @ intermet.de>在消息新闻中写道:OD ************** @ TK2MSFTNGP09.phx.gbl ...

嗨组,
我有一个问题,我已经发布到德国C#新闻组。
由于没有人可以帮助我,我想在这里再试一次:

我设置了这样一个组合框的选定项目:

myCombo.SelectedItem = 3;

但之后,ComboBox只显示列表的第一个条目。
当我添加行

myCombo.DroppedDown = true;

之前,然后逐步通过代码,我可以看到组合列表下拉,然后索引(在本例中为3)被选中。之后,
方法完成,没有其他代码被处理。然后,当表单出现时,会有第一个项目被选中!

有趣的是,几周前相同的代码工作 - 在这个地方什么都没有改变。有人知道会发生什么吗?或者可能是,那个MS补丁或类似问题有问题吗?

问候,
Steffen。


嗨Juan,

非常感谢,但我尝试了以下所有方法:


SelectedIdex = 3;


SelectedItem = object; //因为数据源包含一个带有重写的ToString()方法的
对象的ArrayList。我还尝试了一个带有

字符串作为DataSource的ArrayList - 没有成功,还有SelectedIndex和

followwing


FindStringExact(& ; searchstring");


谢谢,

Steffen。


" Vince Yuan" < SH ******* @ citiz.net> schrieb im Newsbeitrag

新闻:OF ************** @ tk2msftngp13.phx.gbl ...

嗨Steffen,

您应该使用SelectedIndex属性!


示例

[Visual Basic,C#,C ++]以下示例说明<的用法br />
FindString方法和SelectedIndex属性。该示例是ComboBox类概述中

可运行代码示例的一部分。

[Visual Basic]

Private Sub findButton_Click(ByVal sender As Object,ByVal e As

System.EventArgs)

Dim index As Integer

index = comboBox1.FindString(textBox2.Text)

comboBox1.SelectedIndex = index

End Sub

[C#]

private void findButton_Click(object sender,System.EventArgs e ){

int index = comboBox1.FindString(textBox2.Text);

comboBox1.SelectedIndex = index;

}


" Steffen Laser" < SG ***** @ intermet.de>在消息中写道

新闻:OD ************** @ TK2MSFTNGP09.phx.gbl ...

嗨组,由于没有人可以帮助我,我想在这里再试一次:

我设置了像这样选择一个组合框的项目:

myCombo.SelectedItem = 3;

但之后,ComboBox只显示列表的第一个条目。
当我添加行

myCombo.DroppedDown = true;

之前,然后逐步通过代码,我可以看到组合列表下拉,然后索引(在本例中为3)被选中。之后,
方法完成,没有其他代码被处理。然后,当
表格出现时,会选择第一个项目!

有趣的是,几周前相同的代码工作 - 在这个地方没有什么改变。有人知道会发生什么吗?或者可能是,
,MS补丁或类似的东西有问题?

问候,
Steffen。


嗨Juan,

非常感谢,但我尝试了以下所有方法:


(1 )SelectedIndex = 3;


(2)SelectedItem = object;

//因为数据源包含一个

/的ArrayList /覆盖了ToString()的对象 - 方法。

//我还尝试了一个带字符串作为DataSource的ArrayList

//所有这里提到的方法/属性 - 同样的结果

//在我的第一封邮件中报告。


(3)FindStringExact(" searchstring");


谢谢,

Steffen。


" Vince Yuan" < SH ******* @ citiz.net> schrieb im Newsbeitrag

新闻:OF ************** @ tk2msftngp13.phx.gbl ...

嗨Steffen,

您应该使用SelectedIndex属性!


示例

[Visual Basic,C#,C ++]以下示例说明<的用法br />
FindString方法和SelectedIndex属性。该示例是ComboBox类概述中

可运行代码示例的一部分。

[Visual Basic]

Private Sub findButton_Click(ByVal sender As Object,ByVal e As

System.EventArgs)

Dim index As Integer

index = comboBox1.FindString(textBox2.Text)

comboBox1.SelectedIndex = index

End Sub

[C#]

private void findButton_Click(object sender,System.EventArgs e ){

int index = comboBox1.FindString(textBox2.Text);

comboBox1.SelectedIndex = index;

}


" Steffen Laser" < SG ***** @ intermet.de>在消息中写道

新闻:OD ************** @ TK2MSFTNGP09.phx.gbl ...

嗨组,由于没有人可以帮助我,我想在这里再试一次:

我设置了像这样选择一个组合框的项目:

myCombo.SelectedItem = 3;

但之后,ComboBox只显示列表的第一个条目。
当我添加行

myCombo.DroppedDown = true;

之前,然后逐步通过代码,我可以看到组合列表下拉,然后索引(在本例中为3)被选中。之后,
方法完成,没有其他代码被处理。然后,当
表格出现时,会选择第一个项目!

有趣的是,几周前相同的代码工作 - 在这个地方没有什么改变。有人知道会发生什么吗?或者可能是,
,MS补丁或类似的东西有问题?

问候,
Steffen。


Hi group,
I have a problem that I already have posted to the german C# newsgroup.
Since nobody could help me there, I''d like to try it here again:

I set the selected item of a combobox like this:

myCombo.SelectedItem = 3;

But after that the ComboBox displays only the first entry of the list.
When I add the line

myCombo.DroppedDown = true;

before, and then step trough the code, I can see the list of the combo
dropping down, then the index (in this case 3) gets selected. After that,
the method is finished, no other code is processed. And then, when the form
comes up, there is the first item selected!

Funny, that the same code works some weeks ago - at this place nothing
changed. Does anybody have an idea what there happens? Or could it be, that
there is a problem with a MS patch or something like that?

Greetings,
Steffen.

解决方案

Hi Steffen,
You should use SelectedIndex property!

Example
[Visual Basic, C#, C++] The following example illustrates the usage of the FindString method and SelectedIndex property. The example is part of a runnable code sample in the ComboBox class overview.

[Visual Basic]
Private Sub findButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim index As Integer
index = comboBox1.FindString(textBox2.Text)
comboBox1.SelectedIndex = index
End Sub
[C#]
private void findButton_Click(object sender, System.EventArgs e) {
int index = comboBox1.FindString(textBox2.Text);
comboBox1.SelectedIndex = index;
}"Steffen Laser" <sg*****@intermet.de> wrote in message news:OD**************@TK2MSFTNGP09.phx.gbl...

Hi group,
I have a problem that I already have posted to the german C# newsgroup.
Since nobody could help me there, I''d like to try it here again:

I set the selected item of a combobox like this:

myCombo.SelectedItem = 3;

But after that the ComboBox displays only the first entry of the list.
When I add the line

myCombo.DroppedDown = true;

before, and then step trough the code, I can see the list of the combo
dropping down, then the index (in this case 3) gets selected. After that,
the method is finished, no other code is processed. And then, when the form
comes up, there is the first item selected!

Funny, that the same code works some weeks ago - at this place nothing
changed. Does anybody have an idea what there happens? Or could it be, that
there is a problem with a MS patch or something like that?

Greetings,
Steffen.



Hi Juan,
thank you very much, but I tried all of the following:

SelectedIdex = 3;

SelectedItem = object; // since the datasource contains an ArrayList of
objects with overridden ToString()-Method. I also tried an ArrayList with
Strings as DataSource - without success, also with SelectedIndex and the
followwing

FindStringExact("searchstring");

Thanks,
Steffen.

"Vince Yuan" <sh*******@citiz.net> schrieb im Newsbeitrag
news:OF**************@tk2msftngp13.phx.gbl...
Hi Steffen,
You should use SelectedIndex property!

Example
[Visual Basic, C#, C++] The following example illustrates the usage of the
FindString method and SelectedIndex property. The example is part of a
runnable code sample in the ComboBox class overview.
[Visual Basic]
Private Sub findButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim index As Integer
index = comboBox1.FindString(textBox2.Text)
comboBox1.SelectedIndex = index
End Sub
[C#]
private void findButton_Click(object sender, System.EventArgs e) {
int index = comboBox1.FindString(textBox2.Text);
comboBox1.SelectedIndex = index;
}

"Steffen Laser" <sg*****@intermet.de> wrote in message
news:OD**************@TK2MSFTNGP09.phx.gbl...

Hi group,
I have a problem that I already have posted to the german C# newsgroup.
Since nobody could help me there, I''d like to try it here again:

I set the selected item of a combobox like this:

myCombo.SelectedItem = 3;

But after that the ComboBox displays only the first entry of the list.
When I add the line

myCombo.DroppedDown = true;

before, and then step trough the code, I can see the list of the combo
dropping down, then the index (in this case 3) gets selected. After that,
the method is finished, no other code is processed. And then, when the form comes up, there is the first item selected!

Funny, that the same code works some weeks ago - at this place nothing
changed. Does anybody have an idea what there happens? Or could it be, that there is a problem with a MS patch or something like that?

Greetings,
Steffen.



Hi Juan,
thank you very much, but I tried all of the following:

(1) SelectedIndex = 3;

(2) SelectedItem = object;
// since the datasource contains an ArrayList of
//objects with overridden ToString()-Method.
//I also tried an ArrayList with Strings as DataSource
// with all here mentioned methods/Properties - with the same result
// as reported in my first mail.

(3) FindStringExact("searchstring");

Thanks,
Steffen.

"Vince Yuan" <sh*******@citiz.net> schrieb im Newsbeitrag
news:OF**************@tk2msftngp13.phx.gbl...
Hi Steffen,
You should use SelectedIndex property!

Example
[Visual Basic, C#, C++] The following example illustrates the usage of the
FindString method and SelectedIndex property. The example is part of a
runnable code sample in the ComboBox class overview.
[Visual Basic]
Private Sub findButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim index As Integer
index = comboBox1.FindString(textBox2.Text)
comboBox1.SelectedIndex = index
End Sub
[C#]
private void findButton_Click(object sender, System.EventArgs e) {
int index = comboBox1.FindString(textBox2.Text);
comboBox1.SelectedIndex = index;
}

"Steffen Laser" <sg*****@intermet.de> wrote in message
news:OD**************@TK2MSFTNGP09.phx.gbl...

Hi group,
I have a problem that I already have posted to the german C# newsgroup.
Since nobody could help me there, I''d like to try it here again:

I set the selected item of a combobox like this:

myCombo.SelectedItem = 3;

But after that the ComboBox displays only the first entry of the list.
When I add the line

myCombo.DroppedDown = true;

before, and then step trough the code, I can see the list of the combo
dropping down, then the index (in this case 3) gets selected. After that,
the method is finished, no other code is processed. And then, when the form comes up, there is the first item selected!

Funny, that the same code works some weeks ago - at this place nothing
changed. Does anybody have an idea what there happens? Or could it be, that there is a problem with a MS patch or something like that?

Greetings,
Steffen.



这篇关于设置ComboBox的选定索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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