Click here to Skip to main content
15,908,906 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
I have a Textbox with an event of Leave that fires query

I want to know how to check if a button is click so that when I ever I click the button the textbox event won't fire the Query

I've tried using a MouseHover event for the button and a boolean but the textbox event still execute

sorry if I didn't add my code



On my Textbox Leave event
VB
Dim chkBtnStatus As Boolean

Private Sub TxtboxLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtBox.Leave
If chkBtnStatus Then
    Dim MainMenu As New MainForm
    Me.Close
    MainMenu.Show
    Exit Sub
End If
RunQuery()
End Sub


Where
VB
Public Sub RunQuery()

Select familyName From Contacts Where FirstName = '" & Textbox.Text & "'
//Lets jump to the result skipping the command and datareader codes
FamilyNameLbl.Text = DataRead(0).ToString

End Sub


Inside my Button.Click Event
VB
chkBtnState = True


The above codes works perfectly fine
I guest my Plan won't work
Leave event will always fire
Posted
Updated 20-Jul-13 0:57am
v3
Comments
Sergey Alexandrovich Kryukov 20-Jul-13 0:52am    
The whole idea is wrong. They are not checked, events are handled...
—SA

The textbox event will always fire before the button click event. There is no way around this, since the textbox loses focus before the button gains it, its just the order of things.

Maybe using the leave event for the textbox isn't the best way to do what you are trying to do, since you didn't tell us what your desired function is (why use the Leave on textbox), nor did you show us any code, its really almost impossible to help you with a solution around what you want to do.
 
Share this answer
 
Comments
iMaker.ph 20-Jul-13 7:01am    
I also Tried making the chkBtnStatus to True on Button MouseHover Event but still no luck :( any idea about this ?
Ron Beyer 20-Jul-13 9:33am    
I think you missed my point, the Leave event will always be fired before the button sets the flag, this is because Leave is raised on the Textbox before the Click event is raised on the button. There is no way to change this behavior, flags or not. The user would have to click the button before typing in the textbox, not after. You need to find a different way of doing what you want without using the leave event for running your query.

BTW, you are VERY susceptible to injection attacks. If you want to see why, back-up your database, type in your textbox: a;'DROP TABLE Contacts;-- and see what happens.
You make a class variable of type Boolean. In the button's click event you set this variable to true. Or you could disable the button after it has been clicked the first time.

VB
Public Class form1
  Private hasBeenClicked As Boolean
  Private Sub btn_Clicked(...) Handles btn.Click
    If Not hasBeenClicked Then
       'do query
    End If
    hasBeenClicked = True
  End Sub
...
End Class
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900