CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2005
    Location
    Quebec, Canada
    Posts
    75

    [RESOLVED] Create an object durring runtime in code

    OK, I know how to create an object as arrays. See this example:

    Code:
      Load Text1(1)
      
      Text1(1).Visible = True
      Text1(1).Top = 0
      Text1(1).Left = 0
    I want the exact same effect but this time without an abject being part of an array. Example: Intead of load Text1(1), I would load Text2. Is this possible, if so, HOW?

    Thank's
    David Richard

  2. #2
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Create an object durring runtime in code

    Code:
      Dim t1 As TextBox
      Set t1 = Controls.Add("VB.TextBox", "Text2")
    or simply
    Code:
      Controls.Add "VB.TextBox", "Text2"

  3. #3
    Join Date
    Jul 2005
    Location
    Quebec, Canada
    Posts
    75

    Smile Re: Create an object durring runtime in code

    Thank you! It works.

    One thing though:

    Code:
    Controls.Add "VB.Textbox", "Text2"
    Will not work by itself. If you need to use Text2's reference, It does not work. The first example is the one to follow! So when you want to set Visible to true or it's size and position, The Set Var = methode is the best.

    Thank you again WoF!
    David Richard

  4. #4
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: [RESOLVED] Create an object durring runtime in code

    You are welcome.
    I didn't know. I would have expected, after Controls.Add "VB.TextBox", "Text2", you could use Text2.Visisble or whatever throughout your code, but come to think of it, that is not possible. Text2 ist not known to the compiler at compile time, so it cannot build references to it. The object comes in by late binding during runtime. So you must care to keep a reference to it, like t1, to keep references to it working, because its name is but a string and not evaluated at compile time.
    Good job to clear that.

Tags for this Thread

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