CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2001
    Posts
    30

    removing a control in run-time

    Hi,
    I'm adding controls in run-time by using:

    Controls.Add ("TrueOleDBGrid70.TDBGrid", "grid_name" , me)



    How can I remove it in run-time?

    Thanks
    Erez


  2. #2
    Join Date
    Aug 2001
    Posts
    30

    Re: removing a control in run-time

    solved


  3. #3
    Join Date
    Oct 2001
    Posts
    13

    Re: removing a control in run-time


    Can u please tell me how you do an add and remove of a control ,
    Thanks
    Wajahath


  4. #4
    Join Date
    Aug 2001
    Posts
    30

    Re: removing a control in run-time

    HOWTO: Create Lightweight Controls with Visual Basic 6.0

    Q184645


    --------------------------------------------------------------------------------
    The information in this article applies to:

    Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, version 6.0

    --------------------------------------------------------------------------------


    SUMMARY
    This article shows you how to create a lightweight control in Visual Basic version 6.0, briefly covers the advantages and limitations of lightweight controls, and contains instructions on how to create a sample project that demonstrates how lightweight controls use less system resources.



    MORE INFORMATION
    Lightweight, or windowless controls, are controls that are similar to regular controls except that lightweight controls do not have window handles. Because of they lack window handles, lightweight controls require less system resources, making them ideal for Internet applications, distributed applications, or any application where limited system resources may be an issue. Further, lightweight controls can be any shape and have true transparency properties.

    When you create a lightweight control, the container can hold only other lightweight controls. If you try to place a regular control in a lightweight user control, you will receive the following error:


    Can't have windowed child controls on a windowless control.

    Regular controls always appear on top of lightweight controls because lightweight controls use the resources of the parent form. The ZOrder of a regular control changes only with respect to other regular controls.

    Because lightweight controls do not have a Hwnd property, you cannot use these controls with Windows API functions that require an HWnd parameter. The HWnd property of the container is used instead, which may result in unpredictable results.

    To make a control into a lightweight control, set the Windowless property of the control to True.

    Visual Basic ships with intrinsic lightweight controls and other lightweight controls in the ActiveX component file MSWLess.ocx. For a list of these intrinsic controls and instructions on how to use the controls in the ActiveX component file MSWLess.ocx, please see the following article in the Microsoft Knowledge Base:
    Q184687 INFO: Lightweight Controls in Visual Basic 6.0
    The next section tells you how to create a sample project that contains a lightweight control. The sample project demonstrates that a lightweight control uses less resources than a regular control.
    Creating the Sample Project
    This section shows you how to create the sample project. The project measures the amount of time to load and unload a lightweight control as compared to a regular control. To create this project, you need to create a Standard EXE project, a lightweight control, and a regular control.
    To Create the Standard EXE Program
    Start a new Standard EXE project in Visual Basic. Form1 is created by default.


    Add the following controls to the Form1 form and set the following properties to the appropriate controls:


    Control Default Name Property Setting
    ---------------------------------------------------------------------
    Command Button Command1 Name cmdWndLess
    Height 375
    Left 120
    Top 840
    Width 2535

    Command Button Command2 Name cmdWnd
    Height 375
    Left 3000
    Top 840
    Width 2535

    TextBox Text1 Name txtNumofControls
    Height 285
    Left 1680
    Top 120
    Width 375

    Label Label1 Name lblWndLessLoad
    AutoSize True
    Height 195
    Left 120
    Top 1320

    Label Label2 Name lblWndLessUnload
    AutoSize True
    Height 195
    Left 120
    Top 1680

    Label Label3 Name lblWndLoad
    AutoSize True
    Height 195
    Left 3000
    Top 1320

    Label Label4 Name lblWndUnload
    AutoSize True
    Height 195
    Left 3000
    Top 1680
    Width 480

    Label Label5 Name lblStartTicks
    Alignment Right Justify
    AutoSize True
    Caption Start Ticker
    Height 195
    Left 4680
    Top 480
    Width 810

    Label Label6 Name lblEndTicks
    Alignment Right Justify
    AutoSize True
    Caption End Ticks:
    Height 195
    Left 4680
    Top 120




    Copy the following code to the Code window of the Form1 form:


    option Explicit
    private Declare Function GetTickCount Lib "kernel32" () as Long
    Dim bLoadWnd as Boolean
    Dim bLoadWndLess as Boolean
    Dim lTickStart as Long
    Dim lTickEnd as Long
    Dim lResult as Long
    Dim colCtrlWnd as Collection
    Dim colCtrlWndLess as Collection

    private Sub cmdWnd_Click()
    'This event loads and unloads regular controls.
    Dim tmpCtrlWnd as Control
    Dim lCounter as Long

    If bLoadWnd = true then
    'Load Regular controls.
    lblWndLoad.Caption = "Loading Regular Controls"
    lTickStart = GetTickCount
    lblStartTicks.Caption = "Start: " & CStr(lTickStart) & " ms"

    for lCounter = 1 to Val(txtNumofControls.Text)
    set tmpCtrlWnd = Controls.Add("TestControls.ctrlWnd", _
    "ctrlWnd" & lCounter)
    colCtrlWnd.Add tmpCtrlWnd
    set tmpCtrlWnd = nothing
    DoEvents
    next

    lTickEnd = GetTickCount
    lblEndTicks.Caption = "End: " & CStr(lTickEnd) & " ms"
    lblWndLoad.Caption = "Load time: " & _
    CStr(lTickEnd - lTickStart) & " ms"
    bLoadWnd = false
    cmdWnd.Caption = "Unload Regular Controls"

    else
    'Unload regular controls.
    lblWndUnload.Caption = "Unloading Regular Controls"
    lTickStart = GetTickCount
    lblStartTicks.Caption = "Start: " & CStr(lTickStart) & " ms"
    ' UnLoad controls.
    for lCounter = colCtrlWnd.Count to 1 step -1
    Controls.Remove colCtrlWnd.Item(lCounter).Name
    colCtrlWnd.Remove (lCounter)
    next
    lTickEnd = GetTickCount
    lblEndTicks.Caption = "End: " & CStr(lTickEnd) & " ms"
    lblWndUnload.Caption = "Unload time: " & _
    CStr(lTickEnd - lTickStart) & " ms"
    bLoadWnd = true
    cmdWnd.Caption = "Load Regular Controls"

    End If

    End Sub

    private Sub cmdWndless_Click()
    'This command button loads and unloads the lightweight controls.

    Dim tmpCtrlWndLess as Control
    Dim lCounter as Long
    If bLoadWndLess = true then
    lblWndLessLoad.Caption = "Loading Lightweight Controls"
    lTickStart = GetTickCount
    lblStartTicks.Caption = "Start: " & CStr(lTickStart) & " ms"

    ' Load controls.
    for lCounter = 1 to Val(txtNumofControls.Text)
    set tmpCtrlWndLess = _
    Controls.Add("TestControls.ctrlWndLess", _
    "ctrlWndLess" & lCounter)
    colCtrlWndLess.Add tmpCtrlWndLess
    set tmpCtrlWndLess = nothing
    DoEvents
    next

    lTickEnd = GetTickCount
    lblEndTicks.Caption = "End: " & CStr(lTickEnd) & " ms"
    lblWndLessLoad.Caption = "Load time: " & _
    CStr(lTickEnd - lTickStart) & " ms"
    bLoadWndLess = false
    cmdWndless.Caption = "Unload Windowless Controls"
    else
    lblWndLessUnload.Caption = "Unloading Lightweight Controls"
    lTickStart = GetTickCount
    lblStartTicks.Caption = "Start: " & CStr(lTickStart) & " ms"

    ' UnLoad controls.
    for lCounter = colCtrlWndLess.Count to 1 step -1
    Controls.Remove colCtrlWndLess.Item(lCounter).Name
    colCtrlWndLess.Remove (lCounter)
    next

    lTickEnd = GetTickCount
    lblEndTicks.Caption = "End: " & CStr(lTickEnd) & " ms"
    lblWndLessUnload.Caption = "Unload time: " & _
    CStr(lTickEnd - lTickStart) & " ms"
    bLoadWndLess = true
    cmdWndless.Caption = "Load Windowless Controls"
    End If
    End Sub

    private Sub Form_Load()
    set colCtrlWnd = new Collection
    set colCtrlWndLess = new Collection
    cmdWndless.Caption = "Load Lightweight Controls"
    bLoadWndLess = true
    cmdWnd.Caption = "Load Regular Controls"
    bLoadWnd = true
    txtNumofControls.Text = "20"
    End Sub






    On the Run menu, click Start or press the F5 key to start the program. If the form successfully launches, then you have completed creating the standard executable program in this project.


    The next section shows you how to create the regular and lightweight controls that the standard executable program uses.
    To Create the Regular and Lightweight Controls
    Add an ActiveX Control project to your project group.


    On the File menu, click Add Project. The Add Project dialog box appears.


    In the New Tab, select ActiveX Control.


    Click Open to add the project to your project group and to close the Add Project dialog box. UserControl1 is created by default.


    Change the name of UserControl1 to ctrlWnd. This will be your regular control.





    Add a combo box to the ctrlWnd control. Save the control.


    Add another UserControl by completing the following steps. This will be your lightweight control:


    On the Project Menu, click Add User Control. The Add User Control dialog box appears with User Control highlighted.


    Click Open to add the User Control to the project and close the Add User Control dialog box. UserControl1 is created by default.


    Change the name of UserControl1 to ctrlWndLess.


    Set the Windowless property to True.





    Save the control.


    Rename the Project to TestControls. Save the project.


    You have completed creating the ActiveX controls required by this project. To run the project, press F5 or from the Run menu, click Start. You can set the number of controls and compare the load and unload times.


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