Appear help button when the text box is selected (C #)

1

I'm looking for a while about how to put a help button that only appears next to the text box when it is selected. Is there any way to do this in C #?

So it should be when the text box is selected, that is, the "i" would appear to provide some information on how to fill that particular text box

Andthisishowitshouldlookiftheboxwasnotselected,butIdonotknowhowtodoit

    
asked by anonymous 04.12.2018 / 18:08

1 answer

2

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!

    
04.12.2018 / 18:37