3

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?

YOhan
  • 505
  • 3
  • 6
  • 17
  • try use button.visible=false in another control enter event not in button control.So this button visible change when another control got focus – Sathish Sep 03 '14 at 06:36
  • Actually there's a ton of other controls and user could focus on any of those. Making events of every field is not really OK. – YOhan Sep 03 '14 at 06:39
  • use the **textBox1.GotFocus** and **textBox1.LostFocus** – a d Sep 03 '14 at 06:39
  • @MehdiKhademloo It works exactly the same. – YOhan Sep 03 '14 at 06:44
  • If you always need the click button event, then hide the button after button clicked. – Abdul Ahad Sep 03 '14 at 06:44
  • User could ignore that button and continue to another control where I must hide button also. Hiding on click could never happen and therefore it would be visible always. – YOhan Sep 03 '14 at 06:49
  • Have you thought about making the button visible only while there is a value in it? It seems like you are wanting a user input into the textbox. – nickm Sep 03 '14 at 07:15
  • Not really, because what that button would do is open another form from which I could select specific text for `textBox2`. User should be able to open that form independently of `textBox2`'s value. – YOhan Sep 03 '14 at 07:21

4 Answers4

2

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");
}
petelids
  • 12,305
  • 3
  • 47
  • 57
  • You're the **MAN**! It works as expected without additional controls or events on every other field. I now recall that I've tried something with `Control.Focused`, but failed miserably. Your answer works like a charm. Thank you! – YOhan Sep 03 '14 at 11:23
1

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;
}
nickm
  • 1,775
  • 1
  • 12
  • 14
  • `Control.CanFocus` does not have a setter. And GotFocus/LostFocus works the same as Enter/Leave - click doesn't fire. – YOhan Sep 03 '14 at 06:46
  • 1
    Apologies, was confusing it with a WPF property. – nickm Sep 03 '14 at 06:49
  • No prob. I'm marking this answer as useful. I don't know anything about WPF, but if CanFocus does what I think it does (if true, focus does not change in program and is left where it was), then it should work in my opinion. – YOhan Sep 03 '14 at 06:58
  • About that edit: as I said in another answer - user could also ignore that button and do nothing with it, so `button3_Click` event could be ignored. – YOhan Sep 03 '14 at 06:58
0

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

Naveed Butt
  • 2,861
  • 6
  • 32
  • 55
  • Sorry, I can't do that, because task is to show/hide button on textbox focus/blur. – YOhan Sep 03 '14 at 06:40
  • You can set a flag on Focus of the text box and then use that in the Panel's Hover event – Naveed Butt Sep 03 '14 at 06:46
  • What about user tabbing to control? Mouse could be completely out of the form in that case. So, hover is not an option, too. – YOhan Sep 03 '14 at 06:53
  • In that case you might need to use `GotFocus` and `LostFocus` events for the Panel – Naveed Butt Sep 03 '14 at 06:57
  • Panel can't get focus, because it is a container. Answer given in following question has implementation of focus, but that's much more effort than delaying button hiding. http://stackoverflow.com/a/3562449/1745228 – YOhan Sep 03 '14 at 07:17
0

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.
Amol
  • 918
  • 7
  • 20