(C#)DateTimePickerのCustomFormat指定について

川端 修 20 評価のポイント
2024-05-07T05:14:33.8266667+00:00

C#でWindowsFormの開発を行っています。

DateTimePickerで月日入力をさせたく、Formatプロパティに「Custom」、CustomFormatプロパティに「MM/dd」を指定しました。

デザイナ上でカレンダー表示アイコンが全て表示されるように幅を調整しているのですが、実行するとアイコンの表示位置が右にずれてしまい「▼」ボタンが枠外にはみ出て表示が隠れてしまいます。

調べてみると幅が97を下回ると、どんなに短い表示にしていても必ず「▼」ボタンが枠外にはみ出てしまうようでした。

アイコンの表示位置をデザイナで表示された位置から動かさない方法は無いものでしょうか?

(デザイン時)

デザイン時

(実行時)

実行時

開発環境

VisualStudio2017

.NetFramework4.7.2

.NET
.NET
.NET ソフトウェア フレームワークに基づく Microsoft テクノロジ。
32 件の質問
C#
C#
C 言語ファミリをルーツとし、コンポーネント指向プログラミングのサポートを含む、オブジェクト指向およびタイプセーフのプログラミング言語。
4 件の質問
0 件のコメント コメントはありません
{count} 件の投票

承認済みの回答
  1. gekka 6,926 評価のポイント MVP
    2024-05-07T08:56:18.7333333+00:00

    DateTimePickerが幅の計算をミスしているようなので、再計算させてみる。

    using System;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Windows.Forms;
    using System.Collections.Generic;
    using System.ComponentModel;
    
    public partial class Form1 : Form
    {
        private NumericUpDown numericUpDown1;
        private DateTimePicker dateTimePicker1;
        private CheckBox chkSwitchFormat;
        private CheckBox chkEnableSizeUpdate;
    
        public Form1()
        {
            this.dateTimePicker1 = new DateTimePicker()
            {
                Format = DateTimePickerFormat.Custom,
                CustomFormat = "MM/dd",
                Left = 5,
                Top = 5,
                Width = 90,
            };
            this.dateTimePicker1.HandleCreated += DateTimePicker_HandleCreated;
    
            this.Controls.Add(this.dateTimePicker1);
    
            this.numericUpDown1 = new NumericUpDown()
            {
                Left = 5,
                Top = dateTimePicker1.Bottom + 5,
                Minimum = 1,
                Maximum = 100,
                Value = 90
            };
            this.numericUpDown1.ValueChanged += numericUpDown1_ValueChanged;
            this.Controls.Add(this.numericUpDown1);
    
            this.chkSwitchFormat = new CheckBox() { Left = this.numericUpDown1.Left, Top = numericUpDown1.Bottom + 5, Text = "フォーマット切り替え", AutoSize = true, Appearance= Appearance.Button  };
            this.chkSwitchFormat.Click += button1_Click;
            this.Controls.Add(chkSwitchFormat);
    
            this.chkEnableSizeUpdate = new CheckBox() { Left = this.numericUpDown1.Left, Top = chkSwitchFormat.Bottom + 5, Text = "不具合回避処理を有効にする", AutoSize = true, Checked = true  };
            this.Controls.Add(chkEnableSizeUpdate);
        }
    
        private void DateTimePicker_HandleCreated(object sender, EventArgs e)
        {
            //DateTimePickerが作られたか再構築されたら、大きさをちょっと変えてサイズ依存の状態を更新させる
            this.BeginInvoke(new Action(() =>
                    {
                        if (!chkEnableSizeUpdate.Checked)
                        {
                            return;
                        }
    
                        this.dateTimePicker1.Width++;
                        this.dateTimePicker1.Width--;
                        this.dateTimePicker1.Update();
                    }));
        }
    
        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            this.dateTimePicker1.Width = (int)this.numericUpDown1.Value;
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            if (this.dateTimePicker1.Format == DateTimePickerFormat.Custom)
            {
                this.dateTimePicker1.Format = DateTimePickerFormat.Time;
            }
            else
            {
                this.dateTimePicker1.Format = DateTimePickerFormat.Custom;
                this.dateTimePicker1.CustomFormat = "MM/dd";
            }
        }
    }
    
    
    1 人がこの回答が役に立ったと思いました。

0 件の追加の回答

並べ替え方法: 最も役に立つ