Vb.Net 如何创建月/年下拉列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21425820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:36:41  来源:igfitidea点击:

Vb.Net How to create Months/Years dropdown lists

asp.netvb.net

提问by s.milziadi

Which is the best way to create two dropdown lists one with months (maybe month names) and the other one with years?

哪个是创建两个下拉列表的最佳方法,一个带有月份(可能是月份名称),另一个带有年份?

They should return two values with this format: 1 (or 2, 3 etc.) for months and 2014 (or 2013, 2012 etc.) for years

他们应该使用这种格式返回两个值:1(或 2、3 等)表示月份,2014(或 2013、2012 等)表示年份

Any suggestions?

有什么建议?

回答by ????

Looping will be the best for your solution. Create a for loop for year as follow

循环将是您的最佳解决方案。为年份创建一个 for 循环,如下所示

 for(int i=2000;i<2020;i++)
 {
      ddYear.Items.Add(new ListItem(i.ToString(), i.ToString());
 }

and for month

和一个月

 DateTime month = Convert.ToDateTime("1/1/2000");
 for (int i = 0; i < 12; i++)
 {
      DateTime NextMont = month.AddMonths(i);
      ListItem list = new ListItem();
      list.Text = NextMont.ToString("MMMM");
      list.Value = NextMont.Month.ToString();
      MyddlMonthList.Items.Add(list);
 }

In VB

在VB中

Dim month As DateTime = Convert.ToDateTime("1/1/2000")
  For i As Integer = 0 To 11
    Dim NextMont As DateTime = month.AddMonths(i)
    Dim list As New ListItem()
    list.Text = NextMont.ToString("MMMM")
    list.Value = NextMont.Month.ToString()
    MyddlMonthList.Items.Add(list)
  Next

回答by Karthikeyan P

Other way we can able to do like this,

我们可以通过其他方式做到这一点,

VB Listbox,

VB 列表框,

Dim months = System.Globalization.DateTimeFormatInfo.InvariantInfo.MonthNames
For Each month As String In months
    If Not String.IsNullOrEmpty(month) Then
        ListBox1.Items.Add(month)
    End If
Next

Or Else

要不然

ListBox1.DataSource = System.Globalization.DateTimeFormatInfo.InvariantInfo.MonthNames

VB Combobox,

VB 组合框,

Dim months = System.Globalization.DateTimeFormatInfo.InvariantInfo.MonthNames
For Each month As String In months
    If Not String.IsNullOrEmpty(month) Then
        ComboBox1.Items.Add(month)
    End If
Next

Or Else

要不然

ComboBox1.DataSource = System.Globalization.DateTimeFormatInfo.InvariantInfo.MonthNames

回答by rajesh

protected void Page_Load(object sender, EventArgs e)
{

DropDownList ddltmp = new DropDownList();
int curYear = DateTime.Now.Year;

for(int i = 1; i < 76; ++i)
{

ListItem tmp = new ListItem();
tmp.Value = curYear.ToString();

tmp.Text = curYear.ToString();

ddltmp.Items.Add(tmp);

curYear = DateTime.Now.AddYears(i).Year;
}

this.form1.Controls.Add(ddltmp);
}

回答by Ibrashcka

If you wish to have the current year with the previous years,this is the simplest answer :)

如果您希望将当前年份与前几年一起使用,这是最简单的答案:)

 int curYear = DateTime.Now.Year;

        for (int i = curYear; i >= 2006; i--)
        {
            ddYear.Items.Add(i.ToString());

        }