Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 174
  • Last Modified:

Keeping the reference to a form

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
AntoniL
Asked:
AntoniL
1 Solution
 
AlexFMCommented:
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
 
AntoniLAuthor Commented:
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
 
AntoniLAuthor Commented:
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
Technology Partners: 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!

 
bob_onlineCommented:
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
 
AlexFMCommented:
Do you use the same instance of Class1 in subsequent calls from exe?
0
 
CimperialiCommented:
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
 
AntoniLAuthor Commented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now