Check the expiry date entered

Hello,

I have two comboboxes for MM and YY . I have already coded it so that the years dropdown as for next 20 years .Any suggestions on how to check  ** Must not expire today or in the past.



 Public Sub LoadMonthsandYear()
        Dim i As Integer
        Dim myvar As String = String.Empty
        i = 1 'initialise counter to 1
        Do While i <= 12
            myvar = MonthName(i, True)
            Me.CmbMonth.Items.Add(myvar)
            i = i + 1
        Loop
        'Add next 20 YEARS to expiration year combobox
        'Must not be more than 20 years in the future
        i = 0 'initialise counter to 0
        Do While i <= 20
            Me.CmbYear.Items.Add(DateTime.Now.Year + i) 'Add years to combobox
            i = i + 1
        Loop
    End Sub

Open in new window


Cheers
RIASAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RIASAuthor Commented:
Need textbox for expiry date,comboboxes was a workaround

Cheers
0
ChloesDadCommented:
Why not use a dateTimepicker rather than combo boxes, you can then limit the dates available so that dates earlier than tomorrow are not selectable

see

https://msdn.microsoft.com/en-us/library/aa231249(v=vs.60).aspx

for details about the dateTimePicker control
0
RIASAuthor Commented:
Hello ChloesDad,
Thanks for the advise but the requirement is to use textboxes for MM/YY .

Cheers
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

it_saigeDeveloperCommented:
Try playing around with these:

Form1.vb -
Imports System.Globalization

Public Class Form1
	Private months As New List(Of DateTime)
	Private years As New List(Of DateTime)
	Private [now] As DateTime = DateTime.Now

	Private Sub OnLoad(sender As Object, e As EventArgs) Handles MyBase.Load
		Dim seed As DateTime = New DateTime([now].Year, 1, 1)
		For i As Integer = 0 To 11 Step 1
			months.Add(seed.AddMonths(i))
		Next

		For i As Integer = 0 To 19 Step 1
			years.Add(seed.AddYears(i))
		Next

		cmbMonth.DataSource = months
		cmbYear.DataSource = years
	End Sub

	Private Sub OnValidating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles tb2M2Y.Validating, tb2M4Y.Validating, DateTimePicker1.Validating, cmbYear.Validating, cmbMonth.Validating
		Dim [selected] As DateTime = DateTime.MinValue
		If sender.GetType().Equals(GetType(MaskedTextBox)) Then
			Dim tb As MaskedTextBox = DirectCast(sender, MaskedTextBox)
			If tb.Equals(tb2M2Y) Then
				DateTime.TryParseExact(tb.Text, "MM/yy", CultureInfo.CurrentCulture, Globalization.DateTimeStyles.AssumeLocal, [selected])
			ElseIf tb.Equals(tb2M4Y) Then
				DateTime.TryParseExact(tb.Text, "MM/yyyy", CultureInfo.CurrentCulture, Globalization.DateTimeStyles.AssumeLocal, [selected])
			End If
		ElseIf sender.GetType().Equals(GetType(DateTimePicker)) Then
			Dim tp As DateTimePicker = DirectCast(sender, DateTimePicker)
			[selected] = tp.Value
		End If

		If [selected] <> DateTime.MinValue Then
			If [selected].Year >= [now].Year AndAlso [selected].Year <= years(years.Count() - 1).Year Then
				If [selected].Year = [now].Year AndAlso [selected].Month >= [now].Month Then
					MessageBox.Show("Selected year and month are good")
				ElseIf [selected].Year > [now].Year Then
					MessageBox.Show("Selected year and month are good")
				Else
					MessageBox.Show("Selected year and month are not good")
				End If
			Else
				MessageBox.Show("Selected year and month are not good")
			End If
		Else
			MessageBox.Show("Selected year and month are not good")
		End If
	End Sub

	Private Sub OnFormat(sender As Object, e As ListControlConvertEventArgs) Handles cmbYear.Format, cmbMonth.Format
		If sender.GetType().Equals(GetType(ComboBox)) Then
			Dim cmb As ComboBox = DirectCast(sender, ComboBox)
			If cmb.Equals(cmbMonth) Then
				e.Value = CType(e.ListItem, DateTime).ToString("MM")
			ElseIf cmb.Equals(cmbYear) Then
				e.Value = CType(e.ListItem, DateTime).ToString("yy")
			End If
		End If
	End Sub
