Resizing DateTimePicker to fit max value text
-
I am creating a ToolStrip that I have a DateTimePicker added on using the following code:
DateTimePicker FromDate = new DateTimePicker();
FromDate.Format = DateTimePickerFormat.Short;
MainToolStrip.Items.Add(new ToolStripControlHost(FromDate));
I am wanting to change the width of the DateTimePicker to be just large enough to fit the text needed. My thought was to measure the MaxDate value string using the font and that would be the width, like so:
Size dtps = TextRenderer.MeasureText(FromDate.MaxDate.ToShortDateString(), FromDate.Font);
FromDate.Size = dtps;
This works, except for one problem, it does not include the width for the drop down button on the DateTimePicker control, so it overlaps the text. I do not know how to get the width of the drop down button on the DateTimePicker, and my searches have been less than successful. Does anyone have a reference to where I can learn how to do this? Also, is there any good references that go into some of this type of detail? Most of the books and websites I visit will teach you the basics of a control, like how to change the font, width, height, and common properties, but I would like to get a bit deeper with these items and really see what they can do. I think these books might be what I am looking for, but have not got them yet: 'GDI+ Custom Controls with Visual C# 2005' by Tiberiu Radu, Iulian Serban, Dragos Brezoi, Adam Ward 'Pro .NET 2.0 Windows Forms and Custom Controls in C#' by Matthew MacDonald 'Windows Forms 2.0 Programming' by Chris Sells, Michael Weinhardt Anyone have any experience with these books, or maybe could recommend something similar? Thanks for your time, in advance! =)
-
I am creating a ToolStrip that I have a DateTimePicker added on using the following code:
DateTimePicker FromDate = new DateTimePicker();
FromDate.Format = DateTimePickerFormat.Short;
MainToolStrip.Items.Add(new ToolStripControlHost(FromDate));
I am wanting to change the width of the DateTimePicker to be just large enough to fit the text needed. My thought was to measure the MaxDate value string using the font and that would be the width, like so:
Size dtps = TextRenderer.MeasureText(FromDate.MaxDate.ToShortDateString(), FromDate.Font);
FromDate.Size = dtps;
This works, except for one problem, it does not include the width for the drop down button on the DateTimePicker control, so it overlaps the text. I do not know how to get the width of the drop down button on the DateTimePicker, and my searches have been less than successful. Does anyone have a reference to where I can learn how to do this? Also, is there any good references that go into some of this type of detail? Most of the books and websites I visit will teach you the basics of a control, like how to change the font, width, height, and common properties, but I would like to get a bit deeper with these items and really see what they can do. I think these books might be what I am looking for, but have not got them yet: 'GDI+ Custom Controls with Visual C# 2005' by Tiberiu Radu, Iulian Serban, Dragos Brezoi, Adam Ward 'Pro .NET 2.0 Windows Forms and Custom Controls in C#' by Matthew MacDonald 'Windows Forms 2.0 Programming' by Chris Sells, Michael Weinhardt Anyone have any experience with these books, or maybe could recommend something similar? Thanks for your time, in advance! =)
In regards to measuring your text, you could just add a constant value to the total width assuming the size of the button does not change.
Just because we can; does not mean we should.
-
I am creating a ToolStrip that I have a DateTimePicker added on using the following code:
DateTimePicker FromDate = new DateTimePicker();
FromDate.Format = DateTimePickerFormat.Short;
MainToolStrip.Items.Add(new ToolStripControlHost(FromDate));
I am wanting to change the width of the DateTimePicker to be just large enough to fit the text needed. My thought was to measure the MaxDate value string using the font and that would be the width, like so:
Size dtps = TextRenderer.MeasureText(FromDate.MaxDate.ToShortDateString(), FromDate.Font);
FromDate.Size = dtps;
This works, except for one problem, it does not include the width for the drop down button on the DateTimePicker control, so it overlaps the text. I do not know how to get the width of the drop down button on the DateTimePicker, and my searches have been less than successful. Does anyone have a reference to where I can learn how to do this? Also, is there any good references that go into some of this type of detail? Most of the books and websites I visit will teach you the basics of a control, like how to change the font, width, height, and common properties, but I would like to get a bit deeper with these items and really see what they can do. I think these books might be what I am looking for, but have not got them yet: 'GDI+ Custom Controls with Visual C# 2005' by Tiberiu Radu, Iulian Serban, Dragos Brezoi, Adam Ward 'Pro .NET 2.0 Windows Forms and Custom Controls in C#' by Matthew MacDonald 'Windows Forms 2.0 Programming' by Chris Sells, Michael Weinhardt Anyone have any experience with these books, or maybe could recommend something similar? Thanks for your time, in advance! =)
hpjchobbes wrote:
This works, except for one problem, it does not include the width for the drop down button on the DateTimePicker contro
I am having success using
SystemInformation.VerticalScrollBarWidth
for the dropdown button width in my experiments.led mike