好的,最后一次尝试 :) 我有一个用户必须填写的小表格(姓名、日期等),当他单击“发送”时,我希望他的姓名与日期一起(用户以 DateTimePicker 格式输入日期)将在 CheckedListBox 中显示为 1 项 (example="gil 17/12/2011") 有可能吗?
问问题
873 次
2 回答
2
你当然可以。这里的诀窍是要知道 ListBox(和 CheckedListBox)包含一个Object
s 列表。它使用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);
}
}
于 2011-04-17T19:49:49.243 回答
0
没有一些努力,不,这是不可能的,但你可以自己实现。看看Editable ListView和In-place editing of ListView subitems。
于 2011-04-17T19:26:39.753 回答