Solved

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

Posted on 2014-01-03
13
1,690 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 74

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 74

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now