如何获取monthCalendar中鼠标选定的日期 C#

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何获取monthCalendar中鼠标选定的日期 C#相关的知识,希望对你有一定的参考价值。

如题

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

namespace Winform

public class Form4 : Form

public Form4()

InitializeComponent();


private void Form4_Load(object sender, EventArgs e)


//加载年 月 日 下拉框的值
for (int i = 1970; i < 2060; i++)

cboYear.Items.Add(i.ToString());

for (int j = 1; j < 13; j++)

cboMonth.Items.Add(j.ToString());

for (int k = 1; k < 32; k++)

cboDay.Items.Add(k.ToString());


//默认选中当天的日期
DateTime dt = DateTime.Today;
cboYear.SelectedItem = dt.Year.ToString();
cboMonth.SelectedItem = dt.Month.ToString();
cboDay.SelectedItem = dt.Day.ToString();



private void button1_Click(object sender, EventArgs e)

monthCalendar1.Visible = true;
monthCalendar1.Focus();


private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)

cboYear.SelectedItem = (e.End).Year.ToString();
cboMonth.SelectedItem = (e.End).Month.ToString();
cboDay.SelectedItem = (e.End).Day.ToString();
monthCalendar1.Visible = false;


/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)

if (disposing && (components != null))

components.Dispose();

base.Dispose(disposing);


#region Windows 窗体设计器生成的代码

/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()

this.cboYear = new System.Windows.Forms.ComboBox();
this.cboMonth = new System.Windows.Forms.ComboBox();
this.label13 = new System.Windows.Forms.Label();
this.label11 = new System.Windows.Forms.Label();
this.cboDay = new System.Windows.Forms.ComboBox();
this.label12 = new System.Windows.Forms.Label();
this.monthCalendar1 = new System.Windows.Forms.MonthCalendar();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// cboYear
//
this.cboYear.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cboYear.FormattingEnabled = true;
this.cboYear.Location = new System.Drawing.Point(25, 33);
this.cboYear.Name = "cboYear";
this.cboYear.Size = new System.Drawing.Size(60, 20);
this.cboYear.TabIndex = 36;
//
// cboMonth
//
this.cboMonth.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cboMonth.FormattingEnabled = true;
this.cboMonth.Location = new System.Drawing.Point(134, 33);
this.cboMonth.Name = "cboMonth";
this.cboMonth.Size = new System.Drawing.Size(60, 20);
this.cboMonth.TabIndex = 38;
//
// label13
//
this.label13.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label13.Location = new System.Drawing.Point(323, 36);
this.label13.Name = "label13";
this.label13.Size = new System.Drawing.Size(45, 18);
this.label13.TabIndex = 41;
this.label13.Text = "日";
//
// label11
//
this.label11.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label11.Location = new System.Drawing.Point(97, 36);
this.label11.Name = "label11";
this.label11.Size = new System.Drawing.Size(45, 18);
this.label11.TabIndex = 37;
this.label11.Text = "年";
//
// cboDay
//
this.cboDay.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cboDay.FormattingEnabled = true;
this.cboDay.Location = new System.Drawing.Point(246, 32);
this.cboDay.Name = "cboDay";
this.cboDay.Size = new System.Drawing.Size(60, 20);
this.cboDay.TabIndex = 40;
//
// label12
//
this.label12.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label12.Location = new System.Drawing.Point(216, 36);
this.label12.Name = "label12";
this.label12.Size = new System.Drawing.Size(45, 18);
this.label12.TabIndex = 39;
this.label12.Text = "月";
//
// monthCalendar1
//
this.monthCalendar1.Location = new System.Drawing.Point(257, 80);
this.monthCalendar1.Name = "monthCalendar1";
this.monthCalendar1.TabIndex = 42;
this.monthCalendar1.Visible = false;
this.monthCalendar1.DateSelected += new System.Windows.Forms.DateRangeEventHandler(this.monthCalendar1_DateSelected);
//
// button1
//
this.button1.Location = new System.Drawing.Point(67, 109);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 43;
this.button1.Text = "选择日期";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form4
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(579, 304);
this.Controls.Add(this.button1);
this.Controls.Add(this.monthCalendar1);
this.Controls.Add(this.cboYear);
this.Controls.Add(this.cboMonth);
this.Controls.Add(this.label13);
this.Controls.Add(this.label11);
this.Controls.Add(this.cboDay);
this.Controls.Add(this.label12);
this.Name = "Form4";
this.Text = "Form4";
this.Load += new System.EventHandler(this.Form4_Load);
this.ResumeLayout(false);



#endregion

private System.Windows.Forms.ComboBox cboYear;
private System.Windows.Forms.ComboBox cboMonth;
private System.Windows.Forms.Label label13;
private System.Windows.Forms.Label label11;
private System.Windows.Forms.ComboBox cboDay;
private System.Windows.Forms.Label label12;
private System.Windows.Forms.MonthCalendar monthCalendar1;
private System.Windows.Forms.Button button1;

参考技术A textBox1.Text = monthCalendar1.SelectionStart.ToString("yyyyMMdd");
这个是显示选中日期的开始时间,如果是点击的话,就是这个语句就好。
如果是拖动选择日期段的话,这个是开始日期,
结束日期是
textBox1.Text = monthCalendar1.SelectionEnd.ToString("yyyyMMdd");
参考技术B date=this.monthcalendar1.SelectionStart.toString(); 参考技术C SelectionStart SelectionEnd试下这两个属性

如何使用 MonthCalendar 获取“星期几”?

【中文标题】如何使用 MonthCalendar 获取“星期几”?【英文标题】:How do I get the "Day of the Week" using MonthCalendar? 【发布时间】:2017-02-16 17:35:33 【问题描述】:

我正在使用 WinForms,我正在尝试获取星期几。

因此,如果有人选择 2/16/2017,那么我将得到星期四。

我知道 DateTimePicker 控件可以做到这一点,但我确实需要使用 monthCalendar

【问题讨论】:

DayOfWeek 是 DateTime 的一个属性 我可以将 monthCalendar 日期转换为 DAteTimePicker 对象吗? 只是 monthCalendar1.SelectionRange.Start.DayOfWeek ? 为什么投反对票? 【参考方案1】:

确实,使用DateTime.DayOfWeek的最简单方法。

mounthCalerndar.SelectionStart.DayOfWeek 

应该做的工作。

【讨论】:

以上是关于如何获取monthCalendar中鼠标选定的日期 C#的主要内容,如果未能解决你的问题,请参考以下文章

从选定的月历日期获取星期

请教如何获取monthcalendar被选中的日期信息

C# MonthCalendar 控件获取光标下的日期

vb.net monthcalendar 判断是不是选中某个日期

如何应用VB.NET MonthCalendar控件

如何从 MonthCalendar 显示日期到 TextBox C#