.Net Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - More.Net Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 6th, 2008, 03:59 PM
gavco98uk gavco98uk is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 3 gavco98uk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 26 m 4 sec
Reputation Power: 0
Setting Combobox Default Selection

I have a combobox with one or more options, when the user makes a selection the SelectedIndexChanged event is called, and a datagrid is populated with data depending on what was selected.

My problem is that by default when the window loads the first item is selected in the combobox, but the event never fires. I can manually set a value in the constructor, such as SelectedIndex = 0, but this never fires the event either, whereas SelectedIndex = 1 or greater will cause the event to trigger. However sometimes there may only be a single entry in the list, and id rather select the first item than the second!

How can I ensure that when the form loads a value is selected and the SelectedIndexChanged event triggers?

Thanks in advance!

Reply With Quote
  #2  
Old May 6th, 2008, 08:29 PM
Raytheon Raytheon is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 97 Raytheon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 32 m 28 sec
Reputation Power: 5
Put this in your forms's Load event:
Code:
yourcombobox.SelectedIndex = 0


That should do it.

Last edited by Raytheon : May 6th, 2008 at 08:31 PM.

Reply With Quote
  #3  
Old May 7th, 2008, 08:41 AM
gavco98uk gavco98uk is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 3 gavco98uk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 26 m 4 sec
Reputation Power: 0
Quote:
Originally Posted by Raytheon
Put this in your forms's Load event:
Code:
yourcombobox.SelectedIndex = 0


That should do it.


Hi Raytheon, looks like I screwed up my original post - that is the value I tried to set, NOT SelectedValue - however while it works for position 1 and greater, it doesnt work when you select the first item in the list.

Reply With Quote
  #4  
Old May 7th, 2008, 11:05 AM
LyonHaert's Avatar
LyonHaert LyonHaert is offline
Arcane Scribbler
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2005
Location: Indianapolis, IN
Posts: 1,848 LyonHaert User rank is Colonel (50000 - 60000 Reputation Level)LyonHaert User rank is Colonel (50000 - 60000 Reputation Level)LyonHaert User rank is Colonel (50000 - 60000 Reputation Level)LyonHaert User rank is Colonel (50000 - 60000 Reputation Level)LyonHaert User rank is Colonel (50000 - 60000 Reputation Level)LyonHaert User rank is Colonel (50000 - 60000 Reputation Level)LyonHaert User rank is Colonel (50000 - 60000 Reputation Level)LyonHaert User rank is Colonel (50000 - 60000 Reputation Level)LyonHaert User rank is Colonel (50000 - 60000 Reputation Level)LyonHaert User rank is Colonel (50000 - 60000 Reputation Level)LyonHaert User rank is Colonel (50000 - 60000 Reputation Level)LyonHaert User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 10 h 37 m 20 sec
Reputation Power: 524
How are you populating the ComboBox?
__________________
Joel B Fant
"An element of conflict in any discussion is a very good thing. Shows everybody's taking part, nobody left out. I like that."

.NET Must-Haves
Tools: Reflector
References: Threading in .NET

Reply With Quote
  #5  
Old May 10th, 2008, 09:18 AM
gavco98uk gavco98uk is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 3 gavco98uk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 26 m 4 sec
Reputation Power: 0
Quote:
Originally Posted by LyonHaert
How are you populating the ComboBox?


The combobox is populated from a DataTable, by assigning the the table to datasource.

Reply With Quote
  #6  
Old May 11th, 2008, 01:29 PM
LyonHaert's Avatar
LyonHaert LyonHaert is offline
Arcane Scribbler
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2005
Location: Indianapolis, IN
Posts: 1,848 LyonHaert User rank is Colonel (50000 - 60000 Reputation Level)LyonHaert User rank is Colonel (50000 - 60000 Reputation Level)LyonHaert User rank is Colonel (50000 - 60000 Reputation Level)LyonHaert User rank is Colonel (50000 - 60000 Reputation Level)LyonHaert User rank is Colonel (50000 - 60000 Reputation Level)LyonHaert User rank is Colonel (50000 - 60000 Reputation Level)LyonHaert User rank is Colonel (50000 - 60000 Reputation Level)LyonHaert User rank is Colonel (50000 - 60000 Reputation Level)LyonHaert User rank is Colonel (50000 - 60000 Reputation Level)LyonHaert User rank is Colonel (50000 - 60000 Reputation Level)LyonHaert User rank is Colonel (50000 - 60000 Reputation Level)LyonHaert User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 10 h 37 m 20 sec
Reputation Power: 524
One option is to call combobox.BeginUpdate() and use a foreach loop to populate the Items collection yourself, then make a selection (or not) manually, then finally call combobox.EndUpdate(). This way, no automatic selection is made for you, as happens with assigning to combobox.DataSource.

Another option is to remove EvenHandlers from the SelectedIndexChanged event, assign the data, make sure nothing is selected, and then restore the EventHandlers.
Code:
C#
comboBox1.BeginUpdate();
comboBox1.SelectedIndexChanged -= comboBox1_SelectedIndexChanged;
comboBox1.DataSource = data;
comboBox1.SelectedIndex = -1; // OR comboBox1.SelectedItem = null;
comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
comboBox1.EndUpdate();
I have a fuzzy memory of there being a property I could change to prevent autoselection when assigning to DataSource on something, but it may not have been a ComboBox.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - More.Net Development > Setting Combobox Default Selection


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT