Solved

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

Posted on 2014-01-03
13
1,770 Views
Last Modified: 2014-01-07
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
0
Comment
Question by:javagair
  • 5
  • 3
  • 2
  • +2
13 Comments
 
LVL 15

Expert Comment

by:ChloesDad
ID: 39755067
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?
0
 
LVL 3

Expert Comment

by:13Shadow
ID: 39755087
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?
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 39755732
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:

Button1.Hide
Button2.Visible=True
Button2.Select

This would implement the Tab order as you intended it, and since the button does have the focus, the first click should activate it.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:javagair
ID: 39759843
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
0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 39760085
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.
0
 
LVL 3

Expert Comment

by:13Shadow
ID: 39760473
'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()
0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 39760498
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.
0
 
LVL 3

Expert Comment

by:13Shadow
ID: 39760593
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.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39760615
@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

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39760645
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.
0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 39760650
@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.
0
 
LVL 40
ID: 39760656
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.
0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 39760660
James,

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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

805 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question