-1

こんにちは、特定のフォームの特定の場所にカレンダーを表示し、選択した日付を文字列で返す関数を作成しようとしています。

これは私がこれまでに持っているものです:

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;

    }

関数呼び出しは次のようになります。

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

カレンダーはフォームに表示されますが、文字列は返されません。イベント ハンドラーを試しましたが、静的プロパティのため、これは機能しません。

助けてくれてありがとう。

4

2 に答える 2

0

これにはハンドラーが必要です。メソッドから static キーワードを削除します。

于 2013-08-28T19:39:26.970 に答える
0

あなたのShowCalendarメソッドはそのように文字列を返すことはできません。カレンダーを表示し、ユーザーに日付を選択させてから非表示にし、選択した日付を文字列に保存したいことを理解しています。

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);
于 2013-08-28T20:01:47.007 に答える