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:-
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:-
===================================================
My Inherited (Child) Form Code:-
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:-
Code:
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:-
===================================================
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:-
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
Last edited: