Show button only if focus is in text box

Is it possible to display a button in a Windows Form only when the focus is in a specific text field?

Tried it with this approach:

    private void button3_Click(object sender, EventArgs e)
    {
        MessageBox.Show("OK");
    }

    private void textBox2_Enter(object sender, EventArgs e)
    {
        button3.Visible = true;
    }

    private void textBox2_Leave(object sender, EventArgs e)
    {
        button3.Visible = false;
    }

      

No luck because the button click doesn't work because the button is hidden right after the textbox loses focus, preventing it from firing button3_Click(/*...*/) { /*...*/ }

.

Now I do it like this:

    private void button3_Click(object sender, EventArgs e)
    {
        MessageBox.Show("OK");
    }

    private void textBox2_Enter(object sender, EventArgs e)
    {
        button3.Visible = true;
    }

    private void textBox2_Leave(object sender, EventArgs e)
    {
        //button3.Visible = false;
        DoAfter(() => button3.Visible = false);
    }

    private async void DoAfter(Action action, int seconds = 1)
    {
        await Task.Delay(seconds*1000);
        action();
    }

      

The form now waits for the second and only then hides button3

.

Is there a better approach?

+3


source to share


4 answers


I think you only want to display the button when the focus is on a specific textbox or the focus is on the button.

For this you can check the property Focused

button3

on the event Leave

textBox2

and only hide the button if the button has no focus. Note that the button will focus before the event occurs Leave

textBox2

.

Then you need to hide the button in the script where it button3

loses focus and focus moves to a different location besides textBox2

. You can use the exact same method here, handling the event Leave

button3

and only hiding button3

if textBox2

it has no focus.



The following code should suit your requirements:

private void textBox2_Leave(object sender, EventArgs e)
{
    if (!button3.Focused)
    {
        button3.Visible = false;
    }
}

private void button3_Leave(object sender, EventArgs e)
{
    if (!textBox2.Focused)
    {
        button3.Visible = false;
    }
}

private void textBox2_Enter(object sender, EventArgs e)
{
    button3.Visible = true;
}

private void button3_Click(object sender, EventArgs e)
{
    MessageBox.Show("Button clicked");
}

      

+2


source


Why not work with the GotFocus and LostFocus events of the TextBox?

private void textBox2_GotFocus(object sender, EventArgs e)
{
    button3.Visible = true;
}

      



Then hide the button in the click event.

private void button3_Click(object sender, EventArgs e)
{
    MessageBox.Show("OK");
    button3.Visible = false;
}

      

+1


source


How about adding a panel and placing a button and text boxes on that panel, and when the user MouseHover

on that panel displays the button ...

This way the user will be able to click on the ...

This is the event you are looking for I think ... http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousehover(v=vs.110).aspx

UPDATE:

var textboxFocussed = false;        
private void textBox2_Enter(object sender, EventArgs e)
{
    textboxFocussed = true;
}

private void textBox2_Leave(object sender, EventArgs e)
{
    textboxFocussed = false;
}

      

UPDATE 2

private void Panel_GotFocus(object sender, EventArgs e)
{
    button3.Visible = textboxFocussed;
}

private void Panel_LostFocus(object sender, EventArgs e)
{
    button3.Visible = false;
}

      

Below is information about the Events Panel

0


source


you can add an Enter event handler for all controls on the Load form . Just remember to skip the controls where you want the button to appear.

    List<string> strControlException = new List<string>();

    public Form1()
    {
        InitializeComponent();
        strControlException.Add("btnMain");
        strControlException.Add("txtMain");
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        for (int i = 0; i < this.Controls.Count;i++ )
        {
            if (!strControlException.Contains(Controls[i].Name))
            {
                Controls[i].Enter += new EventHandler(hideButton);
            }
        }
    }

    private void txtMain_Enter(object sender, EventArgs e)
    {
        btnMain.Visible = true;
    }

    private void hideButton(object sender, EventArgs e)
    {
        btnMain.Visible = false;
    }

      

btnMain (the button you want to manipulate) and txtMain (which controls the ability of the button) are the controls in the competition here

Add additional controls to validate the form.

Explanation for the above code:

  • First, initialize the list with the names of the controls that should render the button
  • In form loading, add an event handler to all controls (except one in our list)
  • In the handler function, hide the button. (You might want to do more logic here based on the control that called this function)
  • The button is hidden by default and only in the text box Enter the event we are showing.
0


source







All Articles