Show help button when text box is selected (C#)

Asked

Viewed 43 times

1

I’ve been 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 it in the c#?

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

And this is how it should look if the box wasn’t selected, but I don’t know how to do it

  • You are using WPF/Winforms/UWP?

1 answer

2


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!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.