Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cancel Form Problem

Status
Not open for further replies.

Crystalyzer

Technical User
Mar 28, 2003
218
0
0
I have a form with a textbox and a button. The form is NOT modal. I have the following code:
Code:
Private Sub textbox1_LostFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles textbox1.LostFocus
        If Not textbox1.Text = "" Then
            LookupCompany()
        Else
            MessageBox.Show("Please enter a company", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            textbox1.Focus()
        End If
    End Sub

AND

Code:
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click

        ActiveForm.Dispose()
    End Sub

My problem is that when I click on button1 I want the form to close without the textbox1 lostfocus event occurring. Is there a way to do this?

Any advice appreciated. Thanks.


Thanks and best regards,
-Lloyd
 
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
textbox1.CausesValidation = False
ActiveForm.Dispose()
End Sub
 
3DDD,

Thanks for the reply, but that doesn't work for some reason. The first sub is NOT a validating event its a LostFocus event. Would that make a difference to your solution?



Thanks and best regards,
-Lloyd
 
How about this: Set up an IsDisposing variable initialized to false at the form level. In the Dispose override, add a line that sets the variable to true. Then evaluate the variable in the LostFocus event.

HTH

Bob
 
Bob,

Thanks for the reply. I was able to accomplish what I wanted by using the following code in the textbox1_LostFocus event handler
Code:
Private Sub textbox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles textbox1.LostFocus
        If Button1.Focused = False Then
            If textbox1.Text <> "" Then
                LookupCompany(conn)
            Else
                MessageBox.Show("Please enter a company.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                textbox1.Focus()
            End If
        End If
    End Sub

Works like a charm. My "validation" works no matter how the focus is lost on textbox1 UNLESS Button1 is clicked with the mouse.


Thanks and best regards,
-Lloyd
 
.Focused, huh? Who knew? I'm still new to .net, and I like that, much cleaner than vb6's activecontrol stuff.

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top