Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.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# 提示用户使用MonthCalendar控件选择日期_C#_.net_Winforms - Fatal编程技术网

C# 提示用户使用MonthCalendar控件选择日期

C# 提示用户使用MonthCalendar控件选择日期,c#,.net,winforms,C#,.net,Winforms,我正在编写一个Winform应用程序,在某个时候我想提示用户从日历中选择一个日期。所以在浏览谷歌之后,我想我可以使用MonthCalendar控件来实现这一点。现在我有了: MonthCalendar monthCalendar = new MonthCalendar(); monthCalendar.Show(); 问题是,当达到此代码时,什么也不会发生。我做错了什么 谢谢这样做并不困难。下面的代码使用一个小的表单,其中包含一个MonthCalender和一个按钮,该按钮是在

我正在编写一个Winform应用程序,在某个时候我想提示用户从日历中选择一个日期。所以在浏览谷歌之后,我想我可以使用MonthCalendar控件来实现这一点。现在我有了:

    MonthCalendar monthCalendar = new MonthCalendar();
    monthCalendar.Show();
问题是,当达到此代码时,什么也不会发生。我做错了什么


谢谢

这样做并不困难。下面的代码使用一个小的
表单
,其中包含一个
MonthCalender
和一个
按钮
,该按钮是在下图所示的设计器中创建的

此表单由另一个“父”表单调用。当用户单击“完成”按钮时,父窗体可以使用位于
窗体日历中的以下公共方法
GetDT
获取此日期。此代码仅获取第一个选定日期,但是用户可以选择多个日期。如果需要,还可以执行任何错误检查。希望这有帮助

表格1:

public Form1() {
  InitializeComponent();
}

private void button1_Click(object sender, EventArgs e) {
  FormCalendar fc = new FormCalendar();
  fc.ShowDialog();
  MessageBox.Show("User Selected Date: " + fc.GetDT.ToLongDateString());
}
日历

public FormCalendar() {
  InitializeComponent();
}

private void button1_Click(object sender, EventArgs e) {
  this.Close();
}

public DateTime GetDT {
  get {
    return monthCalendar1.SelectionStart;
  }
}

这是非常基本的windows窗体处理

MonthCalender
是一个控件。你不能只显示控件。控件只能显示在
集装箱控件上。
ContainerControl
是一个可以容纳其他控件并注意显示它们的控件

最著名的集装箱控制之一是表单。在VisualStudio中,我们通常使用设计器来创建表单并在其上放置控件

根据您是希望特殊MonthCalender仅用于一个特定表单,还是希望在多个表单上重复使用它,您必须决定是将MonthCalender用于表单还是用户控件

用户控件的好处在于,您可以使用visual studio中的设计器在任何表单上拖放特殊的MonthCalender

但最后,你必须把你的MonthCalender放在表格上的某个地方。一旦你展示了你的表格,MonthCalender就会和它一起展示

public class MySpecialForm : Form
{
    public MySpecialForm()
    {
        InitializeComponent();
    }
}
将表单添加到项目后,Visual Studio designer将创建上述表单:在项目的解决方案资源管理器中单击鼠标右键,然后选择“添加-Windows表单”

在设计器中,您选择工具箱(菜单视图工具箱),在工具箱上的
通用控件下选择MonthCalendar,然后单击要显示月历的窗体

这是基本的形式设计。添加MonthCalendar后,将在
InitializeComponent
中创建代码:

private System.Windows.Forms.MonthCalendar monthCalendar1;

private void InitializeComponent()
{
    this.monthCalendar1 = new System.Windows.Forms.MonthCalendar();
    // 
    // monthCalendar1
    // 
    this.monthCalendar1.Location = new System.Drawing.Point(266, 83);
    this.monthCalendar1.Name = "monthCalendar1";
    this.monthCalendar1.TabIndex = 3;
您不会经常编辑此文件,使用设计器更容易

一旦您拥有表单并希望显示它,例如,在有人单击主表单上的按钮后:

private void OnMainFormButtonClicked(object sender, ...)
{
    using (var mySpecialForm = new MySpecialForm()
    {
        // if desired set some properties of the form before showing
        mySpecialForm. ...
        // show the form and wait for the result:
        var dlgResult = mySpecialForm.ShowDialog(this);
        // process the result
        if (dlgResult == ...)
        {
            ProcessResult(form.GetFormResult);
        }
    }

MonthCalendar控件应添加到其表单控件集合中。这一切都不能由它自己来解释。谢谢可以使用
MonthCalender
控件和按钮创建一个小表单,以在输入日期时发出信号。然后在该表单上简单地
ShowDialog()
,提示用户输入日期?