Category / Section
How to change the width of the WinForms DateTimePicker (DateTimePickerAdv) when it is in the standard format?
1 min read
Date time Format
It is possible to adjust the width of the DateTimePickerAdv when it is in standard format. 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 the specified standard format.
C#
//New font size is set here. this.dateTimePickerAdv1.Font = new Font("Tahoma", 13); this.dateTimePickerAdv1.ShowCheckBox = false; this.dateTimePickerAdv1.ShowUpDown = true; this.dateTimePickerAdv1.AutoSize = false; this.dateTimePickerAdv1.AutoSizeMode = AutoSizeMode.GrowOnly; this.dateTimePickerAdv1.Format = DateTimePickerFormat.Long; int updownWidth = 0, dropWidth = 0; //Calculating the UpDown and DropDown button width foreach (Control ctrl in this.dateTimePickerAdv1.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.dateTimePickerAdv1.Font.Size * this.dateTimePickerAdv1.Value.ToLongDateString().Length); this.dateTimePickerAdv1.Width = nWidth;
VB
''Font is set Me.DateTimePickerAdv2.Font = New Font("Tahoma", 13) Me.DateTimePickerAdv2.ShowCheckBox = False Me.DateTimePickerAdv2.ShowUpDown = True Me.DateTimePickerAdv2.AutoSize = False Me.DateTimePickerAdv2.AutoSizeMode = Windows.Forms.AutoSizeMode.GrowOnly Me.DateTimePickerAdv2.Format = DateTimePickerFormat.Long Dim updownWidth As Integer = 0, dropWidth As Integer = 0 ''Calculating the UpDown and DropDown button width For Each ctrl As Control In Me.DateTimePickerAdv2.Controls If ctrl.GetType().ToString() = "Syncfusion.Windows.Forms.Tools.DateTimePickerUpDownButton" Then updownWidth = ctrl.Size.Width ElseIf 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.DateTimePickerAdv2.Font.Size * Me.DateTimePickerAdv2.Value.ToLongDateString().Length) Me.DateTimePickerAdv2.Width = nWidth