Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Consider there is a combobox with four items(All,Chemistry,Physics,Maths) in a windows form.I need to give item "All" when form loading.Then I can see the combobox with item "All" after loading the form.For this I can use form_load event.After loading the form I need to execute some code when I change items in combobox.For this I used SelectedIndexChange event in combobox.But when I load the form there is an error because when I load the form both events are executed.What should I do.
Posted
Updated 23-Dec-17 9:30am
Comments
Sandeep Mewara 9-Jan-13 0:10am    
What error?
ridoy 9-Jan-13 0:35am    
which error?
MT_ 9-Jan-13 1:32am    
Did you tried what I have suggested below ?
NISHAN SANDEEPA 9-Jan-13 4:20am    
Yes I tried that.That's why I accepted your solution.

Hi,

I was wondering, if you can use SelectionChangeCommitted event rather. This will fire only when item is really selected by the user. Just tested it and it doesn't fire on setting up default value in the form load.

If you "HAVE" to use selectedIndexChanged event, use what Christian Graus has suggested.
Remove event handler you might have added through your form designer, now in your form load do as shown below.

C#
private void Form1_Load(object sender, EventArgs e)
{
   comboBox1.SelectedIndex = 1;
    comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
}


Do remember to remove the event handler binding from form1.designer.cs after you remove it from form designer


Hope that helps

Milind
 
Share this answer
 
v2
Comments
[no name] 9-Jan-13 0:25am    
http://msdn.microsoft.com/en-in/library/system.web.ui.webcontrols.dropdownlist_events.aspx
Create the selected index changed event in your load event, instead of having it defined in the designer. Then it won't fire, because it won't be there when you're setting it in code.
 
Share this answer
 
Comments
NISHAN SANDEEPA 9-Jan-13 0:20am    
Will you please elaborate Christian.
Christian Graus 9-Jan-13 0:23am    
I have no clue how to spell it out more clearly. You can hook up an event in code. Do so in your load event
NISHAN SANDEEPA 9-Jan-13 0:27am    
Can I chat with you by gmail or face book.
Christian Graus 9-Jan-13 0:28am    
Not a chance in hell. This is really trivial. Honestly, it is.
NISHAN SANDEEPA 9-Jan-13 0:31am    
Ok doesn't matter. I am a beginner for programming.I'll try again.Thank you so much.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900