PDA

View Full Version : Solved: MDI "like" child userforms ?



MountainVogu
06-08-2006, 05:36 PM
Hi

Is it possible to create MDIchild forms in VBA excel like you can in VB ?

I would like to have a modeless userform that is lauched from another userform, but still be able to access and use the parent userform.

I'm sure I'm gonna get the why do you want to do that with excel responces.

The answer: to see if you can

The idea being to create a 'toolbox' feature for use with the main userform.

Cheers

Jacob Hilderbrand
06-08-2006, 06:52 PM
Sure, just have the first userform, load the second user form. So for example you open the load the first userform:

UserForm1.Show False

Then that userform will load the second one:

UserForm2.Show False

You can have that code run on the Initialize event or by a CommandButton etc.

MountainVogu
06-08-2006, 06:59 PM
Sorry should have been more specific thats not the functionality I'm after.

I wish both userforms visible, obviously second not until its been lauched, but to still be able to use the first userform.

A bit like the toolbox in the VBE and the design userform view. where you can drag stuff from one to the other.

Cheers

Jacob Hilderbrand
06-08-2006, 08:46 PM
In this case you can use both userforms. But I don't know about dragging from one to the other.

Andy Pope
06-09-2006, 02:35 AM
This is a very simple example.

Userform1 will display Userform2 when activated.
Userform2 contains a Label control.

Both userforms have the ShowModal property set to FALSE.

Add following code to Userform1
Private Sub UserForm_Activate()

With UserForm2
.Show
.Left = Me.Left + Me.Width
.Top = Me.Top
End With

End Sub

Private Sub UserForm_BeforeDragOver(ByVal Cancel As MSForms.ReturnBoolean, _
ByVal Control As MSForms.Control, ByVal Data As MSForms.DataObject, _
ByVal X As Single, ByVal Y As Single, ByVal State As MSForms.fmDragState, _
ByVal Effect As MSForms.ReturnEffect, ByVal Shift As Integer)

Cancel = True

Effect = 1
End Sub

Private Sub UserForm_BeforeDropOrPaste(ByVal Cancel As MSForms.ReturnBoolean, _
ByVal Control As MSForms.Control, ByVal Action As MSForms.fmAction, _
ByVal Data As MSForms.DataObject, ByVal X As Single, ByVal Y As Single, _
ByVal Effect As MSForms.ReturnEffect, ByVal Shift As Integer)

Cancel = True

Effect = 1

MsgBox "Dropped " & Data.GetText

End Sub
And this code to userform2
Private Sub Label1_MouseMove(ByVal Button As Integer, _
ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Dim MyDataObject As DataObject

If Button = 1 Then

Set MyDataObject = New DataObject

Dim Effect As Integer

MyDataObject.SetText Label1.Caption

Effect = MyDataObject.StartDrag

End If

End Sub

When both userforms are displayed you should be able to drag label1 across and drop on userform1. A simple message box will display label1's caption.

johnske
06-09-2006, 04:47 AM
Hi Andy,

Hope you don't mind but I added some line-breaks to your code (my screen's just not big enough to read it without them) :)

Regards,
John

gsouza
06-09-2006, 08:48 AM
The message box says dropped, but it did not actually drop over to userform1. What am i doing wrong? I did just like you said. I am using Excel 2000

Andy Pope
06-09-2006, 11:42 AM
No problem John :)

gsouza, the code does not copy across the label it simple recognises the action of drag+drop. You would need to expand the code, although I doubt you could simple change the parent of the label and make it move from userform2 to userform1. More likely you would have to create controls on the fly within userform1.

gsouza
06-09-2006, 11:59 AM
Ohhhhhhhhhhhhhhhhhhhhhhh, :rofl: now i get it. Thanks hehehe

MountainVogu
06-12-2006, 03:14 PM
Thanks Andy,

Nice bit of functionality. but again I wasn't as clear as I should have been.

Anyway did what I needed to do as a result of your direction. It always amazes me how tunnel vision sets in sometimes.

Userforms 101 - Open as many userforms as you like from a userform independent procedure and away you go.

Thanks again.