Solved

Default SelectedIndex in ComboBox (VB.NET - Windows Control)

Posted on 2003-11-06
11
1,300 Views
Last Modified: 2008-02-26
Its a VB.NET-Windows Control (Combobox)

I'm facing a problem of setting default SelectedIndex of Combobox in form_load.
The Problem is
When form is loaded, i bind the combobox to a dataset which is populated from a database, but by default it always shows first item as selected item in the combobox even when i give
combobox1.selectedindex=-1 after binding combo to dataset. By default what i want is no item should be shown as selected in combobox.
Then i placed a button on the form and gave the same command i.e. combobox1.selectedindex=-1 on click event of button and then it works pretty fine i.e. then no item is shown as selected. I really wonder why the same thing is not working in form_load but it works fine once form is loaded and then on click event of button.

Arvind
0
Comment
Question by:arvindb1
11 Comments
 
LVL 5

Author Comment

by:arvindb1
ID: 9698860
Just to add more information which i just figured out is that if datagrid control is there in the same form then this problem comes otherwise not.
Any solutions???
0
 
LVL 2

Expert Comment

by:NaliniP
ID: 9699311
Hi Aravind,

you need to set that 'selectedindex' property before binding the combo with datasource. Then it will work. Otherwise take list item object and then add that to the combo. For this mail me to <e-mail removed for user privacy>

Kant
0
 
LVL 5

Author Comment

by:arvindb1
ID: 9700004
1. I tried setting selectedindex before binding-- still doesnot work.
2. There is no list item object i have seen in windows forms rather i've seen it in web forms. or are you talking about listviewitem or what? if so then how? can you pls. post some sample code.

Arvind
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 28

Assisted Solution

by:iboutchkine
iboutchkine earned 50 total points
ID: 9700957
Hi Arvind. You are setting the selected index in the form load. Make sure that it is set after form became visible.
0
 
LVL 5

Author Comment

by:arvindb1
ID: 9706244
and how can i do that? in which event? Is there still any event which get fired when form becomes visible without interaction from user?
Arvind
0
 
LVL 2

Accepted Solution

by:
NaliniP earned 75 total points
ID: 9706331
Arvind,

You can do that in FORM_PAINT event. it will fire after the load event. Try to do that

Kant
0
 
LVL 5

Author Comment

by:arvindb1
ID: 9706409
Looks like we are getting closer but still some problem.

I tried with FORM_PAINT event and it works with that but now the problem is that after doing something in form and then change my window focus to some other window and when i return to my form it again resets the selectedindex to -1 (because paint event fires everytime you loose focus and come back again) even if while working on form i changed it to something else.
so any other event that you can suggest????

Thanks a lot
Arvind
0
 
LVL 5

Author Comment

by:arvindb1
ID: 9706422
Hi
I just got it, i tried with Form_Layout event and it works well with it.

Thanks a lot to both iboutchkine and naliniP for their help.
Arvind
0
 

Expert Comment

by:tbgert
ID: 10044263
I think Form_Layout is suspect also since it is fired when the form resizes and consequently resets the selectedindex again.

0
 

Expert Comment

by:jimmykent
ID: 10054469
It appears there is a bug in the .NET Framework's Combobox:

http://support.microsoft.com/default.aspx?scid=kb;EN-US;327244
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question