无法在ComboBox中将SelectedIndex设置为-1 [英] Cannot set SelectedIndex to -1 in ComboBox

查看:82
本文介绍了无法在ComboBox中将SelectedIndex设置为-1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我已经缩小了一个问题我一直在填充一个组合框,其中

存储的查询结果数据表。我正在将ValueMember和

DisplayMember设置为相关的列名,然后我想确保

没有选择任何内容。


默认情况下,如果你什么也不做,那么第一项(0)就是

被选中。但是,我想将SelectedIndex设置为-1。


如果我有一个独立的窗口它可以工作,但如果我有一个窗口有一个

mdiparent set,它忽略了我将SelectedIndex设置为-1的尝试和

总是选择项目0.


(这里是我的代码...... )

ComboBox1.DataSource = dt

ComboBox1.ValueMember =" chCountryCode"

ComboBox1.DisplayMember =" chCountryDesc"

ComboBox1.SelectedIndex = -1


我已经尝试过两次设置SelectedIndex = -1,但这并没有解决

它。唯一似乎解决这个问题的是使窗口非childmdi,

但我们的应用程序是一个MDI风格的应用程序!


这是一个错误吗?请问有什么想法吗?


非常感谢,


史蒂夫

Hi,

I''ve narrowed down a problem I''ve been having filling a combobox with the
results of a query stored in a datatable. I''m setting the ValueMember and
DisplayMember to the relevant column names, and then I want to make sure
nothing is selected.

By default it appears that if you do nothing, the first item (0) is
selected. However, I want to set the SelectedIndex to -1.

If I have a standalone window it works, but if I have a window that has an
mdiparent set, it ignores my attempts to set the SelectedIndex to -1 and
always selects item 0.

(here''s my code...)
ComboBox1.DataSource = dt
ComboBox1.ValueMember = "chCountryCode"
ComboBox1.DisplayMember = "chCountryDesc"
ComboBox1.SelectedIndex = -1

I''ve already tried setting SelectedIndex = -1 twice, but that doesn''t fix
it. The only thing that seems to fix it is making the window non-childmdi,
but our application is an MDI style app!

Is this a bug? Any ideas please?

Many thanks,

Steve

推荐答案

" Steve Dyte" < Steve Dy**@discussions.microsoft.com > schrieb:
"Steve Dyte" <Steve Dy**@discussions.microsoft.com> schrieb:
ComboBox1.ValueMember =" chCountryCode"
ComboBox1.DisplayMember =" chCountryDesc"
ComboBox1.SelectedIndex = -1

我'我已经尝试过两次设置SelectedIndex = -1,但这并没有解决它。唯一似乎解决它的问题是使窗口非childmdi,
但我们的应用程序是MDI风格的应用程序!
ComboBox1.ValueMember = "chCountryCode"
ComboBox1.DisplayMember = "chCountryDesc"
ComboBox1.SelectedIndex = -1

I''ve already tried setting SelectedIndex = -1 twice, but that doesn''t fix
it. The only thing that seems to fix it is making the window non-childmdi,
but our application is an MDI style app!




BUG:ComboBox不将SelectedIndex设置为-1时清除

< URL:http://support.microsoft.com/?scid = kb; EN-US; 327244>


-

Herfried K. Wagner [MVP]

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



BUG: ComboBox Does Not Clear When You Set SelectedIndex to -1
<URL:http://support.microsoft.com/?scid=kb;EN-US;327244>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>


" Steve Dyte" < Steve Dy**@discussions.microsoft.com > schrieb:
"Steve Dyte" <Steve Dy**@discussions.microsoft.com> schrieb:
ComboBox1.ValueMember =" chCountryCode"
ComboBox1.DisplayMember =" chCountryDesc"
ComboBox1.SelectedIndex = -1

我'我已经尝试过两次设置SelectedIndex = -1,但这并没有解决它。唯一似乎解决它的问题是使窗口非childmdi,
但我们的应用程序是MDI风格的应用程序!
ComboBox1.ValueMember = "chCountryCode"
ComboBox1.DisplayMember = "chCountryDesc"
ComboBox1.SelectedIndex = -1

