hi all,

I am to pass two values from form f1 to form f2.i wrote a code on keypress event .It works good.But on loading of f2 i want to close f1.I wrote this on f1

Dim f As New frmattendance
f.Owner = Me
f.Show()
f.txttdays.Text = days
f.l2.Text = dtp.Text

on f2 load event

Me.Owner.Close()


Also tried this
Dim f As New frmmonth
f.Close()

Dani AI

Generated

Short diagnosis: setting the child form as an owned form and then closing its owner is exactly why both windows disappeared. An owned form is closed automatically when its owner is closed. Also, in the VS2003-style startup model the process ends when the startup form’s message loop ends, so closing that form will usually terminate the app. set the Owner and then tried to close the owner from Form2; ’s note about “shutdown mode” applies to newer VB project frameworks (VS2005+), not the VB2003 startup model.

Three practical fixes (pick one that matches your app):

  1. Hide the parent instead of closing it and pass data via properties
  • Expose simple properties on the child, show the child, and hide the parent so the program keeps running but the parent is no longer visible.
' child form
Public Class DetailForm
  Public Property DaysCount As Integer
  Public Property DisplayDate As DateTime

  Private Sub DetailForm_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    lblDays.Text = DaysCount.ToString()
    lblDate.Text = DisplayDate.ToShortDateString()
    If Owner IsNot Nothing Then Owner.Hide()
  End Sub
End Class

' parent (before showing child)
Dim d As New DetailForm()
d.DaysCount = someDays
d.DisplayDate = someDate
d.Show()
  1. Control application lifetime with Sub Main / ApplicationContext (VB2003)
  • If you really need to close the startup form and keep other forms alive, start the app from Sub Main and run a custom ApplicationContext so you decide when the app exits. This avoids depending on the startup form’s lifetime.
  1. Use an event to request the parent close itself
  • Let the child raise an event (RequestCloseParent) and have the parent handle that event and decide whether to Hide or Close. This keeps responsibility with the parent and avoids the owner/owned pitfalls.

Quick cautions:

  • Don’t set Owner if you expect the child to survive the parent closing.
  • If you Close the startup form in VB2003 without a custom context, the app will stop.

Recommended Answers

All 4 Replies

If you are using vb 2005 then you don't need to dim a new form just load the second form. Just make sure that the "Shutdown" in the Projects is set to "When last form closes". If it is set to "When startup form closes" then as soon as you close form1 then the program ends.

frmattendance.txttdays.Text = days
        frmattendance.l2.Text = dtp.Text
        frmattendance.Show()
        Me.Close()

If you are using vb 2002 or 2003 try this. I don't have either but this should work.

Dim f As New frmattendance
        f.txttdays.Text = days
        f.l2.Text = dtp.Text
        f.Show()
        Me.Close()

Hi

I Have already tried the code as I am using VB.net 2003.But it was closing both the forms

Did you check the shutdown mode?

hi friends
I solved this problem.

I define
f.owner=me

then On f2 form closing

me.owner.close
me.close

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.