Link to home
Start Free TrialLog in
Avatar of javagair
javagairFlag for United States of America

asked on

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?

gary
Avatar of ChloesDad
ChloesDad
Flag of United Kingdom of Great Britain and Northern Ireland image

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?
Avatar of 13Shadow
13Shadow

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?
ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of javagair

ASKER

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.

gary
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
button2.focus()
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.
Avatar of kaufmed
@13Shadow

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
        btnSecond.PerformClick()
        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

P.S.

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))
        return;
      this.ResetFlagsandPaint();
      this.OnClick(EventArgs.Empty);
    }

Open in new window


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

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.
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.
James,

It was just an example by Kaufmed to prove a point, its not a solution to the actual question.