dazzy Posted October 27, 2009 Posted October 27, 2009 (edited) Hi All, Firstly, I am new to inheritance, but understand the concept (probably at quite a simply level though). I am having a problem with closing a form which I have inherited. I have a form called frmAddForm_Simple, which contains 4 main controls (A Data Grid, A Save Button, A Close Button and a Text Box). The idea around the form is to create a form which could be used for inputting of many different types of data which are all entered in the same way (i.e. to add a new row to a database for [status] table or [Location] table, all of which contain 2 columns in the database (an [id] and a [value]). On the form I have a property called _FormChanged, which when the textbox text changes, sets the property to True. I then have the following code on the Close Button:- Public Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click If _FormChanged Then If MessageBox.Show("Are you sure you want to close and discard any changes?", "Discard Changes", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then Me.Close() End If End If End Sub The form is opened as a dialog (ShowDialog) and when I go to close the inherited form (by click on btnClose) and answer No to the above MessageBox – the form closes anyway. Using the Debugged I have worked out that the following events occur in sequence (all of which runs in the Master Form mentioned above):- 1) btnClose_Click sub Routine runs 2) I answer No to the question – do I want to close the form 3) The debugger steps over the Me.Close() line (as expected) 4) The debugger jumps up to the Sub Initialise (where ShowDialog was called from) and closes the form. So at no point has the debugger even ran a line of code which reads Me.Close(), but the form closes anyway – so I’m fairly confused as to why the form should close. If anyone could give me some pointers as to where I might be going wrong, I would much appreciate it. It’s obviously something I’m not understanding about inheritance – but whatever I read about on the subject, I seem go over the same things which (I think) I already understand, but I guess the light bulb hasn’t quite come on yet! Many Thanks in advance for your time:) . My Master Form (frmAddForm_Simple) Code:- =================================================== Public Class frmAddForm_Simple Private _FormChanged As Boolean = False Private _FormLoading As Boolean = False Public Sub Initialise() _FormLoading = True StandardiseForm(Me) SetupForm() _FormLoading = False ShowDialog() End Sub Public Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click If _FormChanged Then If MessageBox.Show("Are you sure you want to close and discard any changes?", "Discard Changes", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then Me.Close() End If End If End Sub Public Overridable Sub SetupForm() End Sub Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click SaveForm() End Sub Public Sub SaveForm() End Sub Private Sub SetFormStatus() _FormChanged = True If tData.Text.Trim <> "" Then btnSave.Enabled = True Else btnSave.Enabled = False End Sub Private Sub tData_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tData.TextChanged If Not _FormLoading Then SetFormStatus() End If End Sub End Class My Inherited (Child) Form Code:- Public Class frmAdmin_PersonTitle Private Shadows _FormChanged As Boolean = False Public Overrides Sub SetupForm() lDataTitle.Text = "PersonTitle" Dim Data As New PersonTitle modGeneral.BindAndSetupGrid(grdData, Data.DataTable_Get(), Queries.PersonTitle.DefaultView()) End Sub Public Shadows Sub SaveForm() Dim Data As New PersonTitle With Data.PersonTitleList .PersonTitleId.Value = 0 .PersonTitle.Value = tData.Text.Trim End With If Not Data.UpdateCreate() Then MessageBox.Show("Failed to update/create new Person Title, please contact support", "Failed to Save", MessageBoxButtons.OK, MessageBoxIcon.Error) End If _FormChanged = False SetupForm() End Sub End Class Edited October 27, 2009 by dazzy Quote
Administrators PlausiblyDamp Posted October 27, 2009 Administrators Posted October 27, 2009 Does the button in question have a DialogResult assigned to it in the properties window? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Leaders snarfblam Posted October 27, 2009 Leaders Posted October 27, 2009 If you add an event handler to the Closing event (or override the OnClosing method) you should be able to check the e.CloseReason. That may help explain why it is closing. You can also set a breakpoint here and look at he call stack (try enabling "Show External Code"). You may see the which function is calling Close() in there. Quote [sIGPIC]e[/sIGPIC]
dazzy Posted October 28, 2009 Author Posted October 28, 2009 Hi PlausiblyDamp/marble_eater Thanks both for replying and the suggestions!! I believe that I have resolved the issue - but I'm not sure as to why (what I have done) has resolved the issue. I originally had the Cancel attribute of the master for set to btnClose. Whilst debugging this issue, I reset this property to none. Last night, I decided to delete and recreate the button and now everything works as I would expect!! This morning, I set the Cancel property again to btnClose (just to make sure I was correct in my analysis) and the same thing happen (even when I reset the property - it still caused the form to close). -------------------------------------------------------------------- PlausiblyDamp - No the DialogResult was originally set to Cancel (which I guess was a result of changing the Forms Cancel property) - but it's been set to None during my testing- good suggestion though!:) ---------------------------------------------------------------------- marble_eater - I have just tried that approach (I'd not thought of that one:)) and the Reason is "None" for both the Master forms closing event as well as the overridden one on the child form - Weird!! Thanks!! Quote
BrettW Posted November 13, 2009 Posted November 13, 2009 Another way to get the msgbox result is: Select Case(MsgBox("Are you sure you want to close and whatever...", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Are You Sure...")) Case MsgBoxResult.Yes me.close case MsgBoxResult.No End Select OR: Dim result As MsgBoxResult result = (MsgBox("Are you sure you want to close and whatever...", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Are you sure?")) If result = MsgBoxResult.Yes Then Me.Close() :):):):):) 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.