2012-04-05 247 views
7

我有一个用户控件,其中有一个DateTimePicker覆盖了覆盖DateTimePicker的文本部分的单行文本框(Tb)
。文本框Tb(绿色)被锚定(L T R B)。隐藏DateTimePicker的文本和用户控件调整大小

Datepicker overlaid with textbox

的问题时,用户控件大小来。
叠加的datetimepicker出现在文本Tb后面。

The overlaid datetimepicker shows up behind the green textbox Tb.

目前用户控制242(W)×20(H)。 当控件的大小调整到100(宽度)以下时出现问题。

我试图将CustomFormat设置为“”的文本留空,但用户控件支持许多datetimepicker功能,因此这不是一个选项。

我试图在用户控件上设置最小大小,但这不起作用。

编辑:
的控制已在使用的一些applications.In这些应用的最小尺寸 对照的是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; 
    } 
} 

编辑:加分大小后,它看起来更好,但压延下拉消失
当控件大小以分钟和项目重建..
此外,文本框不会停留在日历按钮附近..

编辑:pic添加为above..calender消失。

date calender disappears

编辑:哦,我错过了一两件事,这两个日期字段和时间字段是collapsible.thats为什么分割容器是存在的。最小尺寸的计算也必须认识到这一点(我猜2个不同的分大小取决于时间字段的可见性。)

问题:

一)如何防止用户控件调整大小超过一定最小限度以避免上述问题? (图片1似乎是想法的大小)

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

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

+0

你是什么意思*“我试图在用户控件上设置最小尺寸,但这不起作用”*?它应该强制控制仅调整到一定的最小限制。什么没有工作? – alex 2012-04-05 05:54:21

+0

从Visual Studio设计者我已经设置了用户控件的最小尺寸..但是当控件放在窗体上时,最终用户可以将该最小尺寸更改为(0,0),然后控件可以在最小尺寸(如你在第二张图中看到的) – Amitd 2012-04-05 06:07:22

+0

我可以问为什么你在DateTimePicker(dtp)上覆盖一个文本框控件,我没有深入研究控件,但我可以猜测dtp会是一个带下拉的文本框日历控制。我可能是错误的,但它似乎你可能想让你自己的用户控制脱离dtp控制 - 使用反射或dotpeek在System.Windows.Forms dll – 2012-04-05 06:11:59

回答

3

你有一个简单的问题和一个难题。简单的问题在截图中很明显。 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受到覆盖照顾保持文本框大小合适的。添加任何代码来处理editbox.Text属性。唯一没有注意的细节是禁用视觉样式时下拉按钮的大小。罕见的这些日子,但仍然有可能。关闭机器上的主题并调整硬编码的大小,使其与外观相匹配。

+0

+1谢谢我会检查并回复你。 – Amitd 2012-04-08 09:12:01

+0

thx @Hans Passant ...我结束了你的方法..更简单,更容易理解。一点混搭。 – Amitd 2012-04-11 18:21:35

+0

我发现另一个有趣的事情是这个SO问题http://stackoverflow.com/questions/154543/panel-dock-fill-ignoring-other-panel-dock-setting – Amitd 2012-04-11 18:35:48

0

我不认为你会得到一个很好的结果通过覆盖DateTimePicker与文本框。总会有调整大小和焦点问题。

我会用其中一个选项:

  • 使用第三方控制像DevExpress的PopupContainerEdit。然后,您可以弹出标准的Windows Forms MonthCalendar并根据需要自定义显示的文本。我确信类似的控件可以从Telerik等获得。
  • 使用或自定义类似代码项目的类似下拉容器控件。有至少两个,你可以为灵感使用:

  • 你甚至可以适应降我colour picker下部分的代码项目

最后一个选项会给你更多的灵活性,从Windows窗体转移到WPF。像这样创建自定义控件是非常容易的(无论如何,在陡峭的学习曲线之后)。