8


DateTimePickerのテキスト部分をカバーする1行のテキストボックス(Tb)でDateTimePickerをオーバーレイするユーザーコントロールがあります。テキストボックスTb()が固定されています(LTRB)。

テキストボックスでオーバーレイされたDatepicker

この問題は、ユーザーコントロールのサイズが変更されたときに発生します。
オーバーレイされたdatetimepickerは、テキストTbの後ろに表示されます。

オーバーレイされたdatetimepickerは、緑色のテキストボックスTbの後ろに表示されます。

現在、ユーザーコントロールは242(W)x 20(H)です。コントロールのサイズが100(幅)未満になると、問題が発生します。

CustomFormatを""に設定してテキストを空白にしてみましたが、ユーザーコントロールはdatetimepicker機能の多くをサポートしているため、オプションではありませんでした。

ユーザーコントロールで最小サイズを設定しようとしましたが、それも機能しません。

編集:
コントロールはすでにいくつかのアプリケーションで使用されています。これらのアプリケーションでは、コントロールの最小サイズは90X20ですが、現在設定している最小サイズは97x20です。
VSデザイナーは、変更を加えた後、既存のコントロールのサイズを97x20に設定しますか?
この効果を実現するにはどうすればよいですか(97x20にサイズ変更)?

最小サイズを追加した後、****コードを編集します****

namespace WindowsFormsApplication1
{
    partial class CDatePicker
    {
        private System.ComponentModel.IContainer components = null;


        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code


        private void InitializeComponent()
        {
            this.splitContainer1 = new System.Windows.Forms.SplitContainer();
            this.datepanel = new System.Windows.Forms.Panel();
            this.datetxt = new System.Windows.Forms.TextBox();
            this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
            this.timepanel = new System.Windows.Forms.Panel();
            this.timetxt = new System.Windows.Forms.TextBox();
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
            this.splitContainer1.Panel1.SuspendLayout();
            this.splitContainer1.Panel2.SuspendLayout();
            this.splitContainer1.SuspendLayout();
            this.datepanel.SuspendLayout();
            this.timepanel.SuspendLayout();
            this.SuspendLayout();
            // 
            // splitContainer1
            // 
            this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer1.Location = new System.Drawing.Point(0, 0);
            this.splitContainer1.Name = "splitContainer1";
            // 
            // splitContainer1.Panel1
            // 
            this.splitContainer1.Panel1.Controls.Add(this.datepanel);
            this.splitContainer1.Panel1MinSize = 105;
            // 
            // splitContainer1.Panel2
            // 
            this.splitContainer1.Panel2.Controls.Add(this.timepanel);
            this.splitContainer1.Size = new System.Drawing.Size(236, 20);
            this.splitContainer1.SplitterDistance = 178;
            this.splitContainer1.SplitterWidth = 1;
            this.splitContainer1.TabIndex = 0;
            // 
            // datepanel
            // 
            this.datepanel.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.datepanel.Controls.Add(this.datetxt);
            this.datepanel.Controls.Add(this.dateTimePicker1);
            this.datepanel.Location = new System.Drawing.Point(0, 0);
            this.datepanel.Margin = new System.Windows.Forms.Padding(0);
            this.datepanel.Name = "datepanel";
            this.datepanel.Size = new System.Drawing.Size(175, 20);
            this.datepanel.TabIndex = 0;
            // 
            // datetxt
            // 
            this.datetxt.BackColor = System.Drawing.Color.Gainsboro;
            this.datetxt.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.datetxt.Location = new System.Drawing.Point(0, 0);
            this.datetxt.MinimumSize = new System.Drawing.Size(60, 20);
            this.datetxt.Name = "datetxt";
            this.datetxt.Size = new System.Drawing.Size(71, 20);
            this.datetxt.TabIndex = 3;
            this.datetxt.Text = "date";
            // 
            // dateTimePicker1
            // 
            this.dateTimePicker1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.dateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Short;
            this.dateTimePicker1.Location = new System.Drawing.Point(0, 0);
            this.dateTimePicker1.Margin = new System.Windows.Forms.Padding(0);
            this.dateTimePicker1.MinimumSize = new System.Drawing.Size(65, 20);
            this.dateTimePicker1.Name = "dateTimePicker1";
            this.dateTimePicker1.Size = new System.Drawing.Size(175, 20);
            this.dateTimePicker1.TabIndex = 2;
            this.dateTimePicker1.Value = new System.DateTime(2012, 11, 15, 0, 0, 0, 0);
            // 
            // timepanel
            // 
            this.timepanel.Controls.Add(this.timetxt);
            this.timepanel.Dock = System.Windows.Forms.DockStyle.Fill;
            this.timepanel.Location = new System.Drawing.Point(0, 0);
            this.timepanel.Margin = new System.Windows.Forms.Padding(0);
            this.timepanel.Name = "timepanel";
            this.timepanel.Size = new System.Drawing.Size(57, 20);
            this.timepanel.TabIndex = 0;
            // 
            // timetxt
            // 
            this.timetxt.Dock = System.Windows.Forms.DockStyle.Fill;
            this.timetxt.Location = new System.Drawing.Point(0, 0);
            this.timetxt.Name = "timetxt";
            this.timetxt.Size = new System.Drawing.Size(57, 20);
            this.timetxt.TabIndex = 1;
            this.timetxt.Text = "time";
            this.timetxt.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            // 
            // CDatePicker
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.AutoSize = true;
            this.Controls.Add(this.splitContainer1);
            this.MaximumSize = new System.Drawing.Size(236, 20);
            this.MinimumSize = new System.Drawing.Size(100, 20);
            this.Name = "CDatePicker";
            this.Size = new System.Drawing.Size(236, 20);
            this.splitContainer1.Panel1.ResumeLayout(false);
            this.splitContainer1.Panel2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
            this.splitContainer1.ResumeLayout(false);
            this.datepanel.ResumeLayout(false);
            this.datepanel.PerformLayout();
            this.timepanel.ResumeLayout(false);
            this.timepanel.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.SplitContainer splitContainer1;
        private System.Windows.Forms.Panel datepanel;
        private System.Windows.Forms.TextBox datetxt;
        private System.Windows.Forms.DateTimePicker dateTimePicker1;
        private System.Windows.Forms.Panel timepanel;
        private System.Windows.Forms.TextBox timetxt;
    }
}


編集:最小サイズを追加した後は見栄えが良くなりますが、コントロールのサイズを最小に変更してプロジェクトを再構築すると、カレンダーのドロップダウンが消えます。
また、テキストボックスはカレンダーボタンの近くに固定されたままになりません。

編集:上記のために追加された写真..カレンダーが消えます。

日付カレンダーが消える

編集:ああ、日付フィールドと時間フィールドの両方が折りたたみ可能であるというもう1つのことを見逃しました。そのため、分割コンテナがあります。最小サイズの計算もこれに注意する必要があります(時間フィールドの可視性に応じて、2つの異なる最小サイズを推測します)。

質問:

a)上記の問題を回避するために、ユーザーコントロールが特定の最小制限を超えてサイズ変更されないようにするにはどうすればよいですか?(pic1はアイデアサイズのようです)

