Results 1 to 14 of 14

Thread: Load controls at runtime with out using arrays.

  1. #1

    Thread Starter
    Fanatic Member 007shahid's Avatar
    Join Date
    Feb 2001
    Posts
    562

    Question Load controls at runtime with out using arrays.

    I know you can load as many a activex controls as you want to a form during runtime. But you have to have atleast one of those controls, with index '0'. Then you give...
    VB Code:
    1. Load ControlName(UBound(ControlName))
    2. ControlName.Move 100, 100, 500, 300
    3. ControlName.Visible = True
    4. 'etc...
    But I want to know if it is possible to add during runtime if....
    1. It is added as a component, but not added to the form during design time.
    2. Nothing has been done in relation to the control during design time. ie... the path of the activex control is got during runtime and then the form loads it.
    THE TIME/WEATHER IS
    Don't know how to use APIs or have problem with them,
    Download API-Guide & API-Viewer from http://www.allapi.net

  2. #2
    AIS_DK
    Guest
    I'm not sure about CommandBtn and other controls, that are directly part of the runtime. But for Usercontrols and properly also other controls you can use this

    Me.Controls.Add <Name of UserControl>, <Key to identify control>

    The name must be somthing like this: LibraryName.ControlName, if you know what I mean.

  3. #3
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    one question. why?
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  4. #4

    Thread Starter
    Fanatic Member 007shahid's Avatar
    Join Date
    Feb 2001
    Posts
    562
    AIS_DK, can you be more specific. Please upload a small project if you can.

    one question. why?
    Go here...
    http://www.vbforums.com/showthread.php?threadid=173736

    I will need to add controls to my forms during runtime. Controls maybe common vb controls, where I can easily use the load function. But what if the user wants to add a custom control
    THE TIME/WEATHER IS
    Don't know how to use APIs or have problem with them,
    Download API-Guide & API-Viewer from http://www.allapi.net

  5. #5
    AIS_DK
    Guest
    I'll try to create a small program, but it won't be before later today.

  6. #6
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011
    Following are some example of creating controls at runtime without using arrays...
    VB Code:
    1. Dim Command1 As CommandButton
    2. Set Command1 = Controls.Add ("VB.CommandButton","Command1",Me)
    3. Command1.Visible = True
    4.  
    5. Dim Label1 As Label
    6. Set Label1 = Controls.Add ("VB.Label","Label1",Me) 'Instead of Me you can also put any container control's name
    7. Label1.Visible = True

    Hope this will help...

  7. #7

    Thread Starter
    Fanatic Member 007shahid's Avatar
    Join Date
    Feb 2001
    Posts
    562
    Thank You. It works fine. Now I need to know, how to do the same without setting reference to the component during runtime
    THE TIME/WEATHER IS
    Don't know how to use APIs or have problem with them,
    Download API-Guide & API-Viewer from http://www.allapi.net

  8. #8
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011
    What do you mean by without setting reference? you mean without assigning object to the object variable?? If you don't do so... how would you be able to set its visibility to True and other properties????
    I think I couldn't get your question.

  9. #9

    Thread Starter
    Fanatic Member 007shahid's Avatar
    Join Date
    Feb 2001
    Posts
    562
    If you want to add an activex control, you select 'Components' from from the 'Project' menu in VB. Then you check the components you want from the list and click OK. The required controls get added to the toolbar. I want to do this in my vb app also. ie... through some code I want to add activex controls in my form, even though I haven't made it available through the components window shown in the picture attached.
    Attached Images Attached Images  
    THE TIME/WEATHER IS
    Don't know how to use APIs or have problem with them,
    Download API-Guide & API-Viewer from http://www.allapi.net

  10. #10
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011
    You'll have to add the license for the control for that purpose..
    VB Code:
    1. Dim lKey As String
    2. lKey = Licenses.Add("MSADODCLib.ADODC")
    3. With Controls.Add("MSADODCLib.ADODC", "ADODC1")
    4.     .Visible = True
    5. End With

  11. #11

    Thread Starter
    Fanatic Member 007shahid's Avatar
    Join Date
    Feb 2001
    Posts
    562
    Thank You. That is just what I wanted. But now, I got to figure out how to get the Library Name and the Control Name from a selected file. I checked out API-GUIDE. This code shows how to use an API without declaring it.
    VB Code:
    1. 'Create a new project and add this code to Form1
    2. Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
    3. Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
    4. Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
    5. Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Any, ByVal wParam As Any, ByVal lParam As Any) As Long
    6. Private Sub Form_Load()
    7.     On Error Resume Next
    8.     'KPD-Team 1999
    9.     'URL: [url]http://www.allapi.net/[/url]
    10.     'E-Mail: [email][email protected][/email]
    11.     'We're going to call an API-function, without declaring it!
    12.     Dim lb As Long, pa As Long
    13.     'map 'user32' into the address space of the calling process.
    14.     lb = LoadLibrary("user32")
    15.     'retrieve the address of 'SetWindowTextA'
    16.     pa = GetProcAddress(lb, "SetWindowTextA")
    17.     'Call the SetWindowTextA-function
    18.     CallWindowProc pa, Me.hWnd, "Hello !", ByVal 0&, ByVal 0&
    19.     'unmap the library's address
    20.     FreeLibrary lb
    21. End Sub
    THE TIME/WEATHER IS
    Don't know how to use APIs or have problem with them,
    Download API-Guide & API-Viewer from http://www.allapi.net

  12. #12

    Thread Starter
    Fanatic Member 007shahid's Avatar
    Join Date
    Feb 2001
    Posts
    562
    Just another problem. Add a button 'Command1' to a form and paste the following.
    When the command1 button is pressed it gives me an object required error.

    VB Code:
    1. Private Sub Command1_Click()
    2. ADODC1.Visible = False
    3. End Sub
    4.  
    5. Private Sub Form_Load()
    6. Dim lKey As String
    7. lKey = Licenses.Add("MSADODCLib.ADODC")
    8. With Controls.Add("MSADODCLib.ADODC", "ADODC1")
    9.     .Move 100, 100, 5000, 500
    10.     .Visible = True
    11. End With
    12. End Sub
    THE TIME/WEATHER IS
    Don't know how to use APIs or have problem with them,
    Download API-Guide & API-Viewer from http://www.allapi.net

  13. #13
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011
    do this..
    VB Code:
    1. Private Sub Command1_Click()
    2. ADODC1.Visible = False
    3. End Sub
    4.  
    5. Private Sub Form_Load()
    6. Dim lKey As String
    7. Dim ADODC1 As Control
    8. lKey = Licenses.Add("MSADODCLib.ADODC")
    9. Set ADODC1 = Controls.Add("MSADODCLib.ADODC", "ADODC1")
    10. With ADODC1
    11.     .Move 100, 100, 5000, 500
    12.     .Visible = True
    13. End With
    14. End Sub

  14. #14

    Thread Starter
    Fanatic Member 007shahid's Avatar
    Join Date
    Feb 2001
    Posts
    562
    Just a small correction. The scope of the declaration should have been in the beginning, not inside Form_Load().

    VB Code:
    1. Dim ADODC1 As Control
    2.  
    3. Private Sub Command1_Click()
    4. ADODC1.Visible = False
    5. End Sub
    6.  
    7. Private Sub Form_Load()
    8. Dim lKey As String
    9. lKey = Licenses.Add("MSADODCLib.ADODC")
    10. Set ADODC1 = Controls.Add("MSADODCLib.ADODC", "ADODC1")
    11. With ADODC1
    12.     .Move 100, 100, 5000, 500
    13.     .Visible = True
    14. End With
    15. End Sub
    THE TIME/WEATHER IS
    Don't know how to use APIs or have problem with them,
    Download API-Guide & API-Viewer from http://www.allapi.net

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