【问题标题】:Possible to set monthCalendar to show current month and previous 2 months?可以设置 monthCalendar 以显示当前月份和前 2 个月吗?
【发布时间】:2011-03-01 18:27:20
【问题描述】:

在 WinForms (3.5) 应用程序中,有一个带有 monthCalendar 控件的表单。

日历控件具有 3 列乘 1 行的 calendarDimension。这意味着它当前显示的是 2010 年 6 月、7 月、8 月。

是否可以让日历改为显示 2010 年 4 月、5 月、6 月?我的数据集没有任何未来日期,因此日期选择将针对当前或更早的日期。

【问题讨论】:

    标签: c# winforms .net-3.5 monthcalendar


    【解决方案1】:

    您可以使用以下代码行将MonthCalendarMaxDate 属性设置为表单加载事件中的当前日期。

    monthCalendar1.MaxDate = DateTime.Now;
    

    【讨论】:

    • 当 monthCalendar1 是表单的组件之一(子控件)时,表单的 Load 事件起作用。但是,如果您稍后在应用程序中添加 MonthCalendar 的实例,则表单加载不起作用。我建议使用 monthCalendar1.Layout 事件来设置 MonthCalendar 实例。
    【解决方案2】:

    如果您将 MonthCalendar 的 MaxDate 设置为当前日期,则月历将仅显示 - 因此允许选择 - 日期早于当前日期。

    【讨论】:

      【解决方案3】:

      为了将当前月份强制向右,我使用了 Pavan 的想法,但我添加了一个计时器 在日历控件上打开后重置 MaxDate。现在我可以在加载控件后滚动到未来。

      public partial class Form1 : Form
      {
         private DateTime _initialDateTime = DateTime.Now;
      
         public Form1()
         {
           InitializeComponent();
           // remember the default MAX date
           _initialDateTime = monthCalendar1.MaxDate;
           // set max date to NOW to force current month to right side
           monthCalendar1.MaxDate = DateTime.Now;
           // enable a timer to restore initial default date to enable scrolling into the future
           timer1.Start();
         }
      
         private void timer1_Tick(object sender, EventArgs e)
         {
           Timer timer = sender as Timer;
           if (timer != null)
           {
              // enable scrolling to the future
              monthCalendar1.MaxDate = _initialDateTime;
              // stop the timer...
              timer.Stop();
           }
         }
      }
      

      【讨论】:

        【解决方案4】:

        我发现在 MonthCalendar 具有自我意识之后,需要操作 MonthCalendar 以“滚动”到所需范围。

        MonthCalendar 自我感知之后(在您的程序完成初始化和显示之后,如果您执行MyMonthCalendar.SetSelectionRange(startDate,endDate),您可以通过在当前显示的月份之外设置startDate 来滚动日历。例如,如果我我将 8 个月显示为 2 列乘 4 行,然后MyMonthCalendar.SetSelectionRange(DateTime.Now.AddMonths(+6),DateTime.Now.AddMonths(+6)); 将滚动 MonthCalendar 以在 Month[col1,row[0]] 中显示 DateTime.Now(顶行,右列)。

        要注意的是,MonthCalendar.SetSelectionRange() 直到在 MonthCalendar 显示之后才会生效,并且在它退出其初始化线程之后可以“滚动”。这就是其他人描述的Timer方法有效的原因。

        我不知道早期的 .NET 版本,但在 .NET 4.6 中,您无需修改​​ MinDate 或 MaxDate 即可滚动 MonthCalendar。

        我建议尝试 MonthCalendar.Layout 事件,而不是使用 Timer 组件和事件。

        public MyForm()
        {
          // Standard design time component initialization
          InitializeComponent();
        
          // enable the MonthCalendar's Layout event handler
          this.MyMonthCalendar.Layout += MyMonthCalendar_Layout;
        }
        
        /// MonthCalendar Layout Event Handler
        private void MyMonthCalendar_Layout;(object sender, LayoutEventArgs e)
        {
          // disable this event handler because we only need to do it one time
          this.MyMonthCalendar.Layout -= MyMonthCalendar_Layout;
        
          // initialize the MonthCalendar so its months are aligned like we want them to be
          // To show a calendar with only April, May, and June 2010 do this
          this.MyMonthCalendar.SetSelectionRange(new DateTime(2010, 4, 1), new DateTime(2010, 6, 30));
        
          // MyMonthCalendar.TodayDate can be any date you want
          // However, MyMonthCalendar.SetDate should be within the SelectionRange or you might scroll the calendar
          this.MyMonthCalendar.SetDate(new DateTime(2010, 6, 30));      
        }
        

        【讨论】:

        • 平台测试 = .NET 4.6.2、VisualStudio 2017、Windows 10
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-06
        • 2014-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-29
        相关资源
        最近更新 更多