Can you close an instance of a Form outside of the Form?

cesarfrancisco

Active member
Joined
Apr 3, 2007
Messages
33
Programming Experience
Beginner
VB.NET:
Option Strict On
Public Class ClothingDataGridView

   Private Sub ClothingDataGridViewl_Load(ByVal sender As System.Object, ByVal e As 
   System.EventArgs) Handles MyBase.Load
           Me.Table2TableAdapter.FillByClothing(Me.HOEXDBDataSet.Table2)
    End Sub

    Public entryform2 As New ClothingForm

    Private Sub Table2DataGridView_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles Table2DataGridView.CellMouseDoubleClick

  If Table2DataGridView.CurrentRow.IsNewRow = True Or e.RowIndex = -1 Then
     Exit Sub
  Else
     entryform2.Show()

                 entryform2.TextBox9.Text = Table2.DataGridView  _
     .Rows(e.RowIndex).Cells(0).Value.ToString   

     entryform2.Table2TableAdapter.FillByCloth(entryform2. _
     HomeExpenseDataSet.Table2, Integer.Parse(entryform2.TextBox9.Text))


     Me.Close()

            End If
      End Sub
End Class
---------------------------------

This is a DataGridView Form where the user can select a row by DoubleClicking on the row. The row's Key is then passed to the Data Entry Form (Clothing) to be filled with the row's data. Purpose: modify or delete a datarow.


The hierarchical order of these form:
Form1 > from this Form all the different Data Entry Forms open (like Shoes, Travel, etc.)
Form2, 3, 4, 5, 6, 7, etc are these Data Entry Forms.
From Forms (2, 3, 4, 5, 6, 7, etc) is possible to open one DataGridView Form corresponding to them. Form2 has one DataGridView, For3 has a DataGridView, and so on.


I would like to be able to close the instance of the ClothingForm (entryform2) created in ClothingDataGridView from Form1.

Is this even possible?
 
Last edited by a moderator:
You should be able to like so:

VB.NET:
   My.Forms.Form3.Close()

And you can open forms from any other forms like so:

VB.NET:
   My.Forms.Form3.Show()
   My.Forms.Form3.Focus()

The my namespace contains a lot of helpful things you should check it out this code is not visible unless you search the entire solution for it in 2005.
 
Last edited:
Nope. It would not do anything. In this case Form3 is an instance of the HClothingForm.

VB.NET:
Option Strict On
 
Public Class HomeExpensesForm '(Form1)
 
 
Private Sub Dental_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Dental.Click
        Closer()
        HDentalForm.Show() '(Form2)
        My.Forms.HClothingForm.Close() '(Form3)
    End Sub
 
End Class
 
Very confusing your description that is. Is this correct, cesarfrancisco:
your MainForm creates DataEntryForm which creates ViewForm. You want to close DataEntryForm from MainForm?

Also as mentioned if your forms are "single instance" (only one of each defined form in use) you should use the Default Forms feature and not create new instances.
 
John,
Yes, it is as you described it. (Note : yes, it is a confusing tale)

The Main form lists the available Data Entry forms (One from 10) for selection.

Each Data Entry form has a DataGridView form in order to find a data row to modify or delete that row.
By the clicking on the row, the Key of that row is passed to the DataEntry form. The key is the way to fill the Data Entry form with the rest of the data of that row. (See code above).
Why is this is an instance of the Data Entry form? Well, is there any other way to pass the Key from DataGridView?

Right, this whole debacle is because there are so many forms. I do not want those, which are not in use, to stay open. Of course the instance can be closed by X.

Again:

Open one Entry Form from the Main Form. Perform data entry.
Choose an other Entry form > this action will close the previous Data Entry form. (There is only one Data Entry Form open at any time).
How to correct or delete a data row ? Open the DataGridView from the Data Entry Form. Choose the row.
Data is passed back to the Data Entry Form (an instance of it - unfortunately - except you have an other idea how to do this).

While all other forms will close, this instance will not, when an other Entry Form is chosen from the Main Form.

Thanks for you interest anyway. I am trying to solve this for a month. Some people say that this is not possible > to close the instance.
 
Last edited:
John,
Yes,it is as you described it. (true: it is a confusing tale)

The main form lists the available Data Entry forms (One from 10) for selection.

Each Data Entry form has a DataGridView form.
By clicking on a row the Key from that row is passed to the DataEntry form > to look at, modify or delete a data row. (See code above).
Why it is an instance of the Data Entry form? Well, is there an other way to pass the Key from an other form (in this case a DataGridView)?

The problem is the large number of forms. I do not want those which are not in use to stay open. Of course the instance can be closed by X.

The logic:
Open one Entry Form from Main Form. Perform data entry.
Choose an other form > this action close the previous Data Entry form ,so there is only one Data Entry Form open at any given time.
To correct or delete a data row > open the DataGridView from the Data Entry Form. Choose the row.
Data is passed to the Data Entry Form (instance- unfortunately- except you have an other idea how to do this).

