Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello all,
I have a question with C# WPF I have a window that passes a User object into the next window and in the constructor I am setting a default combobox value to what the users property is. In the combobox_Selection() I have it so when this specific property is selected a MessageBox.Show(“”,btns) runs and you must click yes/no. I would like to have it so it skips this messagebox in the constructor where I auto set the value.
C#
        public ProfileScreen(User user)
        {
            currentUser = user;
            InitializeComponent();

            if (user.IsCustomer == true)
            {
                Customer customerLoaded = UsersDB.ReadCustomerById(user.UserID);
                PopulateControls(customerLoaded);
//HERE IS WHERE I SET THE COMBOBOX VALUE AND IT RUNS THE MESSAGEBOX
                CboCustomer.SelectedIndex = 2;
                string loadedState = customerLoaded.State;
                ComboboxState.SelectedValue = loadedState;
            }            
        }
private void CboCustomer_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string cboValue = "";
            if (CboCustomer.SelectedIndex > 0)
                cboValue = ((ComboBoxItem)CboCustomer.SelectedItem).Content.ToString();

            if(cboValue.Equals("Yes"))
            {
                boolIsCustomer = true;
                User addCustomer = null;
                Customer newCustomer = null;

                MessageBoxResult result = MessageBox.Show("Updating database to customer status.",
                    "Customer Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Exclamation);
                if (result == MessageBoxResult.Yes)
                {
                    if (currentUser.IsCustomer == true)
                    {
                        try
                        {                          
                            Customer custTestID = UsersDB.ReadCustomerById(currentUser.UserID);

                            if(currentUser.UserID == custTestID.UserID)
                            {
                                return;
                            }                            
                        }
                        catch(Exception ex) { MessageBox.Show(ex.Message.ToString()); }
                    }
                }
            }
        }
    }


What I have tried:

returns and auto select
setting default
Posted
Updated 30-Nov-18 15:11pm

1 solution

The code is doing exactly what you have asked it to do. When the selection changes, default is no selection, the SelectionChanged is fired from the ctor method. IF you set a breakpoint on the line CboCustomer.SelectedIndex = 2; then step through the execution line by line, you will see this happening. It is call debugging.

So, if this is NOT the intended behavior expected, then you need to add a flag to shortcut the SelectionChanged event. Here is how you would do that:
C#
private bool isLoading;

public ProfileScreen(User user)
{
    isLoadiing = true;

    // do initialization here...

    isLoadiing = false;
}

Now when you don't want events firing upon initialization you do the following:
C#
private void CboCustomer_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (isloading) return;

    // Code goes here...
}
 
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