Category / Section
How to change the width of the WinForms DateTimePicker (DateTimePickerAdv) when it is set to custom format?
1 min read
Date time format
It is possible to adjust the width of the DateTimePickerAdv according to the custom format given. The width of the DateTimePickerAdv is calculated by adding the following constraints.
- Width of the DateTimePickerUpDownButton
- Width of the DateTimePickerDropButton
- Font size of the DateTimePickerAdv
- The length of the DataTimePickerAdv's Value in custom format
C#
this.dateTimePickerAdv3.ShowCheckBox = false; this.dateTimePickerAdv3.ShowUpDown = true; this.dateTimePickerAdv3.AutoSize = false; this.dateTimePickerAdv3.AutoSizeMode = AutoSizeMode.GrowOnly; this.dateTimePickerAdv3.Format = DateTimePickerFormat.Custom; this.dateTimePickerAdv3.CustomFormat = "dd/MMM"; int updownWidth = 0, dropWidth = 0; foreach (Control ctrl in this.dateTimePickerAdv3.Controls) { if (ctrl.GetType().ToString() == "Syncfusion.Windows.Forms.Tools.DateTimePickerUpDownButton") updownWidth = ctrl.Size.Width; else if (ctrl.GetType().ToString() == "Syncfusion.Windows.Forms.Tools.DateTimePickerDropButton") dropWidth = ctrl.Size.Width; } //Sum up the width of drop down button, updown button and font. int nWidth = dropWidth + updownWidth + Convert.ToInt32(this.dateTimePickerAdv3.Font.Size * this.dateTimePickerAdv3.Value.ToString(this.dateTimePickerAdv3.CustomFormat).Length); this.dateTimePickerAdv3.Width = nWidth;
VB
Me.dateTimePickerAdv3.ShowCheckBox = False Me.dateTimePickerAdv3.ShowUpDown = True Me.dateTimePickerAdv3.AutoSize = False Me.dateTimePickerAdv3.AutoSizeMode = AutoSizeMode.GrowOnly Me.dateTimePickerAdv3.Format = DateTimePickerFormat.Custom Me.dateTimePickerAdv3.CustomFormat = "dd/MMM" Dim updownWidth As Integer = 0, dropWidth As Integer = 0 For Each ctrl As Control In Me.dateTimePickerAdv3.Controls If ctrl.GetType().ToString() = "Syncfusion.Windows.Forms.Tools.DateTimePickerUpDownButton" Then updownWidth = ctrl.Size.Width Else If ctrl.GetType().ToString() = "Syncfusion.Windows.Forms.Tools.DateTimePickerDropButton" Then dropWidth = ctrl.Size.Width End If Next ctrl ''Sum up the width of drop down button, updown button and font. Dim nWidth As Integer = dropWidth + updownWidth + Convert.ToInt32(Me.dateTimePickerAdv3.Font.Size * Me.dateTimePickerAdv3.Value.ToString(Me.dateTimePickerAdv3.CustomFormat).Length) Me.dateTimePickerAdv3.Width = nWidth