Question

Hello I trying to make a function wich will shof a calender at a givven location on a given form and the returns the selected date in a string.

This is what i've got so far:

public static string ShowCalendar(Point locatieCalender, Form F1)
    {
        MonthCalendar calender = new MonthCalendar();
        calender.Location = locatieCalender;
        calender.Show();
        calender.Visible = true;
        calender.BringToFront();
        calender.Parent = F1;
        string date = calender.SelectionRange.Start.ToShortDateString();
        DateTime dateValue = DateTime.Parse(date);
        string dateForTextbox = dateValue.ToString("dd-MM-yyyy");

        //calender.Hide();
        return dateForTextbox;

    }

And the function call looks like this:

Point calenderLocatie = new Point(405, 69);
        string dateForTextbox = HelpFunction.ShowCalendar(calenderLocatie, this);
        txtPeriode_Tot.Text = dateForTextbox;

The calender shows on the form but no string is returned. I've tried a event handler but because of the static property this doesn't work.

Thanks in advance for the help.

Was it helpful?

Solution

Your ShowCalendar method can't return a string that way, I understand that you want to show the calendar, let user select some date and hide it after that, store the selected date in a string, here is what it should be:

public static void ShowCalendar(Point locatieCalender, Form F1, Control textBox)
{
    MonthCalendar calender = new MonthCalendar();
    calender.Location = locatieCalender;
    calender.Parent = F1;
    //Register this event handler to assign the selected date accordingly to your textBox
    calendar.DateSelected += (s,e) => {
      textBox.Text = e.Start.ToString("dd-MM-yyyy");
      (s as MonthCalendar).Parent = null;
      (s as MonthCalendar).Dispose();          
    };
    calender.Show();
    calender.BringToFront();
}
//Use it
Point calenderLocatie = new Point(405, 69);
HelpFunction.ShowCalendar(calenderLocatie, this, txtPeriode_Tot);

OTHER TIPS

You need a handler for this. Remove the static keyword from the method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top