Results 1 to 5 of 5

Thread: Controls initializing during form_load

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2007
    Posts
    448

    Controls initializing during form_load

    Sorry to bother everybody with this one.

    Years ago, I remember running into this problem, but I have checked prior projects and my own tuts to find the solution and I can't find it.

    The problem is this. A form initializes on app startup. In the process, vb.Net seems to behave differently than vb6 in how it initializes the controls on the form. In vb6, it seemed that controls were not initialized automatically until I invoked them with something like:
    Code:
    Textbox1.text = "blah-blah-blah"
    That would fire my textbox_change procedure. So in order to prevent all the code in the textbox procedure from executing, just before initializing any of my controls, I would put a flag in my form_load procedure:
    Code:
    FormIsLoading= 1
    Then, any control procedures would start out with the line:

    Code:
    If FormIsLoading = 1 Then Exit Sub
    But in vb.Net this does not seem to work. It seems that .Net initializes a form's controls automatically, and before it executes any code in a form_load procedure. Is this the case? And if so, how do you tell .Net to not execute all the code in a control's procedure? So I thought maybe I could set the flag in the main_form.designer area, since it is a lower level area, and it DOES look like that is where the controls are getting initialized. So I tried it, and it seems to work. But is this a dangerous place to be putting code, and did I just get away with it this time, and maybe setting myself up for disaster in some future app?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Controls initializing during form_load

    In the form constructor there is a call to the InitializeComponent method, which is part of the designer-generated code. It's in that method that all controls are created, initialised and added to the form. Any property values that you set in the designer will be set in that method and any appropriate events will be raised and there event handlers invoked. If you don't want to execute the code in a particular event handler then you have to put a conditional statement in that particular event handler. You do it much as you described: declare a Boolean variable at the class level and test that variable in each of the appropriate event handlers. You toggle that variable at the end of the Load event handler to indicate that initialisation is complete.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2007
    Posts
    448

    Re: Controls initializing during form_load

    OK then, I see what I did. I was thinking old school and declaring a variable separate from initializing it at the same time so that I could put it up there at the class level. So I did what you suggested and put the following up there at the top of the class:

    Code:
    Friend Class MAIN_form
                Inherits System.Windows.Forms.Form
    
                Dim FormIsLoading As Byte = 1
                .
                .
                .
    And that did it. Thanks for your help, jmcilhinney!

    p.s. And I guess, for your run-of-the-mill code...Stay out of the designer area?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Controls initializing during form_load

    Why use a Byte, particularly when I specifically said to use a Boolean? Your variable is supposed to indicate whether the form is loading or not. That's two values: one to indicate positive and one to indicate negative. There's a data type for that. It's Boolean.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2007
    Posts
    448

    Re: Controls initializing during form_load

    DoH!

    I meant to put Boolean.
    Ahh what the heck!...Who cares about memory management anyway!

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