我有一个包含DropDown组合框的winform,用户可以在其中输入购买日期。C#:在SelectedIndexChange事件处理程序中设置ComboBox文本值?
组合框中的项目是'选择日期',它会显示一个日历,以便用户可以选择一个日期'今天'和'上周'。如果用户选择“今天”或“上周”,我想将下拉控件的文本值设置为该日期字符串。我试图在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,并且应该给你想要在组合框中显示的字符串。 – reggie 2011-02-24 19:56:38
原始文章编辑显示代码 – Curtis 2011-02-24 20:02:10
您是否尝试过更改ListItem本身的文本,而不是ComboBox.SelectedText属性? – 2011-02-24 20:09:36