我可以在checkedListBox中添加名称和日期吗? (c#)

| 好的,最后尝试:) 我有一个小表格,用户单击“发送”时必须填写(姓名,日期等) 我希望他的名字和日期(以DateTimePicker格式输入日期的用户)将在CheckedListBox中显示为1个项目(example = \“ gil 17/12/2011 \”) 有可能吗?     
已邀请:
你当然可以。这里的诀窍是要知道ListBox(和CheckedListBox)包含一个
Object
的列表。它使用那些对象的“ 1”方法显示。您所要做的就是使用您自己的类型填充列表,该类型具有
ToString
覆盖。
using System;
using System.Drawing;
using System.Windows.Forms;

public class Form1 : Form
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

    public Form1()
    {
        Controls.Add(new Label { Text = \"Name\", Location = new Point(10, 10), AutoSize = true });
        Controls.Add(new TextBox { Name = \"Name\", Location = new Point(60, 10) });
        Controls.Add(new Label { Text = \"Date\", Location = new Point(10, 40), AutoSize = true });
        Controls.Add(new DateTimePicker { Name = \"Date\", Location = new Point(60, 40) });
        Controls.Add(new Button { Name = \"Submit\", Text = \"Submit\", Location = new Point(10, 70) });
        Controls.Add(new CheckedListBox { Name = \"List\", Location = new Point(10, 100), Size = new Size(ClientSize.Width - 20, ClientSize.Height - 100 - 10) });
        Controls[\"Submit\"].Click += (s, e) =>
                (Controls[\"List\"] as CheckedListBox).Items.Add(new MyItem { Name = Controls[\"Name\"].Text, Date = (Controls[\"Date\"] as DateTimePicker).Value });
    }
}

public class MyItem
{
    public string Name { get; set; }
    public DateTime Date { get; set; }

    public override string ToString()
    {
        return String.Format(\"{0} {1}\", Name, Date);
    }
}
    
没有任何努力,这是不可能的,但是您可以自己实现。看一下可编辑的ListView和ListView子项的就地编辑。     

要回复问题请先登录注册