搞一个List<ListItem> itemYears
然后:
for (int i = DateTime.Now.Year - 10; i <= DateTime.Now.Year + 1;i++ )
{
itemYears.Item.Add(i+"年");
}
接下来,看你的了...................
写一个数据源就好了。
比如写一个类:
public class YearsList : System.Collections.IList { private System.Collections.IList _years = new System.Collections.Generic.List<string>(); public YearsList() { } private void Init() { for (int i = -10; i <= 1; i++) { _years.Add(DateTime.Now.AddYears(i).ToString("yyyy年")); } } #region IList int System.Collections.IList.Add(object value) { throw new NotSupportedException(); } void System.Collections.IList.Clear() { throw new NotSupportedException(); } bool System.Collections.IList.Contains(object value) { return this._years.Contains(value); } int System.Collections.IList.IndexOf(object value) { return this._years.IndexOf(value); } void System.Collections.IList.Insert(int index, object value) { throw new NotSupportedException(); } bool System.Collections.IList.IsFixedSize { get { return this._years.IsFixedSize; } } bool System.Collections.IList.IsReadOnly { get { return this._years.IsReadOnly; } } void System.Collections.IList.Remove(object value) { throw new NotSupportedException(); } void System.Collections.IList.RemoveAt(int index) { throw new NotSupportedException(); } object System.Collections.IList.this[int index] { get { return this._years[index]; } set { throw new NotSupportedException(); } } void System.Collections.ICollection.CopyTo(Array array, int index) { this._years.CopyTo(array, index); } int System.Collections.ICollection.Count { get { return this._years.Count; } } bool System.Collections.ICollection.IsSynchronized { get { return this._years.IsSynchronized; } } object System.Collections.ICollection.SyncRoot { get { return this._years.SyncRoot; } } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this._years.GetEnumerator(); } #endregion }
然后new这个类作为Combox的DataSource。
//月份:1-12
private void cbMonth_DropDown(object sender, EventArgs e)
{
for (int i = 1; i <= 12; i++)
{
cbMonth.Items.Add(i);
}
}
//年份:目前年份前后10年
private void cbYear_DropDown(object sender, EventArgs e)
{
int i = Convert.ToInt32(DateTime.Now.Year);
for (int j = i - 10; j <= i + 10; j++)
{
cbYear.Items.Add(j);
}
}
自己解决了,唔。。 多谢有人回复。
多动脑,多动手,伙计!加油!
修正下我给你的代码(我理解成12年了),同时增加个构造函数,把日期传递进去,你的代码就可以修改为:
private void cbYear_DropDown(object sender, EventArgs e) { cbYear.DataSource=new YearsList(); }