【问题标题】:Hiding DateTimePicker's text and Usercontrol resize隐藏 DateTimePicker 的文本和用户控件调整大小
【发布时间】:2012-04-18 20:55:14
【问题描述】:

我有一个用户控件,它的 DateTimePicker 覆盖了单行文本框 (Tb)
覆盖 DateTimePicker 的文本部分。文本框 Tb(green)被锚定(L T R B)。

调整用户控件大小时会出现问题。
覆盖的日期时间选择器显示在文本 Tb 后面。

当前用户控件为 242(W) x 20(H) 。 将控件的大小调整到 100 (Width) 以下时会出现问题。

我尝试将 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;
    }
}

编辑:添加最小尺寸后,它看起来更好但日历下拉消失了
当控件的大小调整为 min 并重建项目时..
此外,文本框不会在日历按钮附近保持锚定..

编辑:为上面添加的图片..日历消失了。

编辑:哦,我又错过了一件事,日期字段和时间字段都是可折叠的。这就是拆分容器存在的原因。最小尺寸计算也必须意识到这一点。(我猜 2 个不同的最小尺寸取决于时间字段的可见性。)

问题:

a) 如何防止用户控件调整大小超出某个最小限制以避免上述问题? (pic1 似乎是创意大小)

b) 我可以绘制 datetimepicker 以使文本被空白并且我们只看到
日历下拉按钮?如果是这样,我还需要处理调整大小事件吗?

c) 我怎样才能不断调整文本框的大小,使其始终覆盖 DTP 的日期文本部分。

【问题讨论】:

  • “我试图在用户控件上设置最小尺寸,但也不起作用”是什么意思?它应该强制控件仅将大小调整到某个最小限制。究竟是什么不起作用?
  • 从 Visual Studio 设计师那里,我已经在用户控件上设置了最小尺寸.. 但是当控件放置在表单上时,最终用户可以将该最小尺寸更改为 (0,0),然后控件可以调整到最小尺寸以下(如您在第二张图片中看到的)
  • 我能问一下为什么你在 DateTimePicker (dtp) 上覆盖一个文本框控件,我还没有深入研究该控件,但我猜想 dtp 将是一个带有下拉日历控件的文本框。我可能是错的,但似乎您可能想让自己的用户控件脱离 dtp 控件 - 在 System.Windows.Forms dll 上使用反射器或 dotpeek
  • 您遇到了这个问题,因为 DTP 会动态布置其外观,当空间不足时会丢弃右侧按钮的部分内容。你可以通过调用 GetThemeRect() 来做一些事情。
  • 想要达到什么目的?是否使用 DatePicker 的下拉菜单,但有自定义显示格式?显然 DatePicker.CustomFormat 对您没有用,否则您不会问这个问题。显示的日期是只读的还是需要通过键盘编辑?

标签: c# .net winforms user-controls datetimepicker


【解决方案1】:

你有一个简单的问题和一个难题。从屏幕截图中可以明显看出这个简单的问题。 DateTimePicker 未正确调整大小,请注意下拉按钮如何在右侧被剪裁。 UserControl 太奇怪了,无法真正查明问题,但我认为它是 datePanel。您正在使用 Anchor.Right 而不是 Dock.Fill。

难题在于下拉按钮会根据可用于呈现 DTP 内容的空间量动态调整自身大小。至少在 Windows 7 上,早期版本使用不同的渲染策略。遗憾的是,VisualStyles api 不会返回按钮的实际大小。唯一合适的处理方法是确保文本框足够大以覆盖图标,以便只有下拉箭头可见。

我不能对 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 属性所需的任何代码。唯一没有注意的细节是禁用视觉样式时下拉按钮的大小。这些天不常见,但仍有可能。在您的机器上关闭主题并调整硬编码大小,使其与外观相匹配。

【讨论】:

  • thx @Hans Passant ...我最终遵循了您的方法..更简单,更容易理解。有点混搭。
  • 我发现的另一个有趣的事情是这个 SO question stackoverflow.com/questions/154543/…
【解决方案2】:

我认为用文本框覆盖 DateTimePicker 不会获得好的结果。总会有调整大小和焦点的问题。

我会使用以下选项之一:

  • 使用第三方控件,例如 DevExpress PopupContainerEdit。然后,您可以弹出标准的 Windows 窗体 MonthCalendar 并根据需要自定义显示的文本。我确信 Telerik 等提供了类似的控件。
  • 在代码项目之类的地方使用或自定义类似的下拉容器控件。至少有两个你可以用作灵感:

  • 您甚至可以在代码项目中调整我的colour picker 的下拉部分

为您提供更大灵活性的最后一个选项是从 Windows 窗体迁移到 WPF。创建这样的自定义控件要容易得多(无论如何,在陡峭的学习曲线之后)。

【讨论】:

    猜你喜欢
    • 2018-04-26
    • 2010-12-27
    • 1970-01-01
    • 1970-01-01
    • 2011-04-24
    • 2014-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多