h1.post-title { color:orange; font-family:verdana,Arial; font-weight:bold; padding-bottom:5px; text-shadow:#64665b 0px 1px 1px; font-size:32px; } -->

Pages

Adjust width of DateTimepicker

Today , we will discuss about How to adjust width of DateTimepicker in C#.Net.
Now, let’s start:
Create Project in VS 2008 .
Open your form
Drag and Drop DateTimePicker from Toolbar into Form
Go To Code Section .
Copy and Paste the following code in the Code section .

private void dateTimePickerwidth(DateTimePicker dtp,string format)
        {
         System.Drawing.Graphics g = dtp.CreateGraphics();
            float maxwidth = 0f;
            float w = g.MeasureString(Convert.ToString(dtp), dtp.Font).Width;
               if (w < maxwidth)
                    maxwidth = w;
             g.Dispose();
            switch (format)
            {
                case "Long":
                    dtp.Width = (int)maxwidth + 220;
                    break;
                case "Short":
                    dtp.Width = (int)maxwidth + 100;
                    break;
                case "Time":
                    dtp.Width = (int)maxwidth + 100;
                    break;
                case "Custom":
                    dtp.Width = (int)maxwidth + 105;
                    break;
         }
       }

private void Form1_Load(object sender, EventArgs e)
        {
            dateTimePickerwidth(dateTimePicker1, dateTimePicker1.Format.ToString());
        }



Run the Application and See the Result.
Happy Coding!