|
-
Mar 6th, 2004, 01:36 AM
#1
Thread Starter
Frenzied Member
Mdi Problem
I am writing an MDI app in .NET.
I want to add buttons to the MDI parent form which show child forms when clicked. I want the child forms to merge with the parent so that the parent's main menu items are still visible.
I have 2 problems:
1.When I show a child form using:
VB Code:
Dim Form1 As New Form1
Form1.MdiParent = Me
Form1.Show()
you can still see and access the buttons on the parent form even if I add Form1 .BringToFront()
If I remove the line Form1 .MdiParent = Me, the parent form's menu is no longer visible. ie. the child is not contained within the parent.
2.When I click on a button that shows a child form, it creates a new instance of the form each time. How do I activate a previous instance of the form if there is one rather than creating a new instance?
-
Mar 6th, 2004, 01:44 AM
#2
I don't understand what you are trying to do can you explain further?
-
Mar 6th, 2004, 02:44 AM
#3
Thread Starter
Frenzied Member
OK. I have a parent MDI form called frmMain (IsMDIContainer=True)
frmMain has buttons on it eg.Button1 which shows Form1
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Form1 As New Form1
Form1.MdiParent = Me
Form1.Show()
End Sub
When I click on Button1, Form1 appears OK but the buttons on frmMain are still visible and accessible and any controls in the same position on Form1 are hidden and are not accessible.
I have solved part 2 of the problem by removing:
from the button click procedure and declaring it at form level. Therefore, only one instance of the form is created and when you call the show method, that instance is shown rather than a new instance.
-
Mar 6th, 2004, 02:48 AM
#4
Anything on the MDIParent superceeds the child so you'll need to manually change the Visible property of the things you don't want to appear on the main form once the child is shown.
-
Mar 6th, 2004, 03:06 AM
#5
Thread Starter
Frenzied Member
The solution might be to have a separate main menu form which is a child form itself.
I thought I had solved the second problem by declaring the form at form level. This worked until I closed the form and it wouldn't let me re-open it.
"Cannot access a disposed object named Form1"
I tried adding:
If IsNothing(Form1) Then Form1= New Form1
to the button click procedure but IsNothing(Form1) returns False after the first call to Form1.Show even after Form1 is closed.
-
Mar 6th, 2004, 03:16 AM
#6
Fanatic Member
RobertX
Upload a screen shot of the mdi parent before loading the child then one of when the mdi child is loaded, this will make it clearer to what your trying to do ?
I guess with the buttons your trying to make some kind of toolbar, if so theres already a toolbar control which dosnt allow the mdi area to be infront of the control
also to control opening child forms use this sub , the sub in the parent MDI
VB Code:
Public Sub ShowSingleInstance(ByVal childType As Type)
For Each child As Form In Me.MdiChildren
If child.GetType Is childType Then
child.Activate() 'already loaded so bring to foreground
Return
End If
Next
Dim frm As New Form
frm = Activator.CreateInstance(childType) 'not loaded yet so create
frm.MdiParent = Me
frm.Show()
End Sub
use this to call the sub within the parent
VB Code:
ShowSingleInstance(GetType(FORM_NAME))
If opening a mdi child from another child you need to do this
add a module to your project Call it something like Settings
define the parent from like this
VB Code:
Public MainApp as NAME_OF_THE_MD_PARENT
also when you load the main parent add
then you can use
VB Code:
MainApp.ShowSingleInstance(GetType(FORM_NAME))
that will allow you to open from another child and set the parent
Last edited by carlblanchard; Mar 6th, 2004 at 03:25 AM.
I am curretly building a defect management system for software and web developers,
If you wana try it out (beta test) and keep it for free just send me a message
-
Mar 6th, 2004, 03:40 AM
#7
Thread Starter
Frenzied Member
I have used the MDI form to place buttons onto which forms my main menu. I will have to find some other way of doing this - either by hiding the buttons when a child is shown or by having the main menu on a separate child form.
I just want to be able to show a form without creating multiple instances of the form and without having to show it modally.
-
Mar 6th, 2004, 03:42 AM
#8
Fanatic Member
So you are using buttons as a toolbar, ?
I am curretly building a defect management system for software and web developers,
If you wana try it out (beta test) and keep it for free just send me a message
-
Mar 6th, 2004, 03:55 AM
#9
Thread Starter
Frenzied Member
Yes. There would be too many items to fit across a tool bar unless each sub-item of the tool bar displayed a sub-menu on a another form.
-
Mar 6th, 2004, 06:14 AM
#10
Fanatic Member
Ok in that case what i would do is use the sandbar control
http://www.divil.co.uk/net/controls/sandbar/
It allows for buttons, combo boxes, and best of all drop down menus, i think for what you are trying to do the drop down menus within a tool bar would work great, allowing you to orgranise all the buttons and fitting everything within the parent
Hope it helps
I am curretly building a defect management system for software and web developers,
If you wana try it out (beta test) and keep it for free just send me a message
-
Mar 6th, 2004, 10:36 AM
#11
-
May 30th, 2005, 08:07 PM
#12
Thread Starter
Frenzied Member
Re: Mdi Problem
I have been using this code to activate MDI child forms:
VB Code:
Public Sub ShowSingleInstance(ByVal childType As Type)
For Each child As Form In Me.MdiChildren
If child.GetType Is childType Then
child.Activate() 'already loaded so bring to foreground
Return
End If
Next
Dim frm As New Form
frm = Activator.CreateInstance(childType) 'not loaded yet so create
frm.MdiParent = Me
frm.Show()
End Sub
If the MDI child is either not loaded or is behind another MDI child, its Form_Activated event will fire. However, if the MDI child is already the uppermost form, the Form_Activated event does not fire even if I explicitly call frm.Activate at the end of the above code.
How do I make the Form_Activated event fire regardless of the MDI child form's current state?
-
May 30th, 2005, 08:51 PM
#13
Re: Mdi Problem
The forms Activated event is raised when the form becomes active. If your form is not becoming active because it is already active then there is no event. You could place a public method in the child form that does what you currently do in the Activated event handler. You would then call this method from the parent and call Me.Activate() within this method. That way the child form gets activated if it is not already active and your code gets executed even if the child was already active.
-
May 30th, 2005, 08:57 PM
#14
Thread Starter
Frenzied Member
Re: Mdi Problem
Thanks. I think that is the solution.
-
Jun 3rd, 2005, 03:58 AM
#15
Thread Starter
Frenzied Member
Re: Mdi Problem
How do I reference a specific child form from the MDI parent form? I can reference the active form with Me.ActiveMdiChild. ..... but that does not give me access to the form's methods as it does not reference the form specifically.
-
Jun 3rd, 2005, 05:14 AM
#16
Re: Mdi Problem
The ActiveMdiChild property is of type Form. You can use CType to cast it as the desired type, which will then give you access to all its members. If you are only going to have a single instance of each type of child form, assign each to a variable in the parent. That way you always have access to them. Each time you want to show a child, you check whether its corresponding variable is Nothing or IsDisposed and, if so, create a new instance, otherwise just activate the existing instance.
VB Code:
Private child As ChildForm
If child Is Nothing OrElse child.IsDisposed Then
child = New ChildForm
child.Show()
End If
'Your method.
child.ActivateForm()
Last edited by jmcilhinney; Jun 3rd, 2005 at 05:17 AM.
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
|