Results 1 to 6 of 6

Thread: Limit a form to only one instance of a usercontrol

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Limit a form to only one instance of a usercontrol

    I have a usercontrol that sub classes the parent form. This is fine unless you try to place more than one on the form. It is easy enough to check if there are any of these controls on the form already and inform the user that they are about to make a big mistake by placing a second one on the form. But, is there a way to stop a second control from being placed on the form?

  2. #2
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    In the second control, when it finds out that it's another one there, maybe you could send a message to the first one, telling it to unload all others but that one...

    I've never actually done this, but just thinking...

  3. #3
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    Not exactly what I originally had in mind, but this actually works...

    VB Code:
    1. Private Sub UserControl_InitProperties()
    2.     Dim DeleteMe As Boolean
    3.     Dim Obj
    4.    
    5.     For Each Obj In UserControl.ParentControls
    6.         'TestControl I named the control I was testing this code
    7.         ' change it to whatever is your control named
    8.        
    9.         If TypeOf Obj Is TestControl Then
    10.             If Obj.hWnd <> UserControl.hWnd Then
    11.                 DeleteMe = True
    12.                 Exit For
    13.             End If
    14.         End If
    15.     Next
    16.    
    17.     If DeleteMe Then
    18.         MsgBox "You cannot place more than one control of this type"
    19.         SendKeys "{DEL}"
    20.     End If
    21. End Sub
    22.  
    23. Public Function hWnd() As Long
    24.     hWnd = UserControl.hWnd
    25. End Function

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    Sometimes a second head is needed. I don't think I would of ever thought of using sendkeys. Thanks, it works great!

  5. #5
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    I just realized....

    IF the first control is a control array (i.e. with index = 0), then the programmer can create the other controls at run time, and the delete key won't work anymore...

    BUT, at that time... when using Load you can also use Unload.

    So you'll have to code for that too...

  6. #6
    Frenzied Member macai's Avatar
    Join Date
    Jul 2001
    Location
    Napanoch NY
    Posts
    1,228
    Well, what you can do is create a form that exists solely to be unloaded and force your UserControl into that form. (I'll name this form frmControlKiller) MAKE this Form in your UserControl's Project File. ALSO make sure frmControlKiller's Design window is CLOSED.
    VB Code:
    1. Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
    2.  
    3. Public Function hWnd() As Long
    4.     hWnd = UserControl.hWnd
    5. End Function
    6. Private Sub UserControl_Show()
    7.  Dim x As Object
    8.  For Each x In UserControl.ParentControls
    9.  
    10.   If TypeOf x Is UserControl1 Then
    11.     If x.hWnd <> UserControl.hWnd Then
    12.      frmControlKiller.Visible = False
    13.      SetParent UserControl.hWnd, frmControlKiller.hWnd
    14.      Unload frmControlKiller
    15.      MsgBox "you may not have more than one copy of this control on a form!!"
    16.     End If
    17.    End If
    18.  Next
    19. End Sub
    Luke

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