简体   繁体   中英

How to set font size in DateTimePicker

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)));

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);
    }
}

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)));

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM