?
Solved

Keeping the reference to a form

Posted on 2003-03-27
7
Medium Priority
?
159 Views
Last Modified: 2010-05-01
Hi experts,
I've got an *.exe project that calls a dll. The first task is, from the exe, tell the dll (through its  class) to show up a form (which in turn belongs to this dll). That's alright. But the thing is that when, from the exe, try to open a second form (without first closing the first form), this first form is still seen. How can I hide the first one with code?. I know it sounds easy enough, but take into account that when I click on the exe in order to show the second form, it goes through the class code and I lose the reference to the first one, so a "MyFirstForm.hide" wouldn't work because the dll now doesn't know who MyFirstForm is. A bit of code would clarify the problem ...

In my *.exe:

set Mydll as new DLL1.CLass1

Mydll.ShowForms(1) '1 would mean, show form 1, the 2nd call later on would be (2)

In my Class1:

Private WithEvents NewfrmMyFirstForm As MyFirstForm 'It has to be this way (WithEvents), both forms
Private WithEvents NewfrmMySecondForm As MySecondForm

public sub ShowForms(FormNumber as integer)
   select case FormNumber
       case 1
           Set NewfrmMyFirstForm = New MyFirstForm
           NewfrmMyFirstForm.show
       case 2
           Set NewfrmMySecondForm = New MySecondForm
           NewfrmMySecondForm.show
           NewfrmMyFirstForm.hide  
           'After the second call from the exe, the old reference of NewfrmMyFirstForm is lost, so there is nothing to hide!!
   end select
end sub

Any ideas on how I can keep the old reference and hide the first form so?, any other way to do it?
Cheers
Toni
0
Comment
Question by:AntoniL
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 8217103
Private WithEvents NewfrmMyFirstForm As MyFirstForm
Private WithEvents NewfrmMySecondForm As MySecondForm

public sub ShowForms(FormNumber as integer)
  select case FormNumber
      case 1
          if NewfrmMyFirstForm is Nothing then
              Set NewfrmMyFirstForm = New MyFirstForm
          end if

          NewfrmMyFirstForm.show

          if not NewfrmMySecondForm is Nothing then
              NewfrmMySecondForm.Hide
          end if

      case 2
          if NewfrmMySecondForm is Nothing then
              Set NewfrmMySecondForm = New MySecondForm
          end if

          NewfrmMySecondForm.show

          if not NewfrmMyFirstForm is Nothing then
              NewfrmMyFirstForm.hide  
          end if
  end select
end sub
0
 

Author Comment

by:AntoniL
ID: 8217248
Hi AlexFM,
I've been trying the "is nothing" as well and couldn't. I tried your code and the last bit (the one I'm looking for), ie:

if not NewfrmMyFirstForm is Nothing then
    NewfrmMyFirstForm.hide  
end if

is not correct because I never get into the "if", during my second call NewfrmMyFirstForm is nothing so it doesn't really work.

Any other ideas?
Ta
Toni
0
 

Author Comment

by:AntoniL
ID: 8217288
Actually, if I used NewfrmMyFirstForm.hide, without the conditional "if", it'd have the same effect: the second call wouldn't know who NewfrmMyFirstForm is, so there would be nothing to hide

A bit desperate now

Toni
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Accepted Solution

by:
bob_online earned 750 total points
ID: 8217352
3 things:

1)  is form 2 being displayed modally -- if it is, the form1 hide doesn't execute until form 2 is closed.  Hide form1 before showing form2 will fix it.

However, I think it is more likely that one of these is what you need to do (your references are going out of scope).

2)  Where are you declaring the variable MyFirstForm?  At a minimum, it needs to be a module level global so that it persists as long as the class that creates it does.  Make sure the class is not terminating between showing form 1 and form 2.

3)  Where are you declaring MyDll.  If you are redeclaring it within a procedure, you are creating a second instance of it which knows nothing of form1.  It also needs to be a module level, or prehaps global, variable that you create an instance of ONCE.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 8217366
Do you use the same instance of Class1 in subsequent calls from exe?
0
 
LVL 5

Expert Comment

by:Cimperiali
ID: 8217398
make a try this way:
in your class:
'----------------------------
dim frmX as form
for each frmX in Forms
   frmX.hide
   'or unload if you need
   'unload frmX
next
'and only now:
set frmX = new theoneyouwant
frmx.show
'---------------------------
0
 

Author Comment

by:AntoniL
ID: 8218653
Hi guys,
thanks for all your responses, but after checking out my code I realised I was using different instances of the same dll, so, as bob_online said (3rd point), it's impossible to keep track of my forms from one call to another. I reckon bob_online is the winner, so my points are yours. Thanks to you all experts!!

Toni
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question