Jay1b Posted September 12, 2003 Posted September 12, 2003 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. Quote
Mehyar Posted September 12, 2003 Posted September 12, 2003 Are you calling the btnCancel_Click with its arguments?? Quote Dream as if you'll live forever, live as if you'll die today
a_jam_sandwich Posted September 12, 2003 Posted September 12, 2003 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 Quote Code today gone tomorrow!
Leaders dynamic_sysop Posted September 12, 2003 Leaders Posted September 12, 2003 :-\ 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 Quote
Jay1b Posted September 12, 2003 Author Posted September 12, 2003 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. Quote
Leaders dynamic_sysop Posted September 12, 2003 Leaders Posted September 12, 2003 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 Quote
Jay1b Posted September 12, 2003 Author Posted September 12, 2003 Dynamic - that looks a lot neater then mine and it works! Thank you. Quote
*Experts* Nerseus Posted September 12, 2003 *Experts* Posted September 12, 2003 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 Quote "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
Jay1b Posted September 12, 2003 Author Posted September 12, 2003 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.