b)テキストが空白になり、
カレンダーのドロップダウンボタンのみが表示されるようにdatetimepickerを描画できますか?もしそうなら、私はまだサイズ変更イベントを処理する必要がありますか?

c)DTPの日付テキスト部分を常にカバーするようにテキストボックスのサイズを変更し続けるにはどうすればよいですか。

4

2 に答える 2

4

単純な問題と難しい問題があります。単純な問題はスクリーンショットから明らかです。DateTimePickerのサイズが正しく変更されていません。ドロップダウンボタンが右側でクリップされていることに注意してください。UserControlは奇妙すぎて問題を正確に特定できませんが、これはdatePanelだと思います。Dock.Fillの代わりにAnchor.Rightを使用しています。

難しい問題は、ドロップダウンボタンが、DTPコンテンツのレンダリングに使用できるスペースの量に基づいて動的にサイズ変更されることです。少なくともWindows7では、以前のバージョンは異なるレンダリング戦略を使用しています。残念ながら、VisualStylesAPIはボタンの実際のサイズを返しません。これに対処する唯一の適切な方法は、テキストボックスがアイコンを覆うのに十分な大きさであることを確認して、ドロップダウン矢印のみが表示されるようにすることです。

UserControlで多くのことを行うことはできません。DateTimePickerクラスから派生し、その中にTextBoxを埋め込む、はるかに単純なソリューションを提案します。また、実行時にはるかに安くなります。

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using System.Runtime.InteropServices;

class MyDateTimePicker : DateTimePicker {
    private TextBox editbox;
    private int buttonWidth;

    public MyDateTimePicker() {
        editbox = new TextBox();
        editbox.BorderStyle = BorderStyle.None;
        editbox.BackColor = Color.Gold;   // debugging
        this.Controls.Add(editbox);
    }

    public override Font Font {
        get { return base.Font; }
        set { base.Font = editbox.Font = value; }
    }

    protected override void OnResize(EventArgs e) {
        if (buttonWidth == 0) measureButtonWidth();
        var margin = (this.ClientSize.Height - editbox.PreferredHeight) / 2;
        editbox.Location = new Point(margin, margin);
        editbox.Width = this.ClientSize.Width - margin - buttonWidth;
        base.OnResize(e);
    }

    private void measureButtonWidth() {
        if (!Application.RenderWithVisualStyles) buttonWidth = 21;   // TODO: measure
        else {
            var renderer = new VisualStyleRenderer("DATEPICKER", 3, 1);
            using (var gr = CreateGraphics()) {
                buttonWidth = renderer.GetPartSize(gr, ThemeSizeType.True).Height;
            }
        }
    }

    protected override void Dispose(bool disposing) {
        if (disposing) editbox.Dispose();
        base.Dispose(disposing);
    }
}

OnResizeオーバーライドは、TextBoxを適切なサイズに保つように処理します。editbox.Textプロパティを処理するために必要なコードを追加します。考慮されていない唯一の詳細は、ビジュアルスタイルが無効になっている場合のドロップダウンボタンのサイズです。最近では珍しいですが、それでも可能です。マシンのテーマをオフにし、ハードコードされたサイズを微調整して外観に一致させます。

于 2012-04-07T15:18:27.127 に答える
0

DateTimePickerをテキストボックスでオーバーレイしても、良い結果が得られるとは思いません。サイズ変更とフォーカスの問題は常に発生します。

私はこれらのオプションの1つを使用します:

  • DevExpressPopupContainerEditのようなサードパーティのコントロールを使用します。次に、標準のWindowsフォームMonthCalendarをポップアップし、必要に応じて表示されるテキストをカスタマイズできます。Telerikなどからも同様のコントロールが利用できると思います。
  • コードプロジェクトのような場所から同様のドロップダウンコンテナコントロールを使用またはカスタマイズします。インスピレーションとして使用できるものが少なくとも2つあります。

  • コードプロジェクトで私のカラーピッカーのドロップダウン部分を適応させることもできます

柔軟性を高める最後のオプションは、WindowsフォームからWPFに移行することです。このようなカスタムコントロールの作成は非常に簡単です(とにかく急な学習曲線の後で)。

于 2012-04-08T21:48:08.877 に答える