I''ve already tried setting SelectedIndex = -1 twice, but that doesn''t fix
it. The only thing that seems to fix it is making the window non-childmdi,
but our application is an MDI style app!




BUG:ComboBox不将SelectedIndex设置为-1时清除

< URL:http://support.microsoft.com/?scid = kb; EN-US; 327244>


-

Herfried K. Wagner [MVP]

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



BUG: ComboBox Does Not Clear When You Set SelectedIndex to -1
<URL:http://support.microsoft.com/?scid=kb;EN-US;327244>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>


我担心这不能解决问题。我已经看过这个bug了。并且

尝试了解决方法。它说它影响了Framework的1.0版,

而我们在v1.1上。


如果我创建一个普通的窗口,它工作正常。只有当我的窗口是MDIParent的一个

子窗口时,它才会忽略我设置SelectedIndex的尝试

= -1。

您应该能够轻松地重新创建这个...


创建一个包含两个MenuItems的表单...


Private Sub MenuItem1_Click(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理MenuItem1.Click


''如果我这样做它将忽略我的SelectedIndex = -1并将其设置为0

Dim x As New Form2

x.MdiParent = Me

x.Show ()

End Sub


Private Sub MenuItem2_Click(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理MenuItem2.Click


''如果我这样做,它将设置SelectedIndex = -1没有问题!!

Dim x As New Form2

x.Show()


End Sub


仅供参考,Form2是我的测试表格。它有一个组合框,并且表格中的代码加载

来获取DataTable,将其绑定到组合框并尝试设置SelectedIndex

= -1(参见我的原创帖子)


谢谢,


Steve

" Herfried K. Wagner [MVP]"写道:
Hi, I''m afraid this doesn''t fix the problem. I''d already seen this "bug" and
tried the workarounds. It says it affects version 1.0 of the Framework,
whereas we''re on v1.1.

If I create a plain window, it works fine. It''s only when my window is a
child window of an MDIParent that it ignores my attemps to set SelectedIndex
= -1.

You should be able to recreate this fairly easily...

Create a form with two MenuItems on it...

Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem1.Click

'' if I do this it will ignore my SelectedIndex = -1 and set it to 0
Dim x As New Form2
x.MdiParent = Me
x.Show()
End Sub

Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem2.Click

'' if I do this, it will set SelectedIndex = -1 with no problems!!
Dim x As New Form2
x.Show()

End Sub

FYI, Form2 is my test form. It has one combobox, and code in the form load
to get a DataTable, bind it to the combobox and try to set the SelectedIndex
= -1 (see my original post)

Thanks,

Steve
"Herfried K. Wagner [MVP]" wrote:
" Steve Dyte" < Steve Dy**@discussions.microsoft.com > schrieb:
"Steve Dyte" <Steve Dy**@discussions.microsoft.com> schrieb:
ComboBox1.ValueMember =" chCountryCode"
ComboBox1.DisplayMember =" chCountryDesc"
ComboBox1.SelectedIndex = -1

我'我已经尝试过两次设置SelectedIndex = -1,但这并没有解决它。唯一似乎解决它的是使窗口非childmdi,
但我们的应用程序是一个MDI风格的应用程序!
ComboBox1.ValueMember = "chCountryCode"
ComboBox1.DisplayMember = "chCountryDesc"
ComboBox1.SelectedIndex = -1

I''ve already tried setting SelectedIndex = -1 twice, but that doesn''t fix
it. The only thing that seems to fix it is making the window non-childmdi,
but our application is an MDI style app!



BUG:ComboBox在您设置时不会清除SelectedIndex为-1
< URL:http://support.microsoft.com/?scid = kb; EN-US; 327244>

-
Herfried K. Wagner [MVP]
< URL:http://dotnet.mvps.org/>



BUG: ComboBox Does Not Clear When You Set SelectedIndex to -1
<URL:http://support.microsoft.com/?scid=kb;EN-US;327244>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>



这篇关于无法在ComboBox中将SelectedIndex设置为-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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