Results 1 to 11 of 11

Thread: Loading objects at runtime...

  1. #1

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Loading objects at runtime...

    I am using this code to load one object from an array of command buttons:


    VB Code:
    1. 'This is a global variable
    2. Dim cmdNew(4) As CommandButton
    3.    
    4.     Set cmdNew(0) = Me.Controls.Add("VB.CommandButton", "cmdButton" & Me.Controls.Count)
    5.    
    6.     With cmdNew(0)
    7.         .Left = 1000
    8.         .Top = 1000
    9.         .Width = 2000
    10.         .Height = 500
    11.         .Caption = "Woof"
    12.         .Visible = True
    13.     End With
    14.    
    15.     Set cmdNew(0) = Me.Controls.Add("VB.CommandButton", "cmdButton" & Me.Controls.Count)




    This code will make one commandbutton on the form. But is it also possible to write code for the events for this command button. I tried write this:


    VB Code:
    1. Private Sub cmdNew_Click(Index As Integer)
    2.  
    3.     cmdNew(0).Caption = "Nese"
    4.     Command1(1).Caption = "Dust"
    5.    
    6. End Sub


    but it doesn't work.


    My other question is what is the parameters for the add function I have used. I found the line of code in a book. But it didn't tell me what the parameters what. Anyone know?

    My last question is. Is cmdNew here the same or more or less the same as a pointer in C++ or any other language?

  2. #2
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    you can't make events with what you did, well at least not without subclassing.


    this is possible:
    VB Code:
    1. Dim WithEvents cmdButton As CommandButton
    2.  
    3. Private Sub cmdButton_Click()
    4. MsgBox "hi"
    5. End Sub
    6.  
    7. Private Sub Form_Load()
    8.     Set cmdButton = Me.Controls.Add("VB.CommandButton", "blah")
    9.     Controls("blah").Visible = True 'or cmdButton.Visible=True
    10. End Sub

    but you can't make control arrays with WithEvents
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  3. #3

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Thanks, yes that worked. But if I am making a controll array at design time, and then loads objects, then I can write event code. So I guess that VB is doing it some other way. Do anyone know if this is possible for us to do too? Or is it just a secret for the compiler?

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    You can write event code for an existing control array, but you cant write code for individual controls (or arrays) that dont exist.

    The usual way of doing it is to create one element of a control array at design time, and set the visible property to False (and any other properties to the default you want). You can then add more controls to the array at run time by doing this:
    VB Code:
    1. Load txtName(1)   '(assumes array is called txtName, and you are creating element 1).
    2.  
    3. With txtName(1)
    4.   .Left = x
    5.   .Top = y
    6.   .Visible = True
    7.   ...
    8. End With

    In the events for the control array there is an Index parameter which you can use to determine which element was used, so you can run the appropriate code.

  5. #5

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Yup....I know....done that the last 5 years...,but this time I was trying to do it completly at run time. But it looks like I can't get the best from both worlds, unless I am using subclassing....

    Thanks anyway...ØØ

  6. #6

  7. #7

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    So do you want to do it for me if I give you my old socks to smoke......J/K

  8. #8

  9. #9

  10. #10

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by MartinLiss
    Isn't it easier/better to just create the (0) member of the control array at design time and hide it until needed?

    Yeah, but that is what I have done the last 5 years. It was actualy an other person that asked me how you loaded objects ar run time. And I started to play with it. And I found several ways to do it. But I couldn't fugure out how to make an array of controlls at run time, and write event code for them. So there is actually no question about an other way. I know more then one other way. Just want that way to work...

  11. #11

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by Wokawidget
    Here you go.
    I'll let you work out what uMsg values are for what events


    Woka
    Of course you had an example.......I will have a look at it after I have eaten. Right now I am trying to make drop down meny for a web site, and I have never done Java script before...:d

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