I have a FormMain which has a button on. That button opens FormSendMail which has my usercontrol "ctlMailSender" on it. On that usercontrol I have few textboxes and two buttons. Other button is supposed to send a mail according to the information in textboxes and the other button is supposed to close the form and that's a problem at the moment.
I wanted to make the control to be more versatile (not to always close the form, because sometimes I might want to just go to previous page instead of closing etc.) so I decided that I'll call a delegate method when the close button is clicked.
So here is the code for the form:
And the code for the usercontrol ctlMailSender:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load End Sub ' Property which sets the delegate to session (had problem with this too: ' ' if the delegate was a private variable I got nullreferenceexception' ' when calling it so I decided to put it on session)' Friend Property MyClosingDelegate() As ClosingDelegate Get Return Me.Session("SMClosingDelegate") End Get Set(ByVal value As ClosingDelegate) Me.Session("SMClosingDelegate") = value End Set End Property Friend Delegate Sub ClosingDelegate() ' Cancel button: Clears the session which has delegate and fires the delegate method' Protected Sub imbCancel_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imbCancel.Click Dim vClosingDelegate As ClosingDelegate = Me.Session("SMClosingDelegate") Me.Session("SMClosingDelegate") = Nothing vClosingDelegate() End Sub
So, what am I doing wrong inside the CloseForm() method? Or should I do the whole code in a different way?
If I call:
on the Cancel button event it closes the form. But that would mean that the control would always close the form, I would have to create new control if I wanted to make next ctlMailSender to go back to the previous page instead of closing the form.