why do I need to hit one button twice on vb.net form

I have two buttons on is hidden and one visiable
the first one calls a database and fills in personnel data.   Then hides itself and makes visible one that allows filling of a datagridview of the month.  Two make this button actually do something I need to click on it twice or it will do nothing.  this button is set to follow the first in tab order.
Why does the program seem to disregard the first click but accept the second?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Which button do you need to click twice, the first, or the second?

have you put a break point in the click event to see when the event is happening?

Does the button look like you are pressing it?
My guess is that maybe the datagridview gets the focus due to something in your code and then it goes to the button??  It is hard to tell with the little information provided. When you make the button visible, do you also setfocus to it?
Jacques Bourgeois (James Burger)PresidentCommented:
Since the second button is hidden when you hide the first one, it could not receive the focus and would logically skip the second button and go the next control.

But as is often the case, logic does not do any good here. If you test, you will see that when you hide a control, the focus does not go the next one in the tab order. It goes back to the control that has its tab index set to 0.

What I would try is to first make sure that there is not code in the control that has TabIndex 0 what could do something with the focus. This could be the culprit.

And since the second button would not automatically receive the focus no matter what, I would for a Select on it with something like the following:


This would implement the Tab order as you intended it, and since the button does have the focus, the first click should activate it.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

javagairAuthor Commented:
been a long time since I programed anything is vb.  I assumed it had something to do with focus but did not see a property for that.
will try the  .select idea.

when I put a break in the second button code is the only time I do not have to click button twice it seems to go right to the break point.  If I don't set a break point I have to click the button twice to fill the datagridview.  
I know that seems weird, it does to me.

It sounds like its either the code is taking a long time to execute, and by putting the break point you are masking this. Its certainly nothing to do with the focus, as the click event will fire when the button is pressed whether it has the focus or not.

Can you post the code that is inside the two click events.
'Its certainly nothing to do with the focus'????   Your statement is incorrect.
If you create a quick vb program with a datagridview and 2 buttons one visible and one not visible and in the visible button click event you make itself non visible and make the second button visible, the focus goes to the datagridview first not the second button. Unless you hit the tab button (or enter button if you have it setup to act like tab) the focus will not go to the second button. If you set the focus to the second button as you make it visible you will only have to hit enter once:
button2.visible = true
Click events have absolutely nothing to do with where the focus is on the form. Hence my statement.

If there is code in the got focus event of the data grid then that's a different issue, and without the full source code we cant tell that at the moment. As the click event fires when the  breakpoint is added then a second click would only cause the click event to fire again.
The issue could be that the click event is not triggered the first time OP hits the Enter key because the button does not have focus, thus it is not active thus no click event.....  
It could also be that the OP has some code in the click event that makes it not do what it is supposed to do the first time.
There isn't enough information.
All I am saying is that you certainly cannot say that the click event has nothing to do with the focus. The default click event will not fire if the control doesn't have focus.
kaufmed   ( ⚆ _ ⚆ )Patches? We Ain't Got No Patches! We Don't Need No Patches! I Don't Have to Push You No Stinkin' Patches!Commented:

The default click event will not fire if the control doesn't have focus.
Please explain the following code then.

Public Class Form1

    Private Sub btnFirst_Click(sender As Object, e As EventArgs) Handles btnFirst.Click
        MessageBox.Show(String.Format("First Button is focused? {0}", btnFirst.Focused))
    End Sub

    Private Sub btnSecond_Click(sender As Object, e As EventArgs) Handles btnSecond.Click
        MessageBox.Show(String.Format("Second Button is focused?  {0}", btnSecond.Focused))
    End Sub
End Class

Open in new window

kaufmed   ( ⚆ _ ⚆ )Patches? We Ain't Got No Patches! We Don't Need No Patches! I Don't Have to Push You No Stinkin' Patches!Commented:

For your reference, here is the decompiled code for the PerformClick method:

    /// <summary>
    /// Generates a <see cref="E:System.Windows.Forms.Control.Click"/> event for a button.
    /// </summary>
    /// <filterpriority>2</filterpriority><PermissionSet><IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/><IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/></PermissionSet>
    public void PerformClick()
      bool validatedControlAllowsFocusChange;
      if (!this.CanSelect || (this.ValidationCancelled || !this.ValidateActiveControl(out validatedControlAllowsFocusChange) && !validatedControlAllowsFocusChange))

Open in new window

Although it is in C#, the last code line should reassure you.

You are assuming that the OP pressed enter to fire the click event on the button. I am assuming that they physically clicked on the button using a mouse, as this is what they said that they did in the OP.
Jacques Bourgeois (James Burger)PresidentCommented:
Just a gut feeling, nothing I can really explain, but I would not personnally trigger a Click event from Inside of a Click event. Specially when there is a MessageBox in the second one, because if opens as a Dialog and thus deactivate the underlying form.

I would create a method that is called by both events. The same action would then be performed no matter if you click on Button1 or Button2, but only one click would be triggered.

It was just an example by Kaufmed to prove a point, its not a solution to the actual question.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.