The solution is to create an extension of the TextBox
control and add the button to it. Then just subscribe 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; };
}
}
}
No Form
just use this control instead of the native, and voila!