繁体   English   中英

C#显示清单 <class> ListBox中的值

[英]C# Display List<class> values in ListBox

我将“ Procs”类添加为列表项:

public static List<Procs> allprocs = new List<Procs>();

因此,当用户填写表格并单击“添加过程”时:

private void btAddProcedure_Click(object sender, EventArgs e)
{
    ClaimVars.allprocs.Add(new Procs
    {

        TreatmentBusinessID = Convert.ToInt32(proLocID.Text),
        DiagCodeID = ClaimVars.DiagID,
        CPTCodeID = ClaimVars.CPTID,
        Charges = Convert.ToDouble(txtCharges.Text),
        AmountPaid = Convert.ToDouble(txtPaid.Text),
        DateServicedFrom = Convert.ToDateTime(dateTimePicker1.Text),
        DateServicedToo = Convert.ToDateTime(dateTimePicker2.Text),
        Notes = notesDiagServ.Text
    });

    // Change the DataSource.
    listBox1.DataSource = null;
    listBox1.DataSource = ClaimVars.allprocs;
    }
}

他们将看到一个新的列表项。

如何显示此类的值而不是添加对象的名称?

添加过程后,这是我的列表框的外观:

Project_LB223.ClaimForms.Procs
Project_LB223.ClaimForms.Procs
Project_LB223.ClaimForms.Procs
Project_LB223.ClaimForms.Procs

这是我希望的样子:

1     32     44     33.44     22.17     12/17/09 11:21:52     12/22/09 10:31:64     patient was feeling sick.
2     42     72     12.45     10.67     12/18/09 22:51:22     12/23/09 11:21:25     patient was showing signs of fatigue and shortness of breath.
1     68     57     83.64     55.47     12/19/09 23:25:45     12/24/09 15:38:42     the patient is feeling better.
5     12     22     37.44     23.45     12/22/09 16:81:11     12/25/09 19:35:22     the patient does not have any more symptoms.

通过调用ToString()显示数据。 您可以在类中重写此方法以显示所需的数据。

例如,您可以添加ToString()方法重写,如下所示:

public class Procs
{
    public override string ToString()
    {
       string spaceinbetween = new string(' ', 5);
       return TreatmentBusinessID + spaceinbetween + DiagCodeID + spaceinbetween ...
    }
}

希望这可以帮助。

您可以使用以下任一方法来实现列布局

  • 在Procs类上重写ToString()
  • 使用列表框的Format事件。

在这两种情况下,您都需要设置一个固定宽度的字体,因此使用DataGrid或ListView可能更好。

ListBox无法显示多个列,请改用DataGridView

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM