4

I want to increase font size in DateTimePicker in Window Form C#. I want to set minimum 14 to 16 font size in DateTime picker.

I have tried below code but it's not working.

dateTimePicker1.CalendarFont = new Font("Courier New", 8.25F, FontStyle.Italic, GraphicsUnit.Point, ((Byte)(0)));
Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
Brijesh Shah
  • 41
  • 1
  • 3
  • Welcome to SO! Please always use a language tag like C# when applicable, and avoid very general tags like `size` and `set` unless they are fundamental. – Ciro Santilli OurBigBook.com Dec 14 '13 at 08:43
  • The font size is fixed by the visual styles theme selected by the user. You'd have to remove the `Application.EnableVisualStyles();` statement in your Main method. Which is usually where that ends. – Hans Passant Dec 14 '13 at 13:53
  • Possible duplicate of : http://stackoverflow.com/questions/16031370/increase-font-size-of-datetimepicker-calender-in-win7-aero-theme – Shachaf.Gortler Jan 02 '14 at 06:24

3 Answers3

6

If you wish to retain visual styles for the other controls in your application, but disable for the date picker's drop down only, you can use the following code:

public class MyDateTimePicker : DateTimePicker
{
    [DllImport("uxtheme.dll", ExactSpelling = true, CharSet = CharSet.Unicode)]
    static extern Int32 SetWindowTheme(IntPtr hWnd, String textSubAppName, String textSubIdList);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr GetParent(IntPtr hWnd);

    [DllImport("user32.dll", SetLastError = true)]
    static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

    protected override void OnDropDown(EventArgs eventargs)
    {
        if (Application.RenderWithVisualStyles)
        {
            const int DTM_GETMONTHCAL = 0x1008;

            //Get handle of calendar control - disable theming
            IntPtr hCalendar = SendMessage(this.Handle, DTM_GETMONTHCAL, IntPtr.Zero, IntPtr.Zero);
            if (hCalendar != IntPtr.Zero)
            {
                SetWindowTheme(hCalendar, "", "");

                //Get handle of parent popup - resize appropriately
                IntPtr hParent = GetParent(hCalendar);
                if (hParent != IntPtr.Zero)
                {
                    //The size you specify here will depend on the CalendarFont size chosen
                    MoveWindow(hParent, 0, 0, 400, 300, true);
                }
            }
        }

        base.OnDropDown(eventargs);
    }
}
pcbbc
  • 81
  • 1
  • 3
  • This works great - Much better than just throwing away visual styles for the entire application, which seems to be the suggested fix elsewhere. – edhubbell May 03 '17 at 18:07
-1

In main program delete / comment line Application.EnableVisualStyles();

and add new line of code after yours:

dateTimePicker1.Font = new Font("Courier New", 8.25F, FontStyle.Italic, GraphicsUnit.Point, ((Byte)(0)));
George Brighton
  • 5,131
  • 9
  • 27
  • 36
  • 1
    Attention!!! Commenting out Application.EnableVisualStyles() may cause some undesired visual results!!! Please check out this before directly applying : http://stackoverflow.com/questions/6764100/does-application-enablevisualstyles-do-anything – curiousBoy Sep 12 '14 at 22:21
-1

In iOS u have to make a renderer:

private void SetFont(CustomPicker view)
{
    UIFont uiFont;
    Control.Font = UIFont.SystemFontOfSize(11f); //the size which u want

}

In android you have to set the font for the renderer:

private void SetFont(CustomDatePicker view)
{
    if (view.Font != Font.Default) 
    {
        Control.TextSize = view.Font.ToScaledPixel();
        Typeface font = Typeface.CreateFromAsset(Forms.Context.Assets,"Roboto-Bold.ttf");
        Control.Typeface = font;
        Control.Typeface = view.Font.ToTypeface();
    }
}
T3 H40
  • 2,326
  • 8
  • 32
  • 43