验证它的SelectedText属性为空的组合框是否总是失败

时间:2013-08-08 09:29:44

标签: c# visual-studio-2010

简单问题:我正在检查组合框是否已使用string.IsNullOrEmpty()选择了一个项目。问题是,即使选择了,也会显示错误消息。我做错了什么?

这是我的代码:

private void button1Click(object sender, EventArgs e)
{
    if (string.IsNullOrEmpty(comboBox1.SelectedText))//here should skip to else - but doesn't
    {
        MessageBox.Show("You must select a conversion type", "Error");
    }
    else
    {
        if (comboBox1.SelectedText == "Currency")
        {
            double input = Convert.ToDouble(textBox1.Text);
            if (!string.IsNullOrEmpty(comboBox2.SelectedText))
            {
                string type = comboBox2.SelectedText;
                double result = convertCurrency(type, input);
                if (result != -1)
                {
                    label1.Text = Convert.ToString(result);
                }
             }
             else
             {
                 MessageBox.Show("You must select a conversion type", "Error");
             }
         }
         else
         {
             MessageBox.Show("curency");
         }
     } 
}

注意:这是我的第二个C#程序 - 所以如果我是愚蠢的话,请不要对我大喊大叫。

6 个答案:

答案 0 :(得分:3)

一般来说有一些观察/建议。

首先,您使用字符串值并基于这些值的逻辑,您可能希望使用Enum并将其所有值绑定到组合框。然后使用SelectedItem属性并将其与Enum进行比较。

当没有选择任何内容时,SelectedItem将返回NULL,另一个选项是使用SelectedIndex,当没有选择任何项目时,它将返回-1。

因此,使用SelectedIndex,它会变成类似的东西;

if (comboBox1.SelectedIndex == -1)//Nothing selected
{
    MessageBox.Show("You must select a conversion type", "Error");
}
else
{
    //Do Magic   
}

通常使用字符串比较只应在任何“强”(如int比较)或更好的枚举比较时才能进行。 (也许它只是我,但字符串经常变化,对这类东西来说只是可怕。)

对于枚举建议,可以查看其中一个链接;

Binding an enum to a WinForms combo box, and then setting it

Load values of enum type into a combobox

Is it possible to load items from an Enum to a ComboBox in .NET 3.5?

Binding a ComboBox to an Enumeration

我不确定哪个.NET版本和你使用的东西作为绑定在WPF中更容易,然后在旧的Windows窗体中(在我看来)。

答案 1 :(得分:1)

private void button2_Click(object sender, EventArgs e)
{
    if (comboBox1.SelectedIndex==-1)
    {
        MessageBox.Show("You must select a conversion type", "Error");
    }
    else
    {
        MessageBox.Show("Selected");
    }

}

答案 2 :(得分:1)

来自MSDN doc

,这完全回答了您的问题

  

您可以使用SelectedText属性来检索或更改   当前在ComboBox控件中选择的文本。但是,你应该   意识到选择可以因用户而自动改变   相互作用。例如,如果您在a中检索SelectedText值   按钮单击事件处理程序,该值将为空字符串。这是   因为当输入焦点时,选择会自动清除   从组合框移动到按钮。

答案 3 :(得分:1)

另一种解决方案: 您可以通过检查索引范围以外的条件(不可能有该项目)来检查组合框是否有选中的项目,然后,如果没有可能的值,则可以手动设置。

if (comboBox1.SelectedIndex == -1) //index out of range
    {
        //show the error message 
        comboBox1.SelectedIndex = 1; //set the first value to the combo box
    }
    else
    {
        //continue   
    }

答案 4 :(得分:0)

微软的命名不佳。您应该使用comboBox.Text来获取您要查找的内容。

comboBox.SelectedIndex所选值的索引

comboBox.SelectedItem如果您使用DataSource,则这是在数据源中选择的项目

comboBox.SelectedValue数据源的ValueMember或当前选择的值(如果您添加了自己的项目)

comboBox.Text您看到的文字,即使它不在列表中

.SelectedText,指的是.Text

中选中(突出显示)的文本

答案 5 :(得分:0)

private void Resetbtn_Click(object sender, EventArgs e)
{    
    comboBox1.Items.Clear();
    //Student is a combobox elements.Add again.
    comboBox1.Items.Add("Student");  
    comboBox1.Items.Add("Staff");

}