【发布时间】:2011-02-24 19:51:47
【问题描述】:
我有一个包含下拉组合框的 winform,用户可以在其中输入购买日期。
组合框中的项目是“选择日期”,它会显示一个日历,以便用户可以选择日期、“今天”和“上周”。如果用户选择“今天”或“上周”,我想将下拉控件的文本值设置为该日期字符串。我试图在 SelectedIndexChanged 处理程序中执行此操作,但没有骰子。 DropDown 列表只显示一个空白字段。
有什么想法吗?
private void comboBoxPurchased_SelectedIndexChanged(object sender, EventArgs e)
{
Types.ComboInfo info = (Types.ComboInfo)comboBoxPurchased.SelectedItem;
DateTime newDate = stock.PurchaseDate;
switch ((Types.PurchasedDate)info.id)
{
case Types.PurchasedDate.PickCustom:
//popup a date dialog and let the user choose the date
PopupCalendar p = new PopupCalendar();
if (p.ShowDialog() == DialogResult.OK)
// show date in combobox
newDate = p.Date;
break;
case Types.PurchasedDate.Today:
newDate = DateTime.Now;
break;
case Types.PurchasedDate.WithinLastWeek:
newDate = DateTime.Now.AddDays(-7);
break;
case Types.PurchasedDate.WithinLastMonth:
newDate = DateTime.Now.AddMonths(-1);
break;
}
// re-create combobox items with new purchase date
//PopulatePurchaseDateCombo(newDate);
comboBoxPurchased.SelectedText = date.ToString("MMMM d, yyyy");
comboBoxPurchased.Text = date.ToString("MMMM d, yyyy");
}
【问题讨论】:
-
可以分享 selectedindexchanged 事件中的代码吗?如果我对您的理解正确,您需要将组合框的 DropDownStyle 属性更改为 DropDown,这应该会给您想要在组合框中显示的字符串。
-
原始帖子已编辑以显示代码
-
您是否尝试过更改 ListItem 本身的文本,而不是 ComboBox.SelectedText 属性?
-
我不想更改列表项的文本,因为我总是希望下拉菜单中提供相同的选项。更改所选项目的列表项将有效地删除该选项并将其替换为日期。