在c# WinApp中,我如何同时添加文本和值到我的组合框的项目? 我做了一个搜索,通常答案是使用“绑定到一个源”..但在我的情况下,我没有一个绑定源准备在我的程序… 我怎么做这样的事情:

combo1.Item[1] = "DisplayText";
combo1.Item[1].Value = "useful Value"

当前回答

除了Adam Markowitz的回答之外,这里还有一种通用的方法(相对来说)简单地将组合框的ItemSource值设置为枚举,同时向用户显示“Description”属性。(您可能认为每个人都想这样做,这样它就会成为。net的一行程序,但事实并非如此,而且这是我发现的最优雅的方式)。

首先,创建这个简单的类,用于将任意Enum值转换为ComboBox项:

public class ComboEnumItem {
    public string Text { get; set; }
    public object Value { get; set; }

    public ComboEnumItem(Enum originalEnum)
    {
        this.Value = originalEnum;
        this.Text = this.ToString();
    }

    public string ToString()
    {
        FieldInfo field = Value.GetType().GetField(Value.ToString());
        DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
        return attribute == null ? Value.ToString() : attribute.Description;
    }
}

其次,在OnLoad事件处理程序中,您需要将组合框的源设置为基于枚举类型中的每个Enum的ComboEnumItems列表。这可以用Linq实现。然后设置DisplayMemberPath:

    void OnLoad(object sender, RoutedEventArgs e)
    {
        comboBoxUserReadable.ItemsSource = Enum.GetValues(typeof(EMyEnum))
                        .Cast<EMyEnum>()
                        .Select(v => new ComboEnumItem(v))
                        .ToList();

        comboBoxUserReadable.DisplayMemberPath = "Text";
        comboBoxUserReadable.SelectedValuePath= "Value";
    }

现在用户将从用户友好的描述列表中进行选择,但他们选择的将是您可以在代码中使用的枚举值。 要在代码中访问用户的选择,comboBoxUserReadable。SelectedItem将是ComboEnumItem和comboBoxUserReadable。SelectedValue将是EMyEnum。

其他回答

using (SqlConnection con = new SqlConnection(insertClass.dbPath))
{
    con.Open();
    using (SqlDataAdapter sda = new SqlDataAdapter(
    "SELECT CategoryID, Category FROM Category WHERE Status='Active' ", con))
    {
        //Fill the DataTable with records from Table.
        DataTable dt = new DataTable();
        sda.Fill(dt);
        //Insert the Default Item to DataTable.
        DataRow row = dt.NewRow();
        row[0] = 0;
        row[1] = "(Selecione)";
        dt.Rows.InsertAt(row, 0);
        //Assign DataTable as DataSource.
        cboProductTypeName.DataSource = dt;
        cboProductTypeName.DisplayMember = "Category";
        cboProductTypeName.ValueMember = "CategoryID";
    }
}             
con.Close();

我有同样的问题,我所做的是添加一个新的ComboBox,只有在相同的索引值,然后第一个,然后当我改变主要组合的索引在第二个改变的同时,然后我取第二个组合的值,并使用它。

这是代码:

public Form1()
{
    eventos = cliente.GetEventsTypes(usuario);

    foreach (EventNo no in eventos)
    {
        cboEventos.Items.Add(no.eventno.ToString() + "--" +no.description.ToString());
        cboEventos2.Items.Add(no.eventno.ToString());
    }
}

private void lista_SelectedIndexChanged(object sender, EventArgs e)
{
    lista2.Items.Add(lista.SelectedItem.ToString());
}

private void cboEventos_SelectedIndexChanged(object sender, EventArgs e)
{
    cboEventos2.SelectedIndex = cboEventos.SelectedIndex;
}

这是我想到的方法之一:

combo1.Items。添加(new ListItem("Text", "Value"))

要更改一个项目的文本或值,你可以这样做:

combo1.Items[0].Text = 'new Text';

combo1.Items[0].Value = 'new Value';

在Windows窗体中没有名为ListItem的类。它只存在于ASP中。因此,在使用它之前,您需要编写自己的类,就像@Adam Markowitz在他的回答中所做的一样。

还可以查看这些页面,它们可能会有帮助:

如何将项目添加到组合框 如何:从Windows窗体组合框、列表框或checkkedlistbox控件中添加和删除项

这是一个非常简单的解决方案为windows窗体,如果所有需要的是最终值(字符串)。项目的名称将显示在组合框上,选择的值可以很容易地进行比较。

List<string> items = new List<string>();

// populate list with test strings
for (int i = 0; i < 100; i++)
            items.Add(i.ToString());

// set data source
testComboBox.DataSource = items;

并在事件处理程序上获取所选值的值(字符串)

string test = testComboBox.SelectedValue.ToString();

你可以像这样使用匿名类:

comboBox.DisplayMember = "Text";
comboBox.ValueMember = "Value";

comboBox.Items.Add(new { Text = "report A", Value = "reportA" });
comboBox.Items.Add(new { Text = "report B", Value = "reportB" });
comboBox.Items.Add(new { Text = "report C", Value = "reportC" });
comboBox.Items.Add(new { Text = "report D", Value = "reportD" });
comboBox.Items.Add(new { Text = "report E", Value = "reportE" });

更新:虽然上面的代码将正确显示在组合框中,你将不能使用组合框的SelectedValue或SelectedText属性。为了能够使用这些,绑定组合框如下:

comboBox.DisplayMember = "Text";
comboBox.ValueMember = "Value";

var items = new[] { 
    new { Text = "report A", Value = "reportA" }, 
    new { Text = "report B", Value = "reportB" }, 
    new { Text = "report C", Value = "reportC" },
    new { Text = "report D", Value = "reportD" },
    new { Text = "report E", Value = "reportE" }
};

comboBox.DataSource = items;