How to Create Object at Runtime

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom Rathbun

    How to Create Object at Runtime

    This is probably simple but it has stumped me. I want to create objects at
    runtime for example:
    A program that would allow you to draw lines on a form. For each new line I
    would like to create a new line object in an array the same way you would a
    simple variable.

    if it was just a variable I could

    dim a(0) as single
    ..
    (some code)
    ..
    b=ubound(a)+1
    redim preserve a(b) as single
    ..
    (other code)

    Is it possible to do this with controls ... say 10 textboxs today 12
    tomarrow etc.
    right now I put a bunch in an array and hide the ones I don't need.

    Thanks
    Tom


  • Randy Birch

    #2
    Re: How to Create Object at Runtime

    Assuming you have one textbox on the form named Text1, with its index
    property set to 0 to create a control array:


    --

    Randy Birch
    MVP Visual Basic

    Please respond only to the newsgroups so all can benefit.


    "Tom Rathbun" <tjrathbun@comc ast.net> wrote in message
    news:jJWdnYB-oKounwKiRVn-vQ@comcast.com. ..
    : This is probably simple but it has stumped me. I want to create objects
    at
    : runtime for example:
    : A program that would allow you to draw lines on a form. For each new line
    I
    : would like to create a new line object in an array the same way you would
    a
    : simple variable.
    :
    : if it was just a variable I could
    :
    : dim a(0) as single
    : .
    : (some code)
    : .
    : b=ubound(a)+1
    : redim preserve a(b) as single
    : .
    : (other code)
    :
    : Is it possible to do this with controls ... say 10 textboxs today 12
    : tomarrow etc.
    : right now I put a bunch in an array and hide the ones I don't need.
    :
    : Thanks
    : Tom
    :
    :


    Comment

    • Randy Birch

      #3
      Re: How to Create Object at Runtime

      grrrrrrr ...

      .... use:

      'load
      Private Sub Command1_Click( )

      Dim cnt As Long

      For cnt = 0 To 25

      If cnt > 0 Then Load Text1(cnt)

      With Text1(cnt)
      .Move 300, 210 + (300 * cnt), 1700
      .Visible = True
      End With

      Next

      End Sub

      'unload
      Private Sub Command2_Click( )

      Dim cnt As Long

      For cnt = 25 To 0 Step -1

      If cnt > 0 Then Unload Text1(cnt)

      Next

      End Sub

      You can't unload the control added at design time, so it's important to test
      to ensure the index > 0.

      --

      Randy Birch
      MVP Visual Basic

      Please respond only to the newsgroups so all can benefit.


      "Randy Birch" <rgb_removethis @mvps.org> wrote in message
      news:N3Enb.8766 1$3f.20064@twis ter01.bloor.is. net.cable.roger s.com...
      : Assuming you have one textbox on the form named Text1, with its index
      : property set to 0 to create a control array:
      :
      :
      : --
      :
      : Randy Birch
      : MVP Visual Basic
      : http://www.mvps.org/vbnet/
      : Please respond only to the newsgroups so all can benefit.
      :
      :
      : "Tom Rathbun" <tjrathbun@comc ast.net> wrote in message
      : news:jJWdnYB-oKounwKiRVn-vQ@comcast.com. ..
      : : This is probably simple but it has stumped me. I want to create objects
      : at
      : : runtime for example:
      : : A program that would allow you to draw lines on a form. For each new
      line
      : I
      : : would like to create a new line object in an array the same way you
      would
      : a
      : : simple variable.
      : :
      : : if it was just a variable I could
      : :
      : : dim a(0) as single
      : : .
      : : (some code)
      : : .
      : : b=ubound(a)+1
      : : redim preserve a(b) as single
      : : .
      : : (other code)
      : :
      : : Is it possible to do this with controls ... say 10 textboxs today 12
      : : tomarrow etc.
      : : right now I put a bunch in an array and hide the ones I don't need.
      : :
      : : Thanks
      : : Tom
      : :
      : :
      :
      :


      Comment

      • Nut Shell

        #4
        Re: How to Create Object at Runtime

        Try this:
        Dim line2 As VB.Line
        Private Sub Form_Load()

        Set line2 = Controls.Add("v b.line", "line2")

        line2.X1 = 2520
        line2.X2 = 360
        line2.Y1 = 400
        line2.Y2 = 2000
        line2.Refresh
        line2.BorderCol or = red
        line2.Visible = True
        ' etc etc
        End Sub

        "Tom Rathbun" <tjrathbun@comc ast.net> wrote in message news:<jJWdnYB-oKounwKiRVn-vQ@comcast.com> ...[color=blue]
        > This is probably simple but it has stumped me. I want to create objects at
        > runtime for example:
        > A program that would allow you to draw lines on a form. For each new line I
        > would like to create a new line object in an array the same way you would a
        > simple variable.
        >
        > if it was just a variable I could
        >
        > dim a(0) as single
        > .
        > (some code)
        > .
        > b=ubound(a)+1
        > redim preserve a(b) as single
        > .
        > (other code)
        >
        > Is it possible to do this with controls ... say 10 textboxs today 12
        > tomarrow etc.
        > right now I put a bunch in an array and hide the ones I don't need.
        >
        > Thanks
        > Tom[/color]

        Comment

        • Joon Nan

          #5
          Re: How to Create Object at Runtime

          This is simple,

          Create a Line object or TextBox object, set Index to 0, set Visible=False
          Put in the example code like below:

          Private Sub Form_Load()
          Dim i As Integer
          For i = 1 To 5
          Load Line1(i)
          Line1(i).Y1 = i * 100
          Line1(i).Y2 = i * 100
          Line1(i).Visibl e = True
          Next i
          End Sub


          Regards,
          Joon Nan

          "Tom Rathbun" <tjrathbun@comc ast.net> wrote in message
          news:jJWdnYB-oKounwKiRVn-vQ@comcast.com. ..[color=blue]
          > This is probably simple but it has stumped me. I want to create objects[/color]
          at[color=blue]
          > runtime for example:
          > A program that would allow you to draw lines on a form. For each new line[/color]
          I[color=blue]
          > would like to create a new line object in an array the same way you would[/color]
          a[color=blue]
          > simple variable.
          >
          > if it was just a variable I could
          >
          > dim a(0) as single
          > .
          > (some code)
          > .
          > b=ubound(a)+1
          > redim preserve a(b) as single
          > .
          > (other code)
          >
          > Is it possible to do this with controls ... say 10 textboxs today 12
          > tomarrow etc.
          > right now I put a bunch in an array and hide the ones I don't need.
          >
          > Thanks
          > Tom
          >
          >[/color]


          Comment

          Working...