By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,709 Members | 2,153 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,709 IT Pros & Developers. It's quick & easy.

combobox mdi load selectedIndex

P: n/a
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

Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi Kalvin,

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

Bernie Yaeger

"Kalvin" <kt***@streck.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
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

Nov 21 '05 #2

P: n/a
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

Nov 21 '05 #3

P: n/a
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

"Kalvin" <kt***@streck.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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

Nov 21 '05 #4

P: n/a


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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.