Now, this instance will not close, when an other Entry Form is chosen from the Main Form.

Thanks for you interest anyway. I am trying to solve this for a month. Some people say that this is not possible > to close the instance.
 
Last edited:
So you are saying MainForm closes one EntryForm if another is chosen, but that will leave an orphaned ViewForm. In that case make the ViewForm owned by the EntryForm. Owned forms closes automatically with the owner. You can either add the child to the parents OwnedForms collection, or set the Owner property for the child, or use the Show method where you set owner as parameter.
 
You're making it more difficult than it is. As described:
VB.NET:
dim f as new form
me.owner=f
f.show
or
VB.NET:
dim f as new form
f.show(me)
or
VB.NET:
dim f as new form
me.addownedform(f)
f.show
 
Public entryform2 As New HClothingForm

Private Sub Table2DataGridView_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles Table2DataGridView.CellMouseDoubleClick
If Table2DataGridView.CurrentRow.IsNewRow = True Or e.RowIndex = -1 Then
Exit Sub
Else
entryform2.Show()
entryform2.TextBox9.Text = Table2DataGridView.Rows(e.RowIndex).Cells(0).Value.ToString
entryform2.Table2TableAdapter.FillByCloth(entryform2.HOEXDBDataSet.Table2, Integer.Parse(entryform2.TextBox9.Text))
entryform2.ToolStripStatusLabel1.Text = " Record is ready to modify"
Me.Close()
End If
End Sub

This code segment is in the DataGridView Form of the HClothingForm. The entryform2 is the instance of the HClothingForm. This form is the problem. I tried your code to insert with the "owner" etc. Nope. It is not doing any good.
 
I asked you earlier: "your MainForm creates DataEntryForm which creates ViewForm. You want to close DataEntryForm from MainForm?" and you said yes. Now you are saying ViewForm creates DataEntryForm. The flow of forms in your application is not clear at all, you should leave no doubt in your next post as for what happens. Use the keywords I have sketched out since they are very generic and easy to distinguish: MainForm, EntryForm, ViewForm. Tell us with these three keywords the exact flow.
 
MainForm: 10 buttons opens 10 EntryForms.
(If an other button is selected all the forms related to a previous button close.)

EntryForm opens 1 ViewForm.
(When Entry Form is closed its ViewForm close.)

If there is need to correct or delete datarow:
ViewForm passes data to an instance of its EntryForm. After the data (Key) is passed to the instance, ViewForm is closed.
((Why is there an instance? -because it is the only way, I know, to pass data to EntryForm from ViewForm.)

In this particular case (when an instance of its EntryForm was opened to (pass data) if an other button is selected in the MainForm, the instance will not close even if the same EntryForm which to this ViewForm belongs is opened again.
What I will have is a new EntryForm and the instance of the EntryForm open. Opening an other EntryForm from the MainForm it will close close this EntryForm and its ViewForm but not the instance of that EntryForm. (Sounds convoluted?)
---------------------------------------------------------------------
Scenario 1

Step 1.
MainForm > EntryFormA > ViewFormA

Step2.
MainForm > EntryFormB
(If EntryFormA is open, it will close together with its ViewForm if that ViewForm is open).

Scenario 2
Step 1.
MainForm > EntryFormA > ViewFormA
(If Datarow is selected for edit or delete in ViewFormA > instance of EntryFormAA open, ViewForm close.) ViewFormA > EntryFormAA
The only way to circumvent this situation is: Let the user look at the ViewForm, notice the Key of the row intended for modification or deletion, then query for it in the EntryFormA.
In this case there is no instance (EntryFormAA) of the EntryForm open. Nevertheless, it seemed practical me to chose a row by clicking.
Definitely I do not want to allow row editing in ViewForm.

Step2.
MainForm > EntryFormB
(If EntryFormA is open, it will close) but if EntryFormAA was not closed by the user , this form remains open, it will not close.
 
Last edited:
I see two options here. The easiest is when user from MainForm wants to take a new path then any forms in previous path is closed first, do this with Application.OpenForms collection:
VB.NET:
    Sub closeAll()
        For i As Integer = Application.OpenForms.Count - 1 To 0 Step -1
            If Application.OpenForms(i).Name <> Me.Name Then
                Application.OpenForms(i).Close()
            End If
        Next
    End Sub
The other option, as I understand from your setup you only use one instance of each form you have designed at any time. This enable you to make use of the default form instance feature, where you don't create New FormWhatever but just call EntryForm.Show(), EntryForm.Hide() etc. Combining this with owned forms for the related ViewForm you can quite easily close the single ViewForm visible by closing the current EntryForm that owns it (even if it's hidden) when a new entry path is choosen from MainForm. The only thing you need in addition here is a class level variable of type Form declared in MainForm, naming it for example "current", then all needed before displaying the new entry path is to call current.Close which also close any open owned ViewForm. Note that you would not close EntryForm in this case when going from EntryForm to ViewForm, you would just Hide it, else you would break the owner reference.
 
Back
Top