Hashey100
Hashey100

Reputation: 984

MonthCalendar control - need help using if statement

Basically I've imported a basic calendar into my C# form using the toolbox. I applied an onclick method, so when the user clicks on any date it will read it into a label. So far so good, but my aim is to use a if statement to read the text into the label and close the form if its a certain date. Below is what I tried.

if(label.Text == "14th-April-2012")
{
    this.hide();
}

private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
{
    monthCalendar1.MaxSelectionCount = 1;
    label9.Text = monthCalendar1.SelectionRange.End.ToString("dd-MMMM-yyyy", new System.Globalization.DateTimeFormatInfo());
}

Upvotes: 0

Views: 272

Answers (1)

Brad Rem
Brad Rem

Reputation: 6026

Use this.Close() to close your Form.

Also, if you know for sure you will have a date listed in your label, it would be better to compare using type DateTime. For example,

var date = DateTime.ParseDate(label.Text);
if(date == new DateTime(2012, 4, 14))
{
     this.Close();
}

Upvotes: 5

Related Questions