End Class

Open in new window

Form1.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
		Me.DateTimePicker1 = New System.Windows.Forms.DateTimePicker()
		Me.cmbMonth = New System.Windows.Forms.ComboBox()
		Me.cmbYear = New System.Windows.Forms.ComboBox()
		Me.tb2M4Y = New System.Windows.Forms.MaskedTextBox()
		Me.tb2M2Y = New System.Windows.Forms.MaskedTextBox()
		Me.SuspendLayout()
		'
		'DateTimePicker1
		'
		Me.DateTimePicker1.Location = New System.Drawing.Point(13, 13)
		Me.DateTimePicker1.Name = "DateTimePicker1"
		Me.DateTimePicker1.Size = New System.Drawing.Size(259, 20)
		Me.DateTimePicker1.TabIndex = 0
		'
		'cmbMonth
		'
		Me.cmbMonth.FormattingEnabled = True
		Me.cmbMonth.Location = New System.Drawing.Point(186, 66)
		Me.cmbMonth.Name = "cmbMonth"
		Me.cmbMonth.Size = New System.Drawing.Size(40, 21)
		Me.cmbMonth.TabIndex = 1
		'
		'cmbYear
		'
		Me.cmbYear.FormattingEnabled = True
		Me.cmbYear.Location = New System.Drawing.Point(232, 66)
		Me.cmbYear.Name = "cmbYear"
		Me.cmbYear.Size = New System.Drawing.Size(40, 21)
		Me.cmbYear.TabIndex = 2
		'
		'tb2M4Y
		'
		Me.tb2M4Y.Location = New System.Drawing.Point(150, 40)
		Me.tb2M4Y.Mask = "00/0000"
		Me.tb2M4Y.Name = "tb2M4Y"
		Me.tb2M4Y.Size = New System.Drawing.Size(122, 20)
		Me.tb2M4Y.TabIndex = 3
		'
		'tb2M2Y
		'
		Me.tb2M2Y.Location = New System.Drawing.Point(13, 40)
		Me.tb2M2Y.Mask = "00/00"
		Me.tb2M2Y.Name = "tb2M2Y"
		Me.tb2M2Y.Size = New System.Drawing.Size(121, 20)
		Me.tb2M2Y.TabIndex = 4
		'
		'Form1
		'
		Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
		Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		Me.ClientSize = New System.Drawing.Size(284, 100)
		Me.Controls.Add(Me.tb2M2Y)
		Me.Controls.Add(Me.tb2M4Y)
		Me.Controls.Add(Me.cmbYear)
		Me.Controls.Add(Me.cmbMonth)
		Me.Controls.Add(Me.DateTimePicker1)
		Me.Name = "Form1"
		Me.Text = "Form1"
		Me.ResumeLayout(False)
		Me.PerformLayout()

	End Sub
	Friend WithEvents DateTimePicker1 As System.Windows.Forms.DateTimePicker
	Friend WithEvents cmbMonth As System.Windows.Forms.ComboBox
	Friend WithEvents cmbYear As System.Windows.Forms.ComboBox
	Friend WithEvents tb2M4Y As System.Windows.Forms.MaskedTextBox
	Friend WithEvents tb2M2Y As System.Windows.Forms.MaskedTextBox

End Class

Open in new window

Which produces the following output -I have two masked text boxes here, one will accept month and year in two digit format.  The other month in two digit and year in four digit.Choosing a valid date.Choosing and invalid date.-saige-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
it_saigeDeveloperCommented:
You could do the same with just a single text box, you just want to parse the date and ensure that you accept multiple formats.

-saige-
0
RIASAuthor Commented:
Cheers mate will give a try and get back!!
0
RIASAuthor Commented:
Saige,
Thanks a million mate!!!worked like charm!!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.