โค้ดเป็นแบบนี้ครับ
namespace System.WindowsTOR.Forms
{
using System;
using System.Drawing;
using System.Drawing.Design;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms.Layout;
using System.Collections.Specialized;
using System.Runtime.InteropServices;
using System.Windows.Forms.Design;
using System.Security;
using System.Security.Permissions;
using Microsoft.Win32;
using System.Windows.Forms;
using System.Globalization;
[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ToolStrip)]
public class ToolStripDateTimePicker : ToolStripControlHost
{
private FlowLayoutPanel controlPanel;
private DateTimePicker picker = new DateTimePicker();
public ToolStripDateTimePicker()
: base(new FlowLayoutPanel())
{
controlPanel = (FlowLayoutPanel)base.Control;
picker.Format = DateTimePickerFormat.Long;
picker.Value = DateTime.Now;
this.picker.ValueChanged += new System.EventHandler(this.dateTimePicker_ValueChanged);
controlPanel.BackColor = Color.Transparent;
controlPanel.Controls.Add(picker);
}
string _CustomFormat;
[EditorBrowsable(EditorBrowsableState.Always)]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Bindable(true)]
public string CustomFormat
{
get
{
return _CustomFormat;
}
set
{
_CustomFormat = value;
if (DateTimeFormat == DateTimePickerFormat.Custom)
{
picker.CustomFormat = value;
}
}
}
DateTimePickerFormat _DateTimeFormat;
[EditorBrowsable(EditorBrowsableState.Always)]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Bindable(true)]
public DateTimePickerFormat DateTimeFormat
{
get { return _DateTimeFormat; }
set { _DateTimeFormat = value; picker.Format = value; }
}
DateTime _Value;
[EditorBrowsable(EditorBrowsableState.Always)]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Bindable(true)]
public DateTime Value
{
get { return _Value; }
set { _Value = value; this.picker.Value = value; }
}
protected override void OnPaint(PaintEventArgs e)
{
this.picker.Value = DateTime.Now;
}
protected override void OnSubscribeControlEvents(Control control)
{
base.OnSubscribeControlEvents(control);
}
protected override void OnUnsubscribeControlEvents(Control control)
{
base.OnUnsubscribeControlEvents(control);
}
private EventHandler onValueChanged;
protected virtual void OnValueChanged(EventArgs eventargs)
{
if (onValueChanged != null)
{
onValueChanged(this, eventargs);
}
}
public event EventHandler ValueChanged
{
add
{
onValueChanged += value;
}
remove
{
onValueChanged -= value;
}
}
private void dateTimePicker_ValueChanged(object sender, EventArgs e)
{
if (onValueChanged != null)
{
onValueChanged(this, e);
}
}
}
}
พอเรา เอาไปลง navigator รันโค้ดแล้ว value ไม่เปลี่ยนตาม CustomFormat ครับ
จะเป็น dd/MM/yyyy เช่น 23/1/2560 ครับ
ทั้งๆที่ DateTimeFormat กับ Value ก็ตั้งได้ครับ แต่ติดตรง CustomFormat
C# WinApp สงสัยเกี่ยวกับ การนำ Control มาลงใน navigator ครับ
namespace System.WindowsTOR.Forms
{
using System;
using System.Drawing;
using System.Drawing.Design;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms.Layout;
using System.Collections.Specialized;
using System.Runtime.InteropServices;
using System.Windows.Forms.Design;
using System.Security;
using System.Security.Permissions;
using Microsoft.Win32;
using System.Windows.Forms;
using System.Globalization;
[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ToolStrip)]
public class ToolStripDateTimePicker : ToolStripControlHost
{
private FlowLayoutPanel controlPanel;
private DateTimePicker picker = new DateTimePicker();
public ToolStripDateTimePicker()
: base(new FlowLayoutPanel())
{
controlPanel = (FlowLayoutPanel)base.Control;
picker.Format = DateTimePickerFormat.Long;
picker.Value = DateTime.Now;
this.picker.ValueChanged += new System.EventHandler(this.dateTimePicker_ValueChanged);
controlPanel.BackColor = Color.Transparent;
controlPanel.Controls.Add(picker);
}
string _CustomFormat;
[EditorBrowsable(EditorBrowsableState.Always)]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Bindable(true)]
public string CustomFormat
{
get
{
return _CustomFormat;
}
set
{
_CustomFormat = value;
if (DateTimeFormat == DateTimePickerFormat.Custom)
{
picker.CustomFormat = value;
}
}
}
DateTimePickerFormat _DateTimeFormat;
[EditorBrowsable(EditorBrowsableState.Always)]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Bindable(true)]
public DateTimePickerFormat DateTimeFormat
{
get { return _DateTimeFormat; }
set { _DateTimeFormat = value; picker.Format = value; }
}
DateTime _Value;
[EditorBrowsable(EditorBrowsableState.Always)]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Bindable(true)]
public DateTime Value
{
get { return _Value; }
set { _Value = value; this.picker.Value = value; }
}
protected override void OnPaint(PaintEventArgs e)
{
this.picker.Value = DateTime.Now;
}
protected override void OnSubscribeControlEvents(Control control)
{
base.OnSubscribeControlEvents(control);
}
protected override void OnUnsubscribeControlEvents(Control control)
{
base.OnUnsubscribeControlEvents(control);
}
private EventHandler onValueChanged;
protected virtual void OnValueChanged(EventArgs eventargs)
{
if (onValueChanged != null)
{
onValueChanged(this, eventargs);
}
}
public event EventHandler ValueChanged
{
add
{
onValueChanged += value;
}
remove
{
onValueChanged -= value;
}
}
private void dateTimePicker_ValueChanged(object sender, EventArgs e)
{
if (onValueChanged != null)
{
onValueChanged(this, e);
}
}
}
}
พอเรา เอาไปลง navigator รันโค้ดแล้ว value ไม่เปลี่ยนตาม CustomFormat ครับ
จะเป็น dd/MM/yyyy เช่น 23/1/2560 ครับ
ทั้งๆที่ DateTimeFormat กับ Value ก็ตั้งได้ครับ แต่ติดตรง CustomFormat