首页 新闻 会员 周边

.NET C/S COMBOX控件

0
悬赏园豆:10 [已解决问题] 解决于 2012-07-11 10:31

COMBOX列表控件中包含从当前年份前10年到当前年份的下一年

求犀利思路,有代码更好  跪求!!!

各位大大求给力,在线等!

失落の熊熊的主页 失落の熊熊 | 初学一级 | 园豆:79
提问于:2012-06-28 16:02
< >
分享
最佳答案
0

搞一个List<ListItem>  itemYears 

然后:

for (int i = DateTime.Now.Year - 10; i <= DateTime.Now.Year + 1;i++ )
{
itemYears.Item.Add(i+"年");

}

接下来,看你的了...................

收获园豆:8
Angkor--:-- | 小虾三级 |园豆:1086 | 2012-06-28 16:42
其他回答(2)
0

写一个数据源就好了。

比如写一个类:

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。

收获园豆:2
无之无 | 园豆:5095 (大侠五级) | 2012-06-28 16:22
0

//月份: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);
            }           
        }

 

自己解决了,唔。。  多谢有人回复。

失落の熊熊 | 园豆:79 (初学一级) | 2012-06-28 16:43

多动脑,多动手,伙计!加油!

支持(0) 反对(0) Angkor--:-- | 园豆:1086 (小虾三级) | 2012-06-28 16:50

修正下我给你的代码(我理解成12年了),同时增加个构造函数,把日期传递进去,你的代码就可以修改为:

        private void cbYear_DropDown(object sender, EventArgs e)
        {
            cbYear.DataSource=new YearsList();           
        }
支持(1) 反对(0) 无之无 | 园豆:5095 (大侠五级) | 2012-06-28 17:18
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册