设置默认项组合框中 [英] Setting default item in combo box

查看:137
本文介绍了设置默认项组合框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数在一个组合框设置项目和一个项目是被默认像

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?

推荐答案

我已经重写这个答案澄清一些东西。

I've rewritten this answer to clarify some stuff.

第一,默认的文本也必须被加入组合项目。 combo.Text 属性
的应用只是增加了描述性文本组合框,这是迷失第一次做用户与控制的东西。
。如果你想永久拥有您的组合默认的文字,您必须将其添加为ComboBox项。

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-";

要搜索

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

请注意,这样你添加的项目 - 选择操作数 - 来的第0(读第一)位置列表。
还要确保你所有的下列项目加1,因为它们现在用一个空格在列表中向下移动。

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.

最后,把

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.

一件事。我不是很确定你想实现与超越的设置组合项目的代码是什么,但如果你想检查什么用户选择使用 cboOperations.SelectedIndex 属性,该属性包含当前在组合选择的项目。您可以添加简单的

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 ;)

这篇关于设置默认项组合框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