HOWTO · Csharp

如何在 C# 中获取 ComboBox 的选定值

学习如何在 C# 中从 Windows Forms ComboBox 读取选定项、显示文本或绑定值。

本页内容

在 Windows Forms ComboBox 中,“选定值"可以指三件不同的事:选定项对象、为该项显示的文本,或者其数据绑定值。当你需要对象时读取 ComboBox.SelectedItem,当你需要其显示文本时使用 ComboBox.GetItemText,当控件已绑定且你需要由 ValueMember 配置的字段时读取 SelectedValue。这些示例假设控件已经位于窗体上,并展示了从按钮单击处理程序中读取选择的过程。

下面的示例面向 Windows Forms 应用程序。本文未提供运行时执行证据,因此它们以未执行的、Windows 专用示例的形式呈现;不声称任何控制台输出。

使用 ComboBox.SelectedItem 属性在 C# 中获取 ComboBox 的选定值

ComboBox.SelectedItem 是一个属性,用于获取或设置列表中当前选定的对象。它并不总是返回字符串:当项是自定义对象时,结果就是该对象。仅当应用程序需要的是该项的字符串表示时,才将其转换为文本。以下完整示例创建了一个窗体,选择了一个项,并在单击按钮时读取当前对象。

using System;
using System.Windows.Forms;

internal static class Program
{
    [STAThread]
    private static void Main()
    {
        ApplicationConfiguration.Initialize();
        var form = new Form();
        var comboBox = new ComboBox { Left = 12, Top = 12, Width = 180 };
        comboBox.Items.AddRange(new object[] { "Red", "Green", "Blue" });
        comboBox.SelectedItem = "Green";
        var button = new Button { Left = 12, Top = 48, Text = "Read selection" };
        button.Click += (sender, args) =>
        {
            object selectedItem = comboBox.SelectedItem;
            string selectedText = Convert.ToString(selectedItem) ?? "";
            MessageBox.Show($"Item: {selectedText}");
        };
        form.Controls.Add(comboBox);
        form.Controls.Add(button);
        Application.Run(form);
    }
}

在现有窗体中,同样的操作可以放在它的单击处理程序中:读取 comboBox1.SelectedItem,然后根据项的实际类型进行处理。如果列表包含字符串,则选定项就是字符串。如果它包含对象,则在使用其属性之前,对返回的对象进行转换或模式匹配。选择也可能不存在,因此在选定某个项之前运行的代码应考虑到 SelectedItem 可能为 null。

使用 ComboBox.GetItemText 在 C# 中获取 ComboBox 的选定值

ComboBox.GetItemText(item) 是一个方法,返回控件用于显示所提供项的文本。用 SelectedItem 调用它即可获得当前选择的显示文本。当项是对象且 ComboBox 使用 DisplayMember 时,这很有用;对对象调用 ToString() 可能会产生不同的文本。GetItemText 回答的是显示问题,而 SelectedValue 回答的是数据绑定问题。

对于已绑定的 ComboBox,将 DisplayMember 设置为向用户显示的属性,将 ValueMember 设置为标识该项的属性。然后在应用程序需要该绑定值时检索 comboBox1.SelectedValue。该示例同时展示了这两个结果,以便你选择代码需要的那一个:

using System;
using System.Collections.Generic;
using System.Windows.Forms;

internal sealed class City
{
    public int Id { get; set; }
    public string Name { get; set; } = "";
}

internal static class Program
{
    [STAThread]
    private static void Main()
    {
        ApplicationConfiguration.Initialize();
        var form = new Form();
        var comboBox = new ComboBox { Left = 12, Top = 12, Width = 180 };
        comboBox.DisplayMember = nameof(City.Name);
        comboBox.ValueMember = nameof(City.Id);
        comboBox.DataSource = new List<City>
        {
            new City { Id = 1, Name = "Paris" },
            new City { Id = 2, Name = "Tokyo" }
        };
        comboBox.SelectedValue = 2;
        var button = new Button { Left = 12, Top = 48, Text = "Read selection" };
        button.Click += (sender, args) =>
        {
            string displayedText = comboBox.GetItemText(comboBox.SelectedItem);
            object selectedValue = comboBox.SelectedValue;
            MessageBox.Show($"Text: {displayedText}; value: {selectedValue}");
        };
        form.Controls.Add(comboBox);
        form.Controls.Add(button);
        Application.Run(form);
    }
}

在这里,GetItemText 读取显示文本(例如 Tokyo),而 SelectedValue 读取绑定的 Id(例如 2)。请使用在控件上配置的值成员,而不是将显示文本视为标识符。这两个示例都需要 Windows Forms 项目,并且未在所提供的运行时环境中执行。