我可以向 checkListBox 添加一个名称 +日期 ? (c#)

发布于 2024-11-02 02:47:37 字数 141 浏览 6 评论 0原文

好的,最后一次尝试:) 我有一个小表格,用户必须在单击“发送”时填写(姓名、日期等) 我希望他的名字和日期(用户以 DateTimePicker 格式输入日期)将在 CheckedListBox 中显示为 1 项(示例 =“gil 17/12/2011”) 有可能吗?

ok , last try :)
i have a small form that a user have to fill(name, date, etc), when he click "send"
i want that his name together with the date (the user entring the date in DateTimePicker format)will show up in the CheckedListBox as 1 item (example="gil 17/12/2011")
is it possible at all ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

咆哮 2024-11-09 02:47:37

当然可以。这里的技巧是要知道 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);
    }
}

Sure you can. The trick here is to know that ListBox (and CheckedListBox) contain a list of Objects. It uses the ToString method of those objects to display. All you have to do is populate the list with your own type which has a ToString override.

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);
    }
}
悲凉≈ 2024-11-09 02:47:37

不付出一些努力,不,这是不可能的,但是你可以自己实现。看看 可编辑 ListViewListView 子项的就地编辑

Without some effort, No, it's not possible, but you can implement that by yourself. Have a look at Editable ListView and In-place editing of ListView subitems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文