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

问题描述 投票:2回答:6

简单的问题:我正在检查组合框是否具有通过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#程序-因此,如果我很愚蠢,请不要对我大喊大叫。

c# visual-studio-2010
6个回答
4
投票

通常是一些观察/建议。

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

[什么都没有选择时,SelectedItem将返回NULL,另一个选项正在使用SelectedIndex,当没有选择任何项时,它将返回-1。

因此,使用SelectedIndex它将变成类似的样子;

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

通常,只有在不可能进行任何“强”像整数比较甚至更好的枚举比较的情况下,才使用字符串比较。 (虽然可能只是我一个人,但字符串经常更改,对于这种东西来说很令人恐惧。)

对于枚举建议,可能请看以下链接之一;

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

[我不确定在WPF中使用哪个.NET版本和您用作绑定的东西在WPF中要比在旧的Windows窗体中容易得多(我认为)。


1
投票

MSDN doc,这将完全回答您的问题

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


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

}

0
投票

Microsoft命名不佳。您应该使用comboBox.Text来获取所需的内容。

comboBox.SelectedIndex所选值的索引

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

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

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

[.SelectedText,指的是在.Text中选择和突出显示的文本


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

}

0
投票

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

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   
    }
© www.soinside.com 2019 - 2024. All rights reserved.