The solution is to create an extension of control TextBox
and add the button. Then subscribe to the events GotFocus
and LostFocus
to show or hide the button:
using System.Windows.Forms;
namespace MyUserControls
{
public class InfoTextBox : TextBox
{
protected override void OnCreateControl()
{
base.OnCreateControl();
Button button = new Button()
{
Name = "btnInfo",
Text = "!",
Font = new System.Drawing.Font("Tahoma", 6f),
Size = new System.Drawing.Size(20, this.Height - 4),
Location = new System.Drawing.Point(this.Width - 24, 0),
Margin = new Padding(2),
Visible = false
};
button.Click += (s, e) =>
{
MessageBox.Show("Clicou!!");
};
this.Controls.Add(button);
this.GotFocus += (s, e) => { button.Visible = true; };
this.LostFocus += (s, e) => { button.Visible = false; };
}
}
}
In the Form
just use this control instead of the native and voila!
You are using WPF/Winforms/UWP?
– Kevin Kouketsu