Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 静态函数C没有返回任何字符串#_C#_Winforms_Static_Return Value_Monthcalendar - Fatal编程技术网

C# 静态函数C没有返回任何字符串#

C# 静态函数C没有返回任何字符串#,c#,winforms,static,return-value,monthcalendar,C#,Winforms,Static,Return Value,Monthcalendar,您好,我正在尝试创建一个函数,该函数将在给定表单上的givven位置显示日历,并以字符串形式返回所选日期 到目前为止,我得到的是: public static string ShowCalendar(Point locatieCalender, Form F1) { MonthCalendar calender = new MonthCalendar(); calender.Location = locatieCalender; calen

您好,我正在尝试创建一个函数,该函数将在给定表单上的givven位置显示日历,并以字符串形式返回所选日期

到目前为止,我得到的是:

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;
日历显示在表单上,但不返回字符串。我尝试了一个事件处理程序,但由于静态属性的原因,这不起作用


提前感谢您的帮助。

您需要一个处理程序。从方法中删除static关键字。

您的
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);

您是否尝试设置断点并通过静态方法进行漫游?断点总是很容易找到,这很有效!!你能解释一下箭头的作用和(s,e)吗?请(-:@user2726516,这是
lambda表达式的一部分
。您可能需要对此进行更多搜索。
(s,e)
参数部分
,一个
DateSelected
事件处理程序有两个参数,部分
=>{…}
定义事件处理程序的代码。事实上,我们可以定义一个单独的方法,例如命名的
DateSelected\u handler
,然后执行此操作:
calendar.DateSelected+=DateSelected\u handler
,但是
DateSelected\u handler
的签名应该是合适的,它包含
object
Da>两个参数teSelectedEventArgs