|
-
Jan 24th, 2003, 03:28 PM
#1
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?
-
Jan 25th, 2003, 12:16 AM
#2
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...
-
Jan 25th, 2003, 01:06 AM
#3
Not exactly what I originally had in mind, but this actually works...
VB Code:
Private Sub UserControl_InitProperties()
Dim DeleteMe As Boolean
Dim Obj
For Each Obj In UserControl.ParentControls
'TestControl I named the control I was testing this code
' change it to whatever is your control named
If TypeOf Obj Is TestControl Then
If Obj.hWnd <> UserControl.hWnd Then
DeleteMe = True
Exit For
End If
End If
Next
If DeleteMe Then
MsgBox "You cannot place more than one control of this type"
SendKeys "{DEL}"
End If
End Sub
Public Function hWnd() As Long
hWnd = UserControl.hWnd
End Function
-
Jan 25th, 2003, 02:20 AM
#4
Sometimes a second head is needed. I don't think I would of ever thought of using sendkeys. Thanks, it works great!
-
Jan 25th, 2003, 03:00 AM
#5
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...
-
Jan 25th, 2003, 03:31 AM
#6
Frenzied Member
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:
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Public Function hWnd() As Long
hWnd = UserControl.hWnd
End Function
Private Sub UserControl_Show()
Dim x As Object
For Each x In UserControl.ParentControls
If TypeOf x Is UserControl1 Then
If x.hWnd <> UserControl.hWnd Then
frmControlKiller.Visible = False
SetParent UserControl.hWnd, frmControlKiller.hWnd
Unload frmControlKiller
MsgBox "you may not have more than one copy of this control on a form!!"
End If
End If
Next
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|