作成日: 2021/03/16 最終更新日: 2021/03/16
文書種別
使用方法
詳細
C1DateEditで使用しているドロップダウンカレンダーには、デフォルトで年/月/日が表示されます。
この動作を、年/月のみが表示されるように変更するには、C1DropDownControlコントロールを使用したカスタムのドロップダウンカレンダーを作成します。
例えば、DropDownFormクラスを継承し、上部に次へ/前へボタン、下部に12個のボタン配置したCalendarDropDownという名のフォームを作成し、各ボタンクリック時の動作をすべてコードで制御して疑似カレンダーとして動作させる方法が考えられます。
⇒サンプルは こちら(C#/VB)
◎サンプルコード(C#の場合)
※このサンプルでは、CalendarDropDownフォーム上にSplitContainerを貼り付け、その上部と下部に各種ボタンを配置しています。
この動作を、年/月のみが表示されるように変更するには、C1DropDownControlコントロールを使用したカスタムのドロップダウンカレンダーを作成します。
例えば、DropDownFormクラスを継承し、上部に次へ/前へボタン、下部に12個のボタン配置したCalendarDropDownという名のフォームを作成し、各ボタンクリック時の動作をすべてコードで制御して疑似カレンダーとして動作させる方法が考えられます。
⇒サンプルは こちら(C#/VB)
◎サンプルコード(C#の場合)
※このサンプルでは、CalendarDropDownフォーム上にSplitContainerを貼り付け、その上部と下部に各種ボタンを配置しています。
(CalendarDropDown.cs)
using C1.Win.C1Input;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace C1CustomDateEdit
{
class CalendarDropDown : DropDownForm
{
private System.Windows.Forms.Button btnNext;
private System.Windows.Forms.Button btnPrevious;
private System.Windows.Forms.Label lblYear;
private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel1;
private System.Windows.Forms.SplitContainer splitContainer1;
private DateTime _dateVal;
private void InitializeComponent()
{
this.btnNext = new System.Windows.Forms.Button();
this.btnPrevious = new System.Windows.Forms.Button();
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.lblYear = new System.Windows.Forms.Label();
this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout();
this.SuspendLayout();
//
// btnNext
//
this.btnNext.AutoSize = true;
this.btnNext.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.btnNext.Dock = System.Windows.Forms.DockStyle.Right;
this.btnNext.FlatAppearance.BorderSize = 0;
this.btnNext.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnNext.Location = new System.Drawing.Point(169, 0);
this.btnNext.Margin = new System.Windows.Forms.Padding(0);
this.btnNext.Name = "btnNext";
this.btnNext.Size = new System.Drawing.Size(21, 25);
this.btnNext.TabIndex = 0;
this.btnNext.Text = ">";
this.btnNext.UseVisualStyleBackColor = true;
this.btnNext.Click += new System.EventHandler(this.button1_Click);
//
// btnPrevious
//
this.btnPrevious.AutoSize = true;
this.btnPrevious.Dock = System.Windows.Forms.DockStyle.Left;
this.btnPrevious.FlatAppearance.BorderSize = 0;
this.btnPrevious.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnPrevious.Location = new System.Drawing.Point(0, 0);
this.btnPrevious.Margin = new System.Windows.Forms.Padding(0);
this.btnPrevious.Name = "btnPrevious";
this.btnPrevious.Size = new System.Drawing.Size(29, 25);
this.btnPrevious.TabIndex = 1;
this.btnPrevious.Text = "<";
this.btnPrevious.UseVisualStyleBackColor = true;
this.btnPrevious.Click += new System.EventHandler(this.button2_Click);
//
// splitContainer1
//
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.Location = new System.Drawing.Point(0, 0);
this.splitContainer1.Name = "splitContainer1";
this.splitContainer1.Orientation = System.Windows.Forms.Orientation.Horizontal;
//
// splitContainer1.Panel1
//
this.splitContainer1.Panel1.Controls.Add(this.lblYear);
this.splitContainer1.Panel1.Controls.Add(this.btnPrevious);
this.splitContainer1.Panel1.Controls.Add(this.btnNext);
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.Controls.Add(this.flowLayoutPanel1);
this.splitContainer1.Size = new System.Drawing.Size(190, 175);
this.splitContainer1.SplitterDistance = 25;
this.splitContainer1.SplitterWidth = 1;
this.splitContainer1.TabIndex = 3;
//
// lblYear
//
this.lblYear.Dock = System.Windows.Forms.DockStyle.Fill;
this.lblYear.Location = new System.Drawing.Point(29, 0);
this.lblYear.Name = "lblYear";
this.lblYear.Size = new System.Drawing.Size(140, 25);
this.lblYear.TabIndex = 2;
this.lblYear.Text = "Year";
this.lblYear.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.lblYear.Click += new System.EventHandler(this.lblYear_Click);
//
// flowLayoutPanel1
//
this.flowLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.flowLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.flowLayoutPanel1.Name = "flowLayoutPanel1";
this.flowLayoutPanel1.Size = new System.Drawing.Size(190, 149);
this.flowLayoutPanel1.TabIndex = 0;
//
// CalendarDropDown
//
this.ClientSize = new System.Drawing.Size(190, 175);
this.Controls.Add(this.splitContainer1);
this.Name = "CalendarDropDown";
this.splitContainer1.Panel1.ResumeLayout(false);
this.splitContainer1.Panel1.PerformLayout();
this.splitContainer1.Panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
this.splitContainer1.ResumeLayout(false);
this.ResumeLayout(false);
}
public CalendarDropDown()
{
InitializeComponent();
this.Options |= DropDownFormOptionsFlags.AlwaysPostChanges;
Load += Calendar_Load;
FormClosing += Calendar_FormClosing;
PostChanges += Calendar_PostChanges;
}
private void UpdateValue()
{
OwnerControl.Value = _dateVal;
}
private void Calendar_PostChanges(object sender, EventArgs e)
{
UpdateValue();
}
private void Calendar_FormClosing(object sender, FormClosingEventArgs e)
{
UpdateValue();
}
private void Calendar_Load(object sender, EventArgs e)
{
Width = OwnerControl.Width;
InitAll();
AddMonthsBtn();
}
private void InitAll()
{
if (OwnerControl.Value is null )
{
_dateVal = DateTime.Today;
}
else
{
try
{
_dateVal = (DateTime)OwnerControl.Value;
}
catch (Exception e) {
Debug.WriteLine(e.Message);
_dateVal = DateTime.Today;
}
}
UpdateYearLabel();
}
private void UpdateYearLabel()
{
lblYear.Text = _dateVal.Year.ToString();
}
private void DownAYear()
{
_dateVal = _dateVal.AddYears(-1);
UpdateYearLabel();
}
private void UpAYear()
{
_dateVal = _dateVal.AddYears(1);
UpdateYearLabel();
}
//Add Months Buttons
private void AddMonthsBtn()
{
int y = 0;
int i = 1;
foreach (var c in CultureInfo.CurrentCulture.DateTimeFormat.AbbreviatedMonthNames)
{
if (c == "") continue;
Button monthButton = new Button();
//monthButton.FlatStyle = FlatStyle.Flat;
monthButton.FlatStyle = FlatStyle.Popup; //****test****
monthButton.FlatAppearance.BorderSize = 0;
monthButton.Size = new System.Drawing.Size(40, 30);
monthButton.Text = c;
monthButton.Tag = i;
i++;
monthButton.Click += MonthButton_Click;
flowLayoutPanel1.Controls.Add(monthButton);
if ((monthButton.Location.Y + monthButton.Height) > Height)
{
Height += monthButton.Height + 10;
}
y = monthButton.Bottom;
}
if (y < flowLayoutPanel1.Height)
{
Height -= flowLayoutPanel1.Height - y - 10;
}
}
private void MonthButton_Click(object sender, EventArgs e)
{
int month = (int)((Control)sender).Tag;
_dateVal = new DateTime(_dateVal.Year, month, 1);
CloseDropDown(true);
}
private void button1_Click(object sender, EventArgs e)
{
UpAYear();
}
private void button2_Click(object sender, EventArgs e)
{
DownAYear();
}
private void lblYear_Click(object sender, EventArgs e)
{
}
}
}
(CustomDateEdit.cs)
using C1.Win.C1Input;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace C1CustomDateEdit
{
class CustomDateEdit : C1DropDownControl
{
public override string DropDownFormClassName {
get {
return "C1CustomDateEdit.CalendarDropDown";
}
}
public CustomDateEdit() {
VisibleButtons = DropDownControlButtonFlags.DropDown;
DataType = typeof(DateTime);
this.FormatType = FormatTypeEnum.YearAndMonth;
}
}
}
(Form1.Designer.cs)(抜粋)
private void InitializeComponent()
{
this.customDateEdit1 = new C1CustomDateEdit.CustomDateEdit();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.customDateEdit1)).BeginInit();
this.SuspendLayout();
//
// customDateEdit1
//
this.customDateEdit1.DataType = typeof(System.DateTime);
this.customDateEdit1.DisplayFormat.FormatType = C1.Win.C1Input.FormatTypeEnum.YearAndMonth;
this.customDateEdit1.DisplayFormat.Inherit = ((C1.Win.C1Input.FormatInfoInheritFlags)((((((C1.Win.C1Input.FormatInfoInheritFlags.CustomFormat | C1.Win.C1Input.FormatInfoInheritFlags.NullText)
| C1.Win.C1Input.FormatInfoInheritFlags.EmptyAsNull)
| C1.Win.C1Input.FormatInfoInheritFlags.TrimStart)
| C1.Win.C1Input.FormatInfoInheritFlags.TrimEnd)
| C1.Win.C1Input.FormatInfoInheritFlags.CalendarType)));
this.customDateEdit1.FormatType = C1.Win.C1Input.FormatTypeEnum.YearAndMonth;
this.customDateEdit1.GapHeight = 0;
this.customDateEdit1.ImagePadding = new System.Windows.Forms.Padding(0);
this.customDateEdit1.Location = new System.Drawing.Point(62, 37);
this.customDateEdit1.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
this.customDateEdit1.Name = "customDateEdit1";
this.customDateEdit1.Size = new System.Drawing.Size(150, 17);
this.customDateEdit1.TabIndex = 0;
this.customDateEdit1.Tag = null;
this.customDateEdit1.VisibleButtons = C1.Win.C1Input.DropDownControlButtonFlags.DropDown;
this.customDateEdit1.TextChanged += new System.EventHandler(this.customDateEdit1_TextChanged);
・・・以下省略・・・
旧文書番号
86493