为什么 TextBox 的大小被截断了?
Why is the size of the TextBox cut off?
我写了一个小控件,允许从标签更改为文本框,反之亦然,一切都很好,问题是它在从标签到文本框时切换,这很奇怪,因为我在测试时一切都很完美。
https://i.imgur.com/yo2tz9O.gif
using System;
using System.Windows.Forms;
namespace LabelBox
{
public partial class labelbox : Label
{
public TextBox textBox = new TextBox();
public labelbox()
{
InitializeComponent();
textBox.LostFocus += TextBox_LostFocus;
textBox.KeyDown += TextBox_KeyDown;
this.Controls.Add(textBox);
textBox.Hide();
textBox.Visible = false;
this.AutoSize = false;
}
// Sobrescribir el metodo Double Click de la clase Label
protected override void OnDoubleClick(EventArgs e)
{
textBox.Show();
textBox.Visible = true;
textBox.Text = this.Text;
textBox.Focus();
}
// Agreagar el metodo Lost Focus del textbox
protected void TextBox_LostFocus(object sender, EventArgs e)
{
this.Text = textBox.Text;
textBox.Hide();
textBox.Visible = false;
}
// Agregar el metodo Key Down para ENTER del textbox
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
this.Text = textBox.Text;
textBox.Hide();
textBox.Visible = false;
}
}
}
}
我什至尝试将文本框的大小修改为与标签相同。
在构造函数中设置 this.AutoSize = false;
不会禁用 属性 并且自动调整大小仍然存在。 Label 控件(作为 CheckBox 和 RadioButton)的 ToolBoxItem
属性的类型是 AutoSizeToolboxItem which enables the AutoSize
property when you drop an instance in the designer. This issue is well explained .
如参考答案所示,您可以通过装饰
class 具有 [ToolboxItem(typeof(ToolboxItem))]
属性:
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
using System.ComponentModel;
[ToolboxItem(typeof(ToolboxItem))]
public partial class LabelBox : Label
{
//...
}
或者,将 AutoSize
属性 覆盖为始终 return false
:
public partial class LabelBox : Label
{
[Browsable(false),
Bindable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
EditorBrowsable(EditorBrowsableState.Never)]
public override bool AutoSize => false;
}
由于评论,为 CheckBox 控件实现了 。
建议:您可以按如下方式重写您的自定义控件:
[ToolboxItem(typeof(ToolboxItem))]
public class LabelBox : Label
{
public LabelBox() : base() { }
// To be able to set the properties of the TextBox in the Properties Window.
[Browsable(true),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public TextBox TextBox { get; } = new TextBox();
protected override void OnHandleCreated(EventArgs e)
{
if (!Controls.Contains(TextBox))
{
TextBox.Leave += (s, a) =>
{
Text = TextBox.Text;
TextBox.Hide();
};
TextBox.KeyDown += (s, a) =>
{
if (a.KeyCode == Keys.Enter)
{
Text = TextBox.Text;
TextBox.Hide();
// Optional to prevent the beep...
a.SuppressKeyPress = true;
}
};
TextBox.Visible = false;
TextBox.Dock = DockStyle.Fill;
Controls.Add(TextBox);
}
base.OnHandleCreated(e);
}
protected override void OnDoubleClick(EventArgs e)
{
TextBox.Show();
TextBox.Text = Text;
TextBox.Focus();
}
// When you create a disposable object, then you should dispose it.
protected override void Dispose(bool disposing)
{
if (disposing) TextBox.Dispose();
base.Dispose(disposing);
}
}
旁注:
- 在此上下文中调用
InitializeComponent();
方法毫无意义。
- 要切换控件的可见性状态,请使用
Visible
属性 或 Show/Hide
方法,但不能同时使用这两种方法来做同样的事情。
我写了一个小控件,允许从标签更改为文本框,反之亦然,一切都很好,问题是它在从标签到文本框时切换,这很奇怪,因为我在测试时一切都很完美。
https://i.imgur.com/yo2tz9O.gif
using System;
using System.Windows.Forms;
namespace LabelBox
{
public partial class labelbox : Label
{
public TextBox textBox = new TextBox();
public labelbox()
{
InitializeComponent();
textBox.LostFocus += TextBox_LostFocus;
textBox.KeyDown += TextBox_KeyDown;
this.Controls.Add(textBox);
textBox.Hide();
textBox.Visible = false;
this.AutoSize = false;
}
// Sobrescribir el metodo Double Click de la clase Label
protected override void OnDoubleClick(EventArgs e)
{
textBox.Show();
textBox.Visible = true;
textBox.Text = this.Text;
textBox.Focus();
}
// Agreagar el metodo Lost Focus del textbox
protected void TextBox_LostFocus(object sender, EventArgs e)
{
this.Text = textBox.Text;
textBox.Hide();
textBox.Visible = false;
}
// Agregar el metodo Key Down para ENTER del textbox
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
this.Text = textBox.Text;
textBox.Hide();
textBox.Visible = false;
}
}
}
}
我什至尝试将文本框的大小修改为与标签相同。
在构造函数中设置 this.AutoSize = false;
不会禁用 属性 并且自动调整大小仍然存在。 Label 控件(作为 CheckBox 和 RadioButton)的 ToolBoxItem
属性的类型是 AutoSizeToolboxItem which enables the AutoSize
property when you drop an instance in the designer. This issue is well explained
如参考答案所示,您可以通过装饰
class 具有 [ToolboxItem(typeof(ToolboxItem))]
属性:
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
using System.ComponentModel;
[ToolboxItem(typeof(ToolboxItem))]
public partial class LabelBox : Label
{
//...
}
或者,将 AutoSize
属性 覆盖为始终 return false
:
public partial class LabelBox : Label
{
[Browsable(false),
Bindable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
EditorBrowsable(EditorBrowsableState.Never)]
public override bool AutoSize => false;
}
由于评论,为 CheckBox 控件实现了
建议:您可以按如下方式重写您的自定义控件:
[ToolboxItem(typeof(ToolboxItem))]
public class LabelBox : Label
{
public LabelBox() : base() { }
// To be able to set the properties of the TextBox in the Properties Window.
[Browsable(true),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public TextBox TextBox { get; } = new TextBox();
protected override void OnHandleCreated(EventArgs e)
{
if (!Controls.Contains(TextBox))
{
TextBox.Leave += (s, a) =>
{
Text = TextBox.Text;
TextBox.Hide();
};
TextBox.KeyDown += (s, a) =>
{
if (a.KeyCode == Keys.Enter)
{
Text = TextBox.Text;
TextBox.Hide();
// Optional to prevent the beep...
a.SuppressKeyPress = true;
}
};
TextBox.Visible = false;
TextBox.Dock = DockStyle.Fill;
Controls.Add(TextBox);
}
base.OnHandleCreated(e);
}
protected override void OnDoubleClick(EventArgs e)
{
TextBox.Show();
TextBox.Text = Text;
TextBox.Focus();
}
// When you create a disposable object, then you should dispose it.
protected override void Dispose(bool disposing)
{
if (disposing) TextBox.Dispose();
base.Dispose(disposing);
}
}
旁注:
- 在此上下文中调用
InitializeComponent();
方法毫无意义。 - 要切换控件的可见性状态,请使用
Visible
属性 或Show/Hide
方法,但不能同时使用这两种方法来做同样的事情。