我可以添加到checkedListBox的名称+日期?(c#)

本文关键字:日期 添加 checkedListBox 我可以 | 更新日期: 2023-09-27 17:50:20

ok,最后一次尝试:)我有一个小表单,用户必须填写(姓名,日期等),当他点击"发送"我希望他的名字与日期(用户输入日期在DateTimePicker格式)将显示在CheckedListBox作为1项(示例="gil 17/12/2011")

我可以添加到checkedListBox的名称+日期?(c#)

当然可以。这里的技巧是要知道ListBox(和CheckedListBox)包含一个Object列表。它使用这些对象的ToString方法来显示。你所要做的就是用你自己的类型填充列表,它有一个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);
    }
}

没有一些努力,不,这是不可能的,但你可以自己实现。看看可编辑列表视图和就地编辑列表视图子项。