I wrote a small control that allows to change from label to text box and vice versa, and everything is fine, the problem is that it cuts when going from label to text box, which is strange because while I was testing it everything worked perfectly.
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;
}
}
}
}
I even tried modifying the size of the text box to be the same as the label.