In this blog we will see how to display a list of previous Years and next Years from current year dynamically into a combobox.


Program


Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

If TextBox1.Text = "" Or TextBox2.Text = "" Then

MsgBox("Please enter the previousYears & afterYears values")

Else

Dim previousYearsCount As Integer

Dim afterYearsCount As Integer

previousYearsCount = TextBox1.Text

afterYearsCount = TextBox2.Text

ComboBox1.Items.Clear()

Dim currentYear As Integer

currentYear = DateAndTime.Now.Year

For addedYear As Integer = currentYear - previousYearsCount To currentYear + afterYearsCount

ComboBox1.Items.Add(addedYear)

Next

TextBox1.Text = ""

TextBox2.Text = ""

End If

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

ComboBox1.Text = "Please Select"

End Sub

End Class


Output


Thanks for reading.