Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 11th, 2006, 12:45 PM
Authorized User
 
Join Date: May 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to ColdFusion
Default comboBox selectedindex problem

I have two comboBox that are dependend on each other. If one changes the other changes as well. Now that is not a problem there what I can see, the problem is when this windowsform gets loaded and for some reason the selectedindex stands on 0 instead of -1 for a second before displayed where it gets to -1 as told. Now my problem here is how it can be selectedindex 0, this problem leads to the DialogResult being set to OK where it not should be.

Help would be appreciated.



The code:


        private void Initialize_comboBox_FindCustomerByName()
        {
            ArrayList cutomerIdList = customerLogic.GetCustomers();
            if (0 < cutomerIdList.Count)
            {
                ArrayList customers = new ArrayList();
                customers.Add(new Customer("Ny Kund"));
                foreach (int i in cutomerIdList)
                {
                    customers.Add(new Customer(i));
                }
                comboBox_FindCustomerByName.DataSource = customers;
                comboBox_FindCustomerByName.DisplayMember = "Name";
                comboBox_FindCustomerByName.ValueMember = "Id";
            }
            comboBox_FindCustomerByName.SelectedIndex = -1;
            comboBox_FindCustomerByName.Text = "Sök kund";
        }
        private void Initialize_comboBox_FindCustomerByNumber()
        {
            ArrayList cutomerIdList = customerLogic.GetCustomers_SortedByCustomerNumber( );
            if (0 < cutomerIdList.Count)
            {
                ArrayList customers = new ArrayList();
                customers.Add(new Customer("Ny Kund"));
                foreach (int i in cutomerIdList)
                {
                    customers.Add(new Customer(i));
                }
                comboBox_FindCustomerByNumber.DataSource = customers;
                comboBox_FindCustomerByNumber.DisplayMember = "CustomerNumber";
                comboBox_FindCustomerByNumber.ValueMember = "Id";
            }
            comboBox_FindCustomerByNumber.SelectedIndex = -1;
            comboBox_FindCustomerByNumber.Text = "Sök kund";
        }


       private void comboBox_FindCustomerByName_SelectedIndexChanged(o bject sender, System.EventArgs e)
        {
            if (!comboBox_FindCustomerByName.SelectedIndex.Equals (-1))
            {
                button_Save.Enabled = true;
                if (comboBox_FindCustomerByName.SelectedValue.Equals(-2))
                {
                    this.DialogResult = DialogResult.No;
                    currentCustomer = null;
                }
                else
                {
                    comboBox_FindCustomerByNumber.SelectedValue = comboBox_FindCustomerByName.SelectedValue;
                    currentCustomer = (Customer)comboBox_FindCustomerByNumber.SelectedIt em;
                    this.DialogResult = DialogResult.OK;
                }
            }
            else
            {
                button_Save.Enabled = false;
                this.DialogResult = DialogResult.No;
            }
        }
        private void comboBox_FindCustomerByNumber_SelectedIndexChanged (object sender, System.EventArgs e)
        {
            if (!comboBox_FindCustomerByNumber.SelectedIndex.Equa ls(-1))
            {
                button_Save.Enabled = true;
                if (comboBox_FindCustomerByNumber.SelectedValue.Equal s(-2))
                {
                    this.DialogResult = DialogResult.No;
                    currentCustomer = null;
                }
                else
                {
                    comboBox_FindCustomerByName.SelectedValue = comboBox_FindCustomerByNumber.SelectedValue;
                    currentCustomer = (Customer)comboBox_FindCustomerByName.SelectedItem ;
                    this.DialogResult = DialogResult.OK;
                }
            }
            else
            {
                button_Save.Enabled = false;
                this.DialogResult = DialogResult.No;
            }
        }

 
Old May 11th, 2006, 02:46 PM
Authorized User
 
Join Date: May 2006
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

First thing to look at is the function Initialize_comboBox_FindCustomerByName.

The line
   comboBox_FindCustomerByName.SelectedIndex = -1;

will cause the SelectedIndex event to fire as soon as it is executed. Same is true in the other initialize function. Basically, this means that SelectedIndex is being called on one combo box before the other has been initialized.





Brandon
 
Old May 12th, 2006, 02:14 AM
Authorized User
 
Join Date: May 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to ColdFusion
Default

Yes I noticed that, nothing to do about it? Seing how the index from the start is set on 0, I creat that -1 with "Chose" and once chosen something that -1 location will not be visible or selectable again, perfect with other words, were it not for the selectedindex being changed without user having done it.

 
Old May 12th, 2006, 05:06 AM
Authorized User
 
Join Date: Apr 2006
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

you can use a bool variable to prevent from executing the event code. in your init set the bool variable to true and in the event add some code....

if(!isUpdating)
{
.......doeventcode....
}

 
Old May 12th, 2006, 05:37 AM
Authorized User
 
Join Date: May 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to ColdFusion
Default

Feels like a workaround thou but works.






Similar Threads
Thread Thread Starter Forum Replies Last Post
ComboBox Problem sfl234 C# 2 January 6th, 2007 01:22 PM
Combobox SelectedIndex Issue kancha C# 7 July 26th, 2006 07:14 AM
SelectedIndex () in Windows 98!! problem thomaz C# 0 March 21st, 2005 02:29 PM
ComboBox problem chacquard Access 2 September 10th, 2004 11:40 AM
Problem Setting Tab Control selectedIndex Ron Howerton VB.NET 2002/2003 Basics 0 July 13th, 2004 03:31 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.