Results 1 to 4 of 4

Thread: Writing Event code for controls that don't exist at design time.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2002
    Location
    St. Louis MO
    Posts
    129

    Writing Event code for controls that don't exist at design time.

    How do you write event procedures for controls that don't exist on your form at design time?
    I have a program where controls aren't added until run time and I need to be able to detect with one has got focus, mouse clicked, etc...

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    Which method do you use to create the run-time controls? Based on your question I will assume Controls.Add.

    You need to declare a variable using WithEvents. Once you write the declaration for your variable, its name will appear in the dropdown list of available controls, allowing you to add the event handlers (like you would for any other control on a form).

    VB Code:
    1. Private WithEvents objComboBox as VB.ComboBox

    Declaring each control is okay if you know the exact number of controls added at runtime. If not, I would suggest using class modules. Check out this recent thread. How to direct all events from 1 type of control to the same handler

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2002
    Location
    St. Louis MO
    Posts
    129
    The problem though, is that some of the controls are Custom Controls and there's about 30 total.

    I'd like to set it up where everybody could "talk" to each other.
    For example: The form tells all the controls to do their "record load" for record #1, and all the controls would reply back with "I did it".
    Any control that couldn't load it's info would reply back with "I couldn't do it", which would trigger the form the pop up a error message, etc...

    I guess I could use control properties to hold messages and have the form loop through all the controls and examine the property value, but I just thought that using events would be cleaner.

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    What you want to do is definitely possible, its just more complex.
    The controls need to raise events to the form and vice versa. Because you are adding controls at run time, you need an interface between these controls and their forms, possibly more than one. For just one project, it may be more trouble than its worth but if the plan is to use this technique for all future development then I would say go for it.

    It will take some serious designing to create everything you want. More time than I am willing to spend without getting paid.

    Just remember that in order to raise events to an object that object must "subscibe to the events", thus look into the WithEvents statement. The link to the thread I posted earlier may give you some insights as to what coding is neccessary.

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