【问题标题】:Correctly displaying list items in a combobox and reacting to an item selection在组合框中正确显示列表项并对项目选择做出反应
【发布时间】:2015-07-26 13:07:15
【问题描述】:

我创建了一个我的自定义类对象的列表“employeeList”。 该类称为“Employee”:

public class Employee
{
    public string firstName;
    public string lastName;
    public string region;
    public Position position;
    public List<double> goals;
    public List<double> tests;
    public double correlation;
    public Employee supervisor;
    ....
}

这里我将列表传递给 Form.cs 并将我的列表绑定到组合框

var employeeList = correlation.ComputeCorrelation();
var bSource = new BindingSource();
bSource.DataSource = employeeList;
comboBox1.DataSource = bSource.DataSource;
Employee f = (Employee)comboBox1.SelectedItem;

老实说,我还没有完全弄清楚整个绑定是如何工作的,但这不是问题。问题是,一旦绑定完成,所有项目都会以它们的默认名称出现在组合框中,例如 Correlation_new.Employee。我想要做的是通过 Employee 类中的字段“名称”显示项目。我怎么做?

另外,我只是不知道如何处理在组合框中选择项目的事件。我需要获取该项目的内容。

【问题讨论】:

  • 您确实需要阅读教程。你在这里问第一章的问题。您的 Employee 类正在使用字段,您可能希望这些是属性。是的,当 ComboBox 的选定项目发生更改时,您可以使用 SelectedIndexChanged 事件或类似的事件。您也可以重写Employee类的ToString函数来修复ComboBox中显示的内容。
  • @LarsTech 好吧,SelectedIndexChanged 不起作用。 private void combobox1__TextChanged(object sender, EventArgs e) { }
  • “不起作用”并没有真正描述问题。你写的看起来像 TextChanged 事件。您也必须手动或通过设计器连接正确的事件。
  • 我也使用了那个代码。事件没有发生,我不知道为什么。一切都写得正确 private void combobox1_SelectedIndexChanged(object sender, EventArgs e) { Employee f = (Employee)comboBox1.SelectedItem; listBox1.Text = f.firstName; }
  • 你把事件联系起来了吗?在设计器属性窗口中,选择闪电、双击事件等。

标签: c# winforms list data-binding combobox


【解决方案1】:

你的 Employee.cs 类应该写成如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace YourProjectName
{
public class Employee
{
    public string firstName;
    public string lastName;
    public string region;
    public Position position;
    public List<double> goals;
    public List<double> tests;
    public double correlation;
    public Employee supervisor;
    public String GetFirstName() { return firstName; }
    public String GetLastName() { return lastName; }
    public String GetRegion() { return region; }
    /*You can increase get method to connect your variables.*/
}

 */*Above class like .net mvc model classes.*/*

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;

    namespace YourProjectName
    {
    public partial class Form 
    {
        private List<Employee> employees{ get; set; }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtTitleBox.Text = comboBox1.SelectedItem.ToString();
        }

        public List<Employee> LoadCombobox()
        {
                List<Employee> employees= new List<Employee>(); 
                var employeeList = correlation.ComputeCorrelation();
                var bSource = new BindingSource();
                bSource.DataSource = employeeList;
                employees.Add(bSource.DataSource);
                return employees;
        }

        private void LoadYourCombobox_1(object sender, EventArgs e)
        {
          employees= LoadCombobox();
          foreach (Employee employee in employees)
            {
                comboBox1.Items.Add(employees.GetFirstName());
            }
        }
    }
    }

当用户选择项目时,您可以使用以下代码块:

Employees employee = employees.Where(f => f.GetFirstName().Equals(comboBox1.SelectedValue)).FistOrDefault();

if (employee!= null)
{
    //do work
}

或者你可以使用下面的代码块:

if you want the firstName: comboBox1.SelectedValue

if you want the index: comboBox1.SelectedIndex

希望代码对你有所帮助。

【讨论】:

  • 令人困惑的帖子。为什么 Employee 类在构造函数中需要一个 IList 参数?为什么“表单”继承自 Employee 类?为什么只将员工的名字放在组合列表中,而不是整个员工对象?
  • 我只放了名字,因为他想放弃名字。 LoadCombobox() 方法有点复杂,你说得对。我会尝试编辑。
猜你喜欢
  • 2020-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-30
  • 2016-10-02
  • 2013-09-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多