[英]Disable visibility of selection item in comboBox
我有一个包含组合框的程序:
让设计师喜欢:
this.month_list.AllowDrop = true;
this.month_list.FormattingEnabled = true;
this.month_list.Items.AddRange(new object[] {
"JAN",
"FEB",
"MAR",
"APR",
"MAY",
"JUN",
"JUL",
"AUG",
"SEP",
"OCT",
"NOV",
"DEC"});
this.month_list.Location = new System.Drawing.Point(35, 13);
this.month_list.Name = "month_list";
this.month_list.Size = new System.Drawing.Size(75, 24);
this.month_list.TabIndex = 13;
现在我的问题是,如果月份选项是将来的月份或在当前月份的旁边,是否可以禁用月份选择可见性? 或换句话说,现在正在前进,因此用户将无法单击APR,MAY等。 任何想法请指教。
在年份ComboBox.SelectedIndexChanged
上添加一个处理程序,并根据所选的当前日期和年份重新填充月份列表。
如果year是2014,则不要将大于当前月份的月份添加到您的月份列表中。
编辑:
private void yearCombo_SelectedIndexChanged(object sender, EventArgs e)
{
if(int.Parse(yearCombo.Text) > DateTime.Now.Year)
{
//remove all entries from ComboBox
}
else if (int.Parse(yearCombo.Text) == DateTime.Now.Year)
{
//Just add the months up to current month
}
else
{
//Add all months
}
}
遗憾的是,仅在VS2003-2005中支持combobox中的禁用项。
您只能通过以下代码将其删除(如果您希望从现有的组合框中删除该项目):
// To remove item with index 0:
this.month_list.Items.RemoveAt(0);
// To remove currently selected item:
this.month_list.Items.Remove(this.month_list.SelectedItem);
// To remove "JAN" item:
this.month_list.Items.Remove("JAN");
下面的代码结合了Luc Morin的答案jonathanh8686的想法,您可能会发现这很有用:
首先,您按照jonathanh8686的回答说明制作字典:
Dictionary<int,string> months = new Dictionary<int,string>();
months.add(1, "Jan");
months.add(2, "Feb");
然后,每当用户更改年份组合框时,都可以按以下方式重置月份组合框:
private void yearCombo_SelectedIndexChanged(object sender, EventArgs e)
{
if(int.Parse(yearCombo.Text) > DateTime.Now.Year)
{
//remove all entries from ComboBox
this.month_list.Items.Clear();
}
else if (int.Parse(yearCombo.Text) == DateTime.Now.Year)
{
//Just add the months up to current month
this.month_list.Items.Clear();
int monthnumber = 1;
while(monthnumber <= DateTime.Now.Month)
{
this.month_list.Items.Add(months[monthnumber]);
monthnumber++;
}
}
else
{
//Add all months
this.month_list.Items.Clear();
int monthnumber = 1;
while(monthnumber <= 12)
{
this.month_list.Items.Add(months[monthnumber]);
monthnumber++;
}
}
}
我是C#的新手,因此我不是回答此问题的最佳人选,但我对如何执行此操作有一个想法,但不确定确切的代码,但可以使用以下想法:
制作包含月份的日期时间
int month = 1;
int monthnumber = 1;
Datetime dt = new DateTime();
然后,您可以制作一个包含月数键和月数键的字典,例如:
Dictionary<int,string> months = new Dictionary<int,string>();
months.add(1, "Jan");
months.add(2, "Feb");
依此类推...在此之后,请检查月份并查看当前月份,以便查看其3月
if(dt.Month = March)
{
month = 3;
}
因此,在那之后添加少于3的所有月份,如下所示:
while(monthnumber < months)
{
this.month_list.Items.Add(dates.Value);
monthnumber++;
}
您将必须编写自定义代码来自己绘制组合框的项目。
将这些事件处理程序也添加到月份列表中:
this.month_list.DrawMode = DrawMode.OwnerDrawFixed;
this.month_list.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.comboBox1_DrawItem);
this.month_list.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
Font font = new Font("Aerial", 10, FontStyle.Regular);
bool DisableIndex(int index)
{
return index > DateTime.Now.Month - 1;
}
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
Brush brushToDrawWith = DisableIndex(e.Index) ? Brushes.LightSlateGray : Brushes.Black;
e.Graphics.DrawString(this.month_list.Items[e.Index].ToString(), font, brushToDrawWith, e.Bounds);
}
void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DisableIndex(month_list.SelectedIndex))
{
month_list.SelectedIndex = -1;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.