Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Resizing DateTimePicker to fit max value text

Resizing DateTimePicker to fit max value text

Scheduled Pinned Locked Moved C#
csharpwinformscssgraphicshelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hpjchobbes
    wrote on last edited by
    #1

    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! =)

    K L 2 Replies Last reply
    0
    • H hpjchobbes

      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! =)

      K Offline
      K Offline
      KaptinKrunch
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • H hpjchobbes

        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! =)

        L Offline
        L Offline
        led mike
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups