Setting default item in combo box

I have a function for setting items in a combobox and one item is to be set by default like

--SELECT LIST--

 public void SetOperationDropDown()

    {

        int? cbSelectedValue = null;
        if(cmbOperations.Items.Count == 0)
        {
            //This is for adding four operations with value in operation dropdown  
            cmbOperations.Items.Insert(0, "PrimaryKeyTables");
            cmbOperations.Items.Insert(1, "NonPrimaryKeyTables");
            cmbOperations.Items.Insert(2, "ForeignKeyTables");
            cmbOperations.Items.Insert(3, "NonForeignKeyTables");
            cmbOperations.Items.Insert(4, "UPPERCASEDTables");
            cmbOperations.Items.Insert(5, "lowercasedtables");
            //ByDefault the selected text in the cmbOperations will be -SELECT OPERATIONS-. 
            cmbOperations.Text = "-SELECT OPERATIONS-";
        }
        else
        {
            if(!string.IsNullOrEmpty("cmbOperations.SelectedValue"))
            {
                cbSelectedValue = Convert.ToInt32(cmbOperations.SelectedValue);
            }
        }
        //Load the combo box cmbOperations again 
        if(cbSelectedValue != null)
        {
            cmbOperations.SelectedValue = cbSelectedValue.ToString();
        }
    }

Can anyone suggest a way to do this?

Answers


I've rewritten this answer to clarify some stuff.

First, the "default" text must be added as combo item as well. Usage of combo.Text property just adds descriptive text to combobox which is "lost" first time user do something with a control. If you like to permanently have "default" text in your combo, you must add it as an combobox item.

By the code you provided, just modify the

cmbOperations.Text = "-SELECT OPERATIONS-";
to

cmbOperations.Items.Insert(0, "-SELECT OPERATIONS-");

Note that this way you add the item "-SELECT OPERANDS-" to the 0th (read first) position in the list. Also make sure that all your following items are increased by 1, because they are now moved by one space down in list.

Finally, put

cboOperations.SelectedIndex = 0;
line at the end of code. By doing so, you're telling combobox to display your "default" item initially when the form (or control) loads.

One more thing. I'm not pretty sure what do you want to achieve with the code beyond setting combo items, but if you like to check what user selected use cboOperations.SelectedIndex property which contains currently selected item in combo. You can add simple

if(cboOperations.SelectedIndex == someIntValue){...}
The rest is your program logic ;)


Need Your Help

How to Create ONE list which can have a string or image as an item in the list

android image listview text listadapter

I want to create a journal that appears as a list in a ListView. The list should be able to have a text entry or an image for each row. Now, I have these entries (text and image) in a database. I j...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.