YOhan
YOhan

Reputation: 523

Show button only when focus is on textbox

Is this possible to display button on Windows Form only when focus is on specific textbox?

Tried that 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 button click does not work then, because button is hidden immediately after textbox lost focus, preventing it from firing button3_Click(/*...*/) { /*...*/ } event.

Now I'm doing it like that:

    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();
    }

Form now waits for a second and only then hides button3.

Is there any better approach?

Upvotes: 3

Views: 1287

Answers (4)

petelids
petelids

Reputation: 12815

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

To do this you can check the Focused property of button3 in the Leave event of textBox2 and only hide the button if the button doesn't have focus. Note that the button will get focus before the Leave event of textBox2 fires.

You will then need to hide the button in the scenario where button3 loses focus and the focus moves to somewhere other than textBox2. You can use exactly the same technique here by handling the Leave event of button3 and only hiding button3 if textBox2 does not have focus.

The following code should fit 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");
}

Upvotes: 2

Amol
Amol

Reputation: 918

you can add Enter event handler for all controls on form at Load. Just make sure to skip the controls on which you want to show the button.

    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 (Button you want to Manipulate) and txtMain (Which controls the vibility of the button) are the controls in contention here

Add more controls on the form to test.

Explanation for the above code :

  1. First initialize a list with the names of controls that should show the Button
  2. On Form Load add an Event handler to all controls (except the one in our list)
  3. In the handler function hide the button. (You might want to perform more logic here based on the control that called this function)
  4. Button is hidden by default and only on textbox Enter event we show the button.

Upvotes: 0

Naveed Butt
Naveed Butt

Reputation: 2901

How about you add a Panel and place the button and text boxes in that panel and when user MouseHovers that Panel then display the button...

This way user would be able to click on the button...

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;
}

Here are the details of the Panel Events

Upvotes: 0

nickm
nickm

Reputation: 1775

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

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

Then hide the button on the click event.

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

Upvotes: 1

Related Questions