CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2003
    Posts
    322

    How to check if a form is visible,without invoking form_load

    Is there any straightforward way from keeping form_load from being invoked, when refernecing Form.visible?

    I was surprized to find out that form_load() gets called if the form was never shown, and I check form.visible.

  2. #2
    Join Date
    Jun 2005
    Location
    Orissa
    Posts
    150

    Re: How to check if a form is visible,without invoking form_load

    Hi,
    Check for the previous instance of the application.

    Thanks

  3. #3
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: How to check if a form is visible,without invoking form_load

    Who said he was calling the app again?


    If you don't want code to fire, but it in Form_Activate, but include a flag that checks whether Form_Visible is True before it fires..

    I hide forms that I'll need again, but only clear all the variables if a pending calculation isn't needed. The pending calc sets the flag.

    If it doesn't need to display a total, _Activate sets the form to all zeroes. If it is a total, then the code doesn't fire.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  4. #4
    Join Date
    Dec 2001
    Posts
    6,332

    Re: How to check if a form is visible,without invoking form_load

    The first thing I thought of is If Not Form1 Is Nothing Then... but oddly it isn't ever Nothing, even after setting it to Nothing. I've been using Set Form1 = Nothing when I want to get a form to completely release memory, because there have been cases where variables wouldn't return to the default state. Since that seems to work it made sense that the form would be Nothing, but apparently not. So I guess I'm gonna have to reinvestigate that.

    Well, I suppose you could have a Boolean for each form in a module that gets set when the form loads and unloads.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: How to check if a form is visible,without invoking form_load

    If you refer to a Form, then it will be loaded into memory, even if it wasn't.
    That causes problems if you try to unload a form, and then refer to it again.
    It gets loaded again.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Sep 2001
    Location
    Québec, Canada
    Posts
    1,923

    Re: How to check if a form is visible,without invoking form_load

    One thing you can do is to browse the forms collection and see if your form is loaded, if it is loaded, then you can check the visible property. Another option would be to create your form with variables, for example, instead of calling "form2.show", you create a variable "private frm2 as form2" for example. That way, you will have more control on the form (you will be able to set it to nothing That way, you can easily set the variable to "nothing" when it is unloaded.

    JeffB
    CodeGuru VB FAQ Visual Basic Frequently Asked Questions
    VB Code color Tool to color your VB code on CodeGuru
    Before you post Importants informations to know before posting

  7. #7
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: How to check if a form is visible,without invoking form_load

    Hey Guys !
    I think its totally easy in most cases.
    do the following
    Code:
    Private Function IsLoaded(sFormName as string) as Boolean
    Dim oForm as Form
    For each oForm in Forms
    If oForm.Name = sFormName then
    	 IsLoaded = True
    	 exit Function
    End if 
    Next
    End Function
    This way you find out if a defined form is loaded
    wich is very useful in many cases. If you have more Forms with the same name then you can add them a number property ( or using the Tag Property ) too and checking for that numer also
    Code:
    Private Function IsLoaded(sFormName as string, lgIndex as long) as Boolean
    Dim oForm as Form
    For each oForm in Forms
    If oForm.Name = sFormName then
    	 If oForm.Tag = lgIndex then
    		 IsLoaded = True
    		 exit Function
    	 End if 
    End if 
    Next
    End Function
    so its not needed to load a form to check for Visible
    Code:
    Private Function IsVisible(sFormName as string) as Boolean
    Dim oForm as Form
    For each oForm in Forms
    If oForm.Name = sFormName then
    	 If oForm.Visible = True then
    		 IsVisible = True
    		 exit Function
    	 End if 
    End if 
    Next
    End Function
    Hope this helps

    Jonny Poet
    Last edited by JonnyPoet; March 2nd, 2006 at 03:24 PM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured