简体   繁体   English

从Winforms月份日历中选择月份?

[英]Get selected month from Winforms month calendar?

I have a MonthCalendar control on my form. 我的表单上有一个MonthCalendar控件。 I have set it so that it selects an entire week at a time (from Sun to Sat). 我已对其进行设置,以使其一次选择一个整个星期(从星期日到星期六)。

At the top of the control, the user is able to select a month. 在控件的顶部,用户可以选择一个月。 How can I obtain the month that the user has selected? 如何获得用户选择的月份? Issues rise when there is a week that contains days from two different months. 当一周中包含来自两个不同月份的几天时,问题就会增加。

Eg If user selects the week of Nov 29 2015 to Dec 5 2015 and has the month of November selected in the control. 例如,如果用户选择2015年11月29日至2015年12月5日这一周,并在控件中选择了11月。 They could have December selected as well, I don't know how to tell. 他们也可以选择12月,我不知道该怎么说。

Code for selecting week (it doesn't select from Sunday to Saturday but that's a problem for later): 选择星期的代码(从星期日到星期六不选择,但是以后会出现问题):

int i = (int)MonthView1.SelectionStart.DayOfWeek;
Date d = MonthView1.SelectionStart;
MonthView1.SelectionStart = d.AddDays(1 - i);
MonthView1.SelectionEnd = d.AddDays(7 - i);

Thanks! 谢谢!

This will use the month of the start of the selection. 这将使用选择开始的月份。 Is this what you want? 这是你想要的吗?

    private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
    {
        DateTime d = monthCalendar1.SelectionRange.Start;
        Console.WriteLine(d.Month.ToString());
    }

You can easily change it to monthCalendar1.SelectionRange.End if using start is not what you want. 如果monthCalendar1.SelectionRange.End使用start,则可以轻松将其更改为monthCalendar1.SelectionRange.End

EDIT: 编辑:

Putting your selection code in MouseDown event, I noticed the small selection (dotted line) box will always be on Monday, which is also start of selection. 将您的选择代码置于MouseDown事件中,我注意到小的选择(虚线)框始终位于星期一,这也是选择的开始。 That means if Monday lies on the previous month, monthCalender will scroll to the previous month. 这意味着,如果星期一在上个月,则monthCalender将滚动到上个月。 Hence using monthCalendar1.SelectionRange.Start should fulfill your requirements. 因此,使用monthCalendar1.SelectionRange.Start应该可以满足您的要求。

EDIT2: 编辑2:

Maybe you tried to put everything in 1 callback? 也许您试图将所有内容放入1个回调中? Here's my complete code. 这是我完整的代码。 It won't glitch. 它不会出现故障。

(Though the monthCalendar1_DateChanged might be called multiple times when you do a mouse down. The LAST time it is called will give you the correct Month) (尽管当您按下鼠标时,可能会多次调用monthCalendar1_DateChanged 。最后一次调用它会为您提供正确的Month)

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.monthCalendar1.DateChanged += new System.Windows.Forms.DateRangeEventHandler(this.monthCalendar1_DateChanged);
        this.monthCalendar1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.monthCalendar1_MouseDown);
    }

    private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
    {
        DateTime d = monthCalendar1.SelectionRange.Start;
        Console.WriteLine(d.Month.ToString()); //Get the month selected. 
    }

    private void monthCalendar1_MouseDown(object sender, MouseEventArgs e)
    {
        int i = (int)monthCalendar1.SelectionStart.DayOfWeek;
        DateTime d = monthCalendar1.SelectionStart;
        monthCalendar1.SelectionStart = d.AddDays(1 - i);
        monthCalendar1.SelectionEnd = d.AddDays(7 - i);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM