Consulting

Results 1 to 18 of 18

Thread: programatically added controls do not show up

  1. #1
    VBAX Regular
    Joined
    Apr 2018
    Posts
    25
    Location

    programatically added controls do not show up

    Hi,

    I am adding frames to a form in a runtime
    There seems to be a bug that makes only the last one appear correctly, while all the others do not appear at the form.
    I know THEY ARE THERE, i can loop through them, they have .visible =true, all the properties (position, parent, etc) seem OK, they are just not visible.
    The funny thing is that when i put a STOP in a code (or break the execution in any way, e.g. msgbox) all the frames appear correctly.
    I tried playing with application.screenupdating, form.repaint but it doesn't seem to help.
    Application.wait also doesnt change anything.

    Any ideas?

    (i cannot paste the code here, it's a big project. And when i tried to reproduce the error in a simpler case, it seems to work correctly - so I am just hoping someone had a similar case)

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Have you tried Form.Repaint after adding the controls?
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    VBAX Regular
    Joined
    Apr 2018
    Posts
    25
    Location
    yup

    Quote Originally Posted by mg25683 View Post

    I tried playing with application.screenupdating, form.repaint but it doesn't seem to help.

    Last edited by mg25683; 04-06-2020 at 03:47 AM.

  4. #4
    It'd help if you share the relevant bit of code. Have you added the controls to the frame or to the userform? You do know the Top and Left properties of a control in a frame are relative to the frame? So 0,0 is the top-left-hand corner of the frame.
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  5. #5
    Hmm, reread your question. Given that it does not work in your project but does in a fresh one, have you tried cleaning the VBA project?
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  6. #6
    VBAX Regular
    Joined
    Apr 2018
    Posts
    25
    Location
    I add them to another frame.
    As I said, all the properties are OK.
    As I said - if I break the code right after adding a frame it shows up in the correct place, so this cannot be a matter of position or any stuff like this
    As I said - the relevant code contains dozens of procedures and is not an option

  7. #7
    VBAX Regular
    Joined
    Apr 2018
    Posts
    25
    Location
    Quote Originally Posted by Jan Karel Pieterse View Post
    Hmm, reread your question. Given that it does not work in your project but does in a fresh one, have you tried cleaning the VBA project?
    No, it is not a matter of cleaning the code/starting over. As i said - adding controls works fine in a simple project (two fixed frames), but does not in a production case (lots of controls, classes, interfaces, frames within frames, etc) that i cannot paste here

  8. #8
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    The funny thing is that when i put a STOP in a code (or break the  execution in any way, e.g. msgbox) all the frames appear correctly.
    'Do Events' instead of Stop or MsgBox?

    As each Frame is completed call
    Function RepaintFrame(Fram As Object)
       Fram.Repaint
       DoEvents()
    End Function
    I've never run into this issue since I always complete the UserForm in design mode, then Hide/Show - Move Back/Front as needed.
    Last edited by SamT; 04-06-2020 at 12:39 PM.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  9. #9
    Does it work if you add a fresh userform to the project? I've never had a problem like this before. E.g. my all vba treeview project adds thousands of controls at runtime.
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  10. #10
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    As Sam says, DoEvents can work. But I have had an instance when I have had to put three together, as in

        DoEvents
        DoEvents
        DoEvents
    to make it work. Not ideal but a fix!
    Semper in excretia sumus; solum profundum variat.

  11. #11
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    I am adding frames to a form in a runtime
    That is not a sensible way to do this.
    Create controls in design mode;prpperty invisible.
    The only thing you have to do in run time is make them visible: frame1.visible=true.

    What you are trying to do now is to use too much code to lift a design failure. (designing precedes coding).

  12. #12
    I disagree snb. Sometimes a userform with controls added at runtime is to be preferred.
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  13. #13
    VBAX Regular
    Joined
    Apr 2018
    Posts
    25
    Location
    Quote Originally Posted by snb View Post
    That is not a sensible way to do this.
    Create controls in design mode;prpperty invisible.
    The only thing you have to do in run time is make them visible: frame1.visible=true.

    What you are trying to do now is to use too much code to lift a design failure. (designing precedes coding).
    sorry VBAX Guru,
    Last edited by SamT; 04-07-2020 at 06:34 AM. Reason: contained personal attack

  14. #14
    VBAX Regular
    Joined
    Apr 2018
    Posts
    25
    Location
    @samT, @paulked

    thanks a lot, doEvents did the job

  15. #15
    VBAX Regular
    Joined
    Apr 2018
    Posts
    25
    Location
    Quote Originally Posted by Jan Karel Pieterse View Post
    I disagree snb. Sometimes a userform with controls added at runtime is to be preferred.
    yup, not only prefered - there are lots of cases this is the only way to do stuff

  16. #16
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    No arguments ? 'Sometimes' 'a lot of cases' not very convincing. (not at all I'd say).

  17. #17
    VBAX Regular
    Joined
    Apr 2018
    Posts
    25
    Location
    c'mon man, google "Why Add Controls at Runtime?". Its the same for every programming language. The fact that you only worked with simple and static forms does not mean that other cases do not exist.

  18. #18
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Its the same for every programming language.
    I didn't even realize that every programming language has a built in UserForms "Factory."
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

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