combobox mdi load selectedIndex

K

Kalvin

I have seen this question raised, but I cannot find an answer.

I have an MDI app, when I load an child form with a combobox being
bound in the load event, it won't allow me to set selectedindex = -1.
If it isn't an mdi child, then there isn't a problem. I have tried
setting it to -1 two times. I have tried setting the selecteditem
property to -1 and also to nothing. None of it seems to make a
difference.

Any help will be appreciated.
Kalvin
 
B

Bernie Yaeger

Hi Kalvin,

Can we see some code? I have literally hundreds of such child forms and
selectedindex = -1 (twice) works fine.

Bernie Yaeger
 
K

Kalvin

When the form is not an MDI child form, this works fine and there is
not a value in the combobox. When the form is an MDI child form, and
this is in the Load event, then there will be a value in the combobox.

Thank you for looking at this.

/****** CODE STARTS HERE *******/
Private Sub frmTest_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim LotServer As New LotServer
Dim Lots As LotCollection

Try
Dim conn As SqlConnection = New SqlConnection({your
connection})
conn.Open()

Dim dt As New DataTable
Dim cmd As SqlCommand = New SqlCommand
cmd.CommandText = {your query}
cmd.CommandType = CommandType.Text
cmd.Connection = conn

Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)

da.Fill(dt)

box1.DataSource = dt
box1.ValueMember = "ID"
box1.DisplayMember = "Name"
box1.SelectedIndex = -1
box1.SelectedIndex = -1

conn.Close()

Catch exc As Exception
MessageBox.Show(exc.Message.ToString)
End Try

End Sub
 
B

Bernie Yaeger

Hi Kalvin,

OK, there are two 'answers'. When I followed your code, I experienced the
same condition you describe. However, when I loaded the data through a
dataset created by the vs .net wizard, it worked fine with selectedindex
= -1 twice. Also, when I load a combobox in additem mode, using a datatable
as you created, it was also fine.

HTH,

Bernie Yaeger
 
K

Kalvin

That makes sense. I need to be able to bind it to a user defined
collection. I have six combo boxes. The first one loads when the form
opens, and then the others load depending on the user choice in the
first box. What I did was is just manually load the box. Then it isn't
bound and doesn't automatically pick the first item. I would rather be
able to bind it like the others though.

Thank you for your help and input, I really appreciate it. Sometimes
it's nice just to know I'm not the only one it doesn't work right for.

/**** CODE STARTS HERE ****/
Friend Function LoadCategories(ByRef DropDown As ComboBox) As
CategoryCollection

'Written(By) : KTuel
'Parameters: DropwDown - A combobox to be filled with the
categories
'
'Task: Load all product categories
'
'Revision(History)
'02/09/2005 - Created kTuel

Dim CategoryServer As New CategoryServer
Dim Categories As CategoryCollection

Categories = CategoryServer.LoadAllCategories()

'.NET has a bug in MDI applications that when a combo box is
bound to a source and
'the combobox is populated in the load event of a child form,
you can't set the
'selectedindex to -1. Here we are populating the combobox
without binding it
Dim Category As Category


If Categories.Count > 0 Then
DropDown.SuspendLayout()

For Each Category In DirectCast(Categories,
IDictionary).Values()
DropDown.Items.Add(Category)
Next Category
DropDown.DisplayMember = "Name"
DropDown.ValueMember = "ID"

DropDown.SelectedIndex = -1
DropDown.SelectedIndex = -1

DropDown.ResumeLayout()

End If

Return Categories

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top