Question

I wan't to make a Class/function which must be able to show a calender at a given point on a form. And then return the selected date.

This is what I came up with:

public static string ShowCalendar(Point locatieCalender)
    {
        MonthCalendar calender = new MonthCalendar();
        calender.Location = locatieCalender;
        calender.Show();
        calender.Visible = true;

        string date = calender.SelectionRange.Start.ToShortDateString();
        DateTime dateValue = DateTime.Parse(date);
        string dateForTextbox = dateValue.ToString("dd-MM-yyyy");

        calender.Hide();
        return dateForTextbox;

       }

The problem is the calender won't show on the form.

I would appreciate some help with this. Thanks in advance

Ok Now I've changed my function to :

 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:

string dateForTextbox = HelpFunction.ShowCalendar(calenderLocatie, this);

Now the calender shows but no string is returned. I've tried with event but because of the static function that won't work.

Was it helpful?

Solution

You need to add it to the form as a control. See this link for help on how to do that: http://msdn.microsoft.com/en-us/library/kyt0fzt1.aspx

For instance:

Controls.Add(calendar);

OTHER TIPS

Remove calender.Hide() in your code. And add it to your form through Controls.Add or Parent property, like this:

   //....
    string dateForTextbox = dateValue.ToString("dd-MM-yyyy");
    calender.Parent = yourForm;
    return dateForTextbox;
   }

I think you should declare a variable of MonthCalendar somewhere in your Form class, use it in your ShowCalendar instead of the local variable calendar.

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