【问题标题】:Select object from listbox and show related object to another listbox从列表框中选择对象并将相关对象显示到另一个列表框
【发布时间】:2017-06-27 18:49:55
【问题描述】:

我有 2 个班级:Doctor 和 Pacient。它们都有一个名为 codeM 的字段。因为医生有一个代码,所以在创建 Pacient 对象时,在 codeM 字段中编写医生的代码时,可以将许多病人分配给该医生。 (我的意思是在创建 Doctor 对象以选择其 codeM 之后) private int codeM Doctor 和 Pacient 类中的字段定义。

和 2 种形式。在 Form1 中,我有一个 Doctor 对象列表,在 Form2 中,我有一个 Pacient 对象列表,我在创建它之后将其转移到 Form1。原因如下:

在 Form1 中,我有一个 listBox,其中显示了 Doctor 对象的列表。名单如下:List<Doctor> listDoctors = new List<Doctor>(); 我从 Form2 传递给 Form1 的 Pacient 对象列表命名为 listPacients

我有一个辅助列表框。我希望当我从 listBox1 中选择一个 Doctor 对象时,将该医生的属性 codeM 与 listPacients 中的每个患者进行比较,并在 listBox2 中向我显示那些 codeM 与医生 codeM 相同的患者。

我什至不知道从哪里开始做这件事,但我有 listBox1 的代码,与医生一起使用

private void listBox1_doctors_SelectedIndexChanged(object sender, EventArgs e)
        {
            Doctor currentItem = listBox1_doctors.SelectedItem as Doctor;

               foreach(Pacient p in listaPacienti2)
        {
            if(currentItem.CodM==p.CodM)
            {
                listBox2_pacienti.DataSource = new ObservableCollection<Pacient>(p);
                listBox2_pacienti.DisplayMember = nameof(Doctor.NumeM);
                listBox2_pacienti.ValueMember = nameof(Doctor.CodM);
                listBox2_pacienti.SelectedIndex = 0;
            }
        }
            //from this point on i'm stuck. Please tell me how to continue, and how to set dataSource to only show me the desired pacients


        }

【问题讨论】:

  • 除非我遗漏了什么,一旦你选择了医生,你需要通过调用 patientListBox.DataSource = listPatients 来填充患者列表框。如果没有更多信息,我们无法帮助您如何为医生获取患者名单。

标签: c# winforms listbox


【解决方案1】:

这似乎并不难,我注释了我的代码,以便您理解它。

private Form2 patientsForm; // You somehow have to get the object of this window, for
                            //   example when showing the window with 
                            //   patientsForm = new Form2().Show();

private void listBox1_doctors_SelectedIndexChanged(object sender, EventArgs e)
{
    Doctor currentItem = listBox1_doctors.SelectedItem as Doctor;

    int docCode = currentItem.codeM;

    // Basically use Linq to select the first Patient in a list of Patients that matches
    //  the codeM of the doctor.
    Patient patient = patientsForm.listPatients.First(p => p.codeM == docCode);
}

编辑:

如果您想获得 多个 个具有匹配代码的患者,只需使用 .Where() 而不是 .First()

Patient[] patients = patientsForm.listPatients.Where(p => p.codeM == docCode).ToArray();

【讨论】:

  • 我写了这 3 行,但它在 listbox2 中一无所获。如果我尝试将我已经拥有的这些行放入其中,它也不会显示任何内容。
  • 这段代码本身没有“显示”任何东西。它只是从列表中检索患者。显示取决于您。还是运行时出现异常?
  • 它告诉我“InvalidArgument:“0”的值对 Selectedindex 无效。参数名称=SelectedIndex” 0 是我分配给医生和 2 名患者的代码之一。这是当我在示例代码中添加数据源等的 4 行时,它没有显示医生的姓名,但他们的代码......
【解决方案2】:

提供的代码Ian H只处理检索部分,如果你想在ListBox上显示患者姓名,那么你可以这样做:

public class Patient
{
    public string Name{ get; set; }
    //Other properties

    public override string ToString()
    {
        return Name;
    }

}

使用它会将患者姓名显示到ListBox

【讨论】:

  • 很高兴为您提供帮助,如果这是您正在寻找的答案,请将其标记为答案,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 2011-04-25
  • 2018-05-11
  • 1970-01-01
  • 2020-04-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多