C# 隐藏日期时间选择器';s文本和用户控件调整大小
我有一个用户控件,它有一个日期时间选择器,上面覆盖着单行文本框(Tb)C# 隐藏日期时间选择器';s文本和用户控件调整大小,c#,.net,winforms,user-controls,datetimepicker,C#,.net,Winforms,User Controls,Datetimepicker,我有一个用户控件,它有一个日期时间选择器,上面覆盖着单行文本框(Tb) 覆盖DateTimePicker的文本部分。文本框Tb(绿色)被锚定(L T R B) 调整usercontrol的大小时会出现问题。 覆盖的datetimepicker显示在文本Tb后面 目前,用户控件为242(宽)x20(高)。 当控件的大小调整到100(宽度)以下时,会出现问题 我尝试了空白,CustomFormat将文本设置为“”,但是用户控件支持许多DATETEMPEKER功能,所以这不是一个选项。 我试图设置
覆盖DateTimePicker的文本部分。文本框Tb(绿色)被锚定(L T R B) 调整usercontrol的大小时会出现问题。
覆盖的datetimepicker显示在文本Tb后面 目前,用户控件为242(宽)x20(高)。 当控件的大小调整到100(宽度)以下时,会出现问题
我尝试了空白,CustomFormat将文本设置为“”,但是用户控件支持许多DATETEMPEKER功能,所以这不是一个选项。 我试图设置用户控件的最小大小,但这也不起作用
编辑:该控件已在少数应用程序中使用。在这些应用程序中 控件的大小为90X20,而我们现在设置的最小大小为97x20。
进行更改后,VS designer会将现有控件的大小调整为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;
}
}
编辑:添加最小尺寸后,它看起来更好,但日历下拉列表消失将控件大小调整为最小值并重建项目时..
此外,文本框不会固定在日历按钮附近 编辑:上面添加了图片。日历消失 编辑:哦,我又错过了一件事,日期字段和时间字段都是可折叠的。这就是拆分容器存在的原因。最小尺寸计算也必须意识到这一点。(我想根据时间域的可见性,有两种不同的最小尺寸。) 问题: a) 如何防止usercontrol的大小超出特定的最小限制以避免上述问题?(图1似乎是理想尺寸) b) 我是否可以绘制datetimepicker,以使文本变为空白,而我们只能看到
日历下拉按钮?如果是这样,我还需要处理调整大小事件吗
c) 如何不断调整文本框的大小,使其始终覆盖DTP的日期文本部分 你有一个简单的问题和一个困难的问题。从截图中可以明显看出这个简单的问题。DateTimePicker没有正确调整大小,请注意右侧的下拉按钮是如何被剪裁的。UserControl太奇怪了,无法真正指出问题所在,但我认为它是datePanel。您使用的是锚点。对,而不是Dock.Fill 困难的问题是,下拉按钮根据可用于呈现DTP内容的空间量动态调整自身大小。至少在Windows7上,早期版本使用不同的渲染策略。遗憾的是,VisualStyles api没有返回按钮的实际大小。处理这个问题的唯一得体方法是确保文本框足够大,可以覆盖图标,因此只有下拉箭头可见 我不能对UserControl做太多的工作,我将提出一个更简单的解决方案,它只是从DateTimePicker类派生并在其中嵌入一个文本框。在运行时也要便宜得多:
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属性所需的任何代码。当禁用视觉样式时,唯一未考虑的细节是下拉按钮的大小。现在不常见,但仍然可能。关闭机器上的主题,调整硬编码的大小,使其与外观匹配。我认为用文本框覆盖DateTimePicker不会得到好结果。总是会有调整大小和焦点的问题 我将使用以下选项之一:
- 使用第三方控件,如DevExpress。然后,您可以弹出标准Windows窗体MonthCalendar,并根据需要自定义显示的文本。我相信Telerik等公司也提供类似的控制
- 从类似代码项目的地方使用或自定义类似的下拉容器控件。至少有两种方法可以作为灵感:
- 您甚至可以在代码项目上修改我的下拉部分
最后一个给您带来更多灵活性的选项是从Windows窗体移动到WPF。创建这样的自定义控件要容易得多(无论如何,在经历了陡峭的学习曲线之后)。你说的“我试图在用户控件上设置最小大小,但这不起作用”是什么意思?它应该强制控件只调整到某个最小限制。到底是什么不起作用?在Visual studio designer中,我设置了usercontrol的最小大小。。但是当控件放置在表单上时,最终用户可以将最小大小更改为(0,0),然后将控件的大小调整到最小大小以下(如第二张图片所示)。我可以问一下,为什么要在日期时间选择器(dtp)上覆盖textbox控件吗,我还没有深入研究这个控件,但我可以猜测dtp将是一个带有下拉日历控件的文本框。我可能错了,但似乎您可能想让自己的usercontrol脱离dtp控件-在System.Windows.Forms dllYou上使用reflector或dotpeek,因为dtp会动态地显示其外观,当没有足够的空间时,会将按钮的部分放在右侧。你可以通过pinvoking GetThemBu立()来做点什么。你想做什么?它是否使用DatePicker下拉列表,但具有自定义显示格式?显然DatePicker.CustomFormat对您没有用,否则您不会问这个问题。显示的日期是只读的还是需要通过键盘进行编辑。。。我最终还是遵循了你的方法。。更简单,更容易理解。我发现另一件有趣的事是这个问题