问题描述
我写了一个小控件,它允许从标签更改为文本框,反之亦然,一切都很好,问题是从标签更改为文本框时它会剪切,这很奇怪,因为在我测试所有内容时效果很好。
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;
}
}
}
}
解决方法
在构造函数中设置In [4]: env.make_instance('(ent0-0 of ENTITY-CLASS (text "Bruce Springsteen")(confidence 1.0)(type PER))')
Out[4]: Instance: [ent0-0] of ENTITY-CLASS (text "Bruce Springsteen") (confidence 1.0) (type PER)
不会禁用该属性,并且保持自动调整大小。 Label控件的this.AutoSize = false;
属性的类型(如CheckBox和RadioButton)为AutoSizeToolboxItem,当您在设计器中放置实例时,它将启用ToolBoxItem
属性。 here对该问题进行了很好的解释。
如所提及的答案所建议,您可以通过装饰
具有AutoSize
属性的类:
[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
:
false
由于注释而为CheckBox控件实现了here。
建议:您可以按以下方式重写自定义控件:
public partial class LabelBox : Label
{
[Browsable(false),Bindable(false),DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),EditorBrowsable(EditorBrowsableState.Never)]
public override bool AutoSize => false;
}
旁注:
- 在这种情况下调用
[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
方法,但不能同时使用同一方法。