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

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
LVL 5
arvindb1Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arvindb1Author Commented:
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
NaliniPCommented:
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
arvindb1Author Commented:
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
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

iboutchkineCommented:
Hi Arvind. You are setting the selected index in the form load. Make sure that it is set after form became visible.
0
arvindb1Author Commented:
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
NaliniPCommented:
Arvind,

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

Kant
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
arvindb1Author Commented:
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
arvindb1Author Commented:
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
tbgertCommented:
I think Form_Layout is suspect also since it is fired when the form resizes and consequently resets the selectedindex again.

0
jimmykentCommented:
It appears there is a bug in the .NET Framework's Combobox:

http://support.microsoft.com/default.aspx?scid=kb;EN-US;327244
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.