Click here to Skip to main content
15,917,456 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

while i set Combo Box selected index, automatically that combo box selected index change event fired in windows application, how i can avoid this,
Posted

The best way is to unsubscribe the combo box from the event , do your stuff and re subscribe, something like this

C#
comboBox1.SelectedIndexChanged -= new EventHandler(comboBox1_SelectedIndexChanged);
           //Do some stuff
           comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);


Either that or you can subscribe to the SelectionChangedCommitted event, which only fires when a user changes the combobox selection.


Hope this helps
 
Share this answer
 
v2
Comments
RaviRanjanKr 4-Sep-11 0:01am    
My 5+
I am assuming that you are setting the selected index of the combo box so that it matches some default value, e.g. editing some sort of record.

The easiest way is to remove the event handler before the change and add if back afterwards.

Assuming that the combobox is called comboBox1 and that the event handler is called comboBox1_SelectedIndexChanged then this should do the job.

C#
using System.Collections.Generic;
using System.Windows.Forms;

namespace sample
{
    public partial class Form1 : Form
    {

        // possible values
        private List<string> _items = new List<string>() { "One", "Two" };

        public Form1()
        {
            InitializeComponent();

            DataBind();
        }

        private void DataBind()
        {
            // disable event handler
            comboBox1.SelectedIndexChanged -= this.comboBox1_SelectedIndexChanged;

            // bind possible values
            comboBox1.DataSource = _items;

            //your logic to get this number
            int selectedIndex = 1;

            //set default value - 
            comboBox1.SelectedIndex = selectedIndex;

            // enable event handler
            comboBox1.SelectedIndexChanged += this.comboBox1_SelectedIndexChanged;
        }

        private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            // normal logic to handle selection chnage
            MessageBox.Show("fired");
        }
    }
}</string></string>
 
Share this answer
 
v2
Comments
RaviRanjanKr 4-Sep-11 0:01am    
Nice answer, My 5+
When I need to do that, I create a variable called m_initialized, and set it to an initial value of false. At the end of the Load event handler, I set it to true. Then in the SelectionChanged event handler for the control, I check to see if m_initialized is true, and if so, I handle the event. Otherwise, I exit the handler.
 
Share this answer
 
I run into this when I am setting up a form in the Form Load event. I work around it by declaring a form level variable and checking in right away in the combo box SelectedIndexChange event to decide if I will perform the code in the event or not. so like this: (It's in vb but you should get the idea)

VB
Private boolPastLoad As Boolean = False

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'Add code here to setup the form
    ComboBox1.SelectedIndex = 0

    boolPastLoad = True
End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    If boolPastLoad Then
        'Add code here for whatever should usually happen on the SelectedIndexChanged
    End If

End Sub
 
Share this answer
 
set AutoPostback="false" in aspx page in dropdown control
 
Share this answer
 

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