C# ComboBox 获取选中项字符串:SelectedItem 和 SelectedItems 属性
在 C# 中,可以使用 ComboBox.SelectedItem 属性来获取 ComboBox 中当前选中的字符串。如果 ComboBox 允许多选,则可以使用 ComboBox.SelectedItems 属性来获取所有选中的字符串。
获取单选字符串:
string selectedString = comboBox1.SelectedItem.ToString();
获取多选字符串:
foreach (var item in comboBox1.SelectedItems)
{
string selectedString = item.ToString();
// 使用 selectedString 进行操作
}
注意:
SelectedItem属性返回单个选中的项目,如果 ComboBox 允许多选,则SelectedItem将返回第一个选中的项目。SelectedItems属性返回所有选中的项目的集合。- 确保 ComboBox 的
DropDownStyle属性设置为DropDown或DropDownList,以允许用户选择项目。
原文地址: https://www.cveoy.top/t/topic/mYr2 著作权归作者所有。请勿转载和采集!