如何通过函数获得`combobox`的值

时间:2023-01-06 19:36:46
public string year(ComboBox cbYears)
{
    string value = null;
    value= cbYears.SelectedText;
    return value;
}

This is my function but i get always null value, where is my mistake can anyone help me ?

这是我的功能,但我总是得到空值,我的错误在哪里可以帮助我?

3 个解决方案

#1


1  

You can use the GetItemText method to get the text of the selected combobox item :

您可以使用GetItemText方法获取所选组合框项目的文本:

public string year(ComboBox cbYears)
{
    return cbYears.GetItemText(cbYears.SelectedItem);
}

#2


0  

You should do it like this:

你应该这样做:

string value = cbYears.SelectedItem.Text; return value;

string value = cbYears.SelectedItem.Text;回报值;

#3


0  

I think it would work

我认为它会起作用

 public string year(ComboBox cbYears)
    {
        var result = cbYears.SelectedItem as string;
        return result;
    }

#1


1  

You can use the GetItemText method to get the text of the selected combobox item :

您可以使用GetItemText方法获取所选组合框项目的文本:

public string year(ComboBox cbYears)
{
    return cbYears.GetItemText(cbYears.SelectedItem);
}

#2


0  

You should do it like this:

你应该这样做:

string value = cbYears.SelectedItem.Text; return value;

string value = cbYears.SelectedItem.Text;回报值;

#3


0  

I think it would work

我认为它会起作用

 public string year(ComboBox cbYears)
    {
        var result = cbYears.SelectedItem as string;
        return result;
    }