Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 7th, 2008, 02:25 AM
Authorized User
 
Join Date: Jan 2008
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default Only one form running in runtime.

hi
i am using vb net (winform).
i have problem with form,
case: i have 1 mdiform which has file - open - employee and file - open - product
i click employee, employee form is appeared and then i clicked product, employee form have to been directly closed without confirmation, and show the product form then reversely click the employee, close the product form and show the employee form.
so means got 1 form running in runtime.

please help me the script. :)
thanks.

 
Old January 7th, 2008, 10:29 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Create a variable in your parent form that holds a reference to the open form. When a new form is going to be opened, simply call the close method on the form in that variable and then set it to the newly opened one.

-Peter
 
Old January 7th, 2008, 09:48 PM
Authorized User
 
Join Date: Jan 2008
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply..
but i still don't understand, sorry still new in vb net..
could you help me for more details?

thanks.

 
Old January 7th, 2008, 10:10 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Here is the basic idea. In your parent form class:

   Dim childForm As System.Windows.Forms.Form

When you open any form:

   If childForm IsNot Nothing
      childForm.Close() 'or whatever is appropriate
   End If
   childForm = New MyChildFormType()
   childForm.Show() 'or whatever is appropriate


-Peter
 
Old January 13th, 2008, 10:09 PM
Authorized User
 
Join Date: Jan 2008
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Peter

sorry, i found some warnings please help me for this
here is the scripts,

Dim frmEmployee As System.Windows.Forms.Form

If frmEmployee IsNot Nothing ' is this means if Employee form is opened?
'Variable 'frmEmployee' is used before it has been assigned a value. A null reference exception could result at runtime.
'i got this warning..
      frmEmployee.Close()
End If
      frmProduct = New MyChildFormType() 'MyChildFormType() --> what is it means?
      'i got this warning, Type MyChildFormType is not defined
      frmProduct.Show()

please help the details :)
thanks again..

 
Old January 13th, 2008, 10:40 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

My mistake on the first line I gave you. You need to set the child form holder variable to Nothing by default.

   Dim childForm As System.Windows.Forms.Form = Nothing

You need to replace "MyChildFormType" with the class type of your child forms. You mention an employee form and a product form, so you could have something like this:

   childForm = New frmEmployee()
or
   childForm = New frmProduct()

Note that I changed the line to use the "childForm" variable as the assigned to variable. The point is that you always create the new form instance in the same variable (childForm) so you can check it to see if there is already one open. Otherwise you'd have lots of test testing each variable for each form type.

The easiest way to solve this is to create a method that opens your forms for you. It can be responsible for handling the open form check and possible closing of it before opening the new form. Something like this should do the trick:
Code:
'Put this at the top of your parent form class code
'This is the placeholder for the single open child form 
'Defaults to Nothing as no form is open by default
Private _childForm As System.Windows.Forms.Form = Nothing  

Private Sub OpenNewForm(newForm As System.Windows.Forms.Form)
    If _childForm IsNot Nothing Then
        _childForm.Close()
    End If
    _childForm = newForm
    _childForm.Show()
End Sub

'Then where ever you need to open the forms (such as menu actions)
OpenNewForm(New frmEmployee())
OpenNewForm(New frmProduct())
  etc...
-Peter
 
Old January 14th, 2008, 09:44 PM
Authorized User
 
Join Date: Jan 2008
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks Peter, it works..
thanks a lot :)






Similar Threads
Thread Thread Starter Forum Replies Last Post
runtime instatiation of form control Baby_programmer C# 2005 1 November 29th, 2007 05:05 AM
How to Move Multiple Control on a form at runtime? sivavenugopal VB How-To 1 January 22nd, 2007 01:49 PM
Form resize at runtime leehambly Access VBA 4 December 21st, 2006 09:35 AM
Running a search on database from form kuznickic Access VBA 3 February 3rd, 2005 06:37 PM
Running a Cross-tab report @ runtime in VB antukin Pro VB Databases 0 August 20th, 2003 03:21 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.