Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I am trying to use the following code to call a check on leaving the form.

 

 

Private Sub frmEditField_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

 

If intChanged = 1 Then

e.Cancel = True

btnCancel_Click()

End If

End Sub

 

For some reason it wont let me use the 'btnCancel_Click()' line, (i get the very familar blue line underneath), however when i create a new procedure and call that from both the 'btnCancel_Click()' code and thefrmEditField_closing - it works fine.

 

I would imagine the reason why it will call 1 procedure and not the other, is because i need something at the top, such as:

 

dim CancalClosing as ......................

 

Could you please tell me what is this, and how can i find these out easily?

 

Thanks.

Posted

When running a procedure within a button event the event expects 2 things one is the sender and the other is event args by adding these you can run the code.

 

' The sender is set as me and for the EventArgs you create new ones
btnCancel_Click(Me, New EventArgs)

 

I hope this helps

 

Andy

Code today gone tomorrow!
  • Leaders
Posted

:-\ all you need is PerformClick(), eg:

   Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
       Button1.PerformClick()
   End Sub

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       MessageBox.Show("closing")
   End Sub

Posted

That Works Perfectly.........Thanks.

 

However it has thrown up an error in my code.

 

-------------------------------------------------------------------------------

 

Private Sub frmEditField_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

 

If intChanged = 1 Then

e.Cancel = True

btnCancel_Click(Me, New EventArgs)

End If

End Sub

 

------------------------------------------------------------------------------

 

Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click

Dim Response As MsgBoxResult

 

If intChanged = 1 Then

Response = MsgBox("Do you wish to leave without saving the field " _

+ txtFieldName.Text, 36)

If Response = MsgBoxResult.Yes Then

intChanged = 0

Me.Close()

End If

Else

DataState = 1

Me.Close()

End If

End Sub

 

-------------------------------------------------------------------------------

 

When i am closing the form if intChanged = 1, it cancels the close, then calls the Cancel button correctly, it then runs through the cancel button code perfectly - however when it hits the me.close i want it to exit, where it doesnt - it continues through the cancel code - then goes back to the form_closing event and through that. It wont actually close - however next time i go to close it closes straight away - as intChanged = 0.

 

How can i make it close when the user clicks yes?

 

Thanks.

  • Leaders
Posted

you want something like this then?

   Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
       If MessageBox.Show("do you want to save your changes before quiting", Me.Name, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
           e.Cancel = True '/// dont unload
       Else
           e.Cancel = False '/// unload
       End If
   End Sub

  • *Experts*
Posted

I think you may have it working now, but I would suggest this anyway:

Don't put any of the "if closing..." type code in the Cancel button's click event. Normally a cancel button will have just Me.Close() and the closing event will do the logic (usually just what dynamic showed).

 

Also, it makes handling the "X" a lot easier since all the code is in one spot.

 

-Ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

Nerseus...........Yeah i have done it now, but thats a really good point - i didnt think about that. I still have several other forms to do, and i will remember that for those.

 

Thanks again.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...