C#-将所选日期从monthCalendar保存到字典中

C#-将所选日期从monthCalendar保存到字典中,c#,dictionary,monthcalendar,C#,Dictionary,Monthcalendar,我正在用C#做日历。为此,我使用MonthCalendar(windows窗体)。 我想在日历上选择一个日期,并为该日期添加一些文本(如约会)。然后我想把日期和文本保存在字典里 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq;

我正在用C#做日历。为此,我使用MonthCalendar(windows窗体)。 我想在日历上选择一个日期,并为该日期添加一些文本(如约会)。然后我想把日期和文本保存在字典里

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;

namespace agenda
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
    {

        //Selected only 1 date
        monthCalendar1.MaxSelectionCount = 1;
    }

    private void monthCalendar1_DateSelected(object sender, System.Windows.Forms.DateRangeEventArgs e)
    {
        var geselecteerdeDatum = monthCalendar1.SelectionRange.Start.ToString(); 

    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        Dictionary<DateTime, string> dictionary = new Dictionary<DateTime, string>();
        dictionary.Add(geselecteerdeDatum, textBox1.Text);

        //show what is in the dictionary / print it in the console
        Console.WriteLine(geselecteerdeDatum);
        foreach (KeyValuePair<DateTime, string> kvp in dictionary)
        {
            Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
        }
    }

    public DateTime geselecteerdeDatum{ get; set; }
}

有人知道如何在字典中获取所选日期吗?

更改
字典.Add(geselecteerdeDatum,textBox1.Text)
添加(monthCalendar1.SelectionStart,textBox1.Text)

而且无需多次设置
MaxSelectionCount
(您可以在表单设计器中进行设置)


var geselecteerdeatum
创建一个新的字符串变量(本地),该变量与您的
DateTime geselecteerdeatum

您在DateSelected事件中有
var geselecteerdeatum
。这超出了范围。删除var并确保在表单范围内声明变量。您的意思是:确保在表单范围内声明变量?在编辑之前,我没有在任何地方看到您的geselecteerdeDatum变量。很抱歉,我粘贴代码并将其翻译为英语时出错。
  Key = 1-1-0001 00:00:00, Value = message