首先定义一个类:
public class ListItem { private string m_Display; private long m_Value; public ListItem(string Display, long Value) { m_Display = Display; m_Value = Value; } public string Display { get { return m_Display; } } public long Value { get { return m_Value; } } }
再调用这个类添加:
for (int i = 1; i < 5; i++) { int year=DateTime.Now.Year-i; comboBox1.Items.Add(new ListItem(year.ToString() + "年", year)); comboBox1.DisplayMember = "Display"; comboBox1.ValueMember = "Value"; } comboBox1.Text = (DateTime.Now.Year - 1).ToString()+"年";效果如下:
- 本文标题: winform如何动态添加comboBox1的选项
- 文章分类:【WinForm/WPF】
- 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.