I have an MDI with Toolbar. Toolbar has standard functions Like New, Open, Delete.
All these operations need to be performed on the child windows of MDI. I can get the Child windows using me.ActiveMdiChild property of MDI but how do I execute a standard method say 'ToolBarHandler' which will be in all child forms and this function will implement the MDI toolbar functionality in all forms respectively. So how do I call this function for active child window from MDI ??? Should I use Delegates, if yes, please post some example.
Instead of just referencing your child form by using me.ActiveMdiChild property of the MDI container, try creating an object variable of the type child form and when you load a new instance of that form, loop through the collection of forms loaded in the MDI container until you find the one that is your current form. You can then reference properties and methods of that form instance.
I attached a simple example project.
Whadayamean it doesn't work....
It works fine on my machine!
perhaps not, but just to clarify my understanding, you have an MDI Container form that will open one or more child forms. The MDI container has a toolbar with several buttons on it. Using the active child of the MDI container, you want the buttons on the tool bar of the MDI container to execute code on the active child. Is that not correct?
Whadayamean it doesn't work....
It works fine on my machine!
Originally posted by CyberHawke perhaps not, but just to clarify my understanding, you have an MDI Container form that will open one or more child forms. The MDI container has a toolbar with several buttons on it. Using the active child of the MDI container, you want the buttons on the tool bar of the MDI container to execute code on the active child. Is that not correct?
everything except 'not execute code on active child' but 'execute code from active child' ...
Originally posted by CyberHawke You want to click the tool bar button and execute a procedure contained in the child form
Thats correct, for example, all child forms (classes) will have a sub called "Open". I would like it to execute when user clicks on the open button in the toolbar which is on MDI form ... hope I explained good this time. Can you help it here ?
You are doing it the document way, its not that way for me. OK, I will try to explain again.
Now in this project, Add another windows form, call it frmXYZ. Put the WriteText method in this form as well. Open it from MDI somehow and open the frmChild form. Now both forms have WriteText method. Now when you click on execute code, execute the writetext method from active form.
You will see now you cannot HardCode ..
Private oChild As frmChild.
Right now you are just creating new forms by using the instance of frmChild. So you can do what you are doing. But I am not doing that. Its not a instance, its totally different form.
The concept is the same, and you can modify the code I sent you to:
1. Limit the instances of any given child type
2. Execute code in the current MDI child based on it's name so that the code executed is child specific
I'll come up with a quick example if you need, but the basics are in the example I sent
Whadayamean it doesn't work....
It works fine on my machine!
Originally posted by CyberHawke The concept is the same, and you can modify the code I sent you to:
1. Limit the instances of any given child type
2. Execute code in the current MDI child based on it's name so that the code executed is child specific
I'll come up with a quick example if you need, but the basics are in the example I sent
well, i would need a quick example.
make a mdi.
make 3 windows forms, call them form1, form2, form3.
add a method to each called 'Test' which displays the form name.
add a toolbar to mdi
add a button called 'Call'
when clicked, it should call the Test function of currently activated form.
Believe me, I really appreciate your efforts. But this I already know. Now you are creating separate form objects and matching them like this ...
Select Case Me.ActiveMdiChild.Name
Case "MDIChild1"
so If I have 10 forms I will be writing 10 such Case's .. plus my toolbar has 10 buttons, I will have to do this thing for 10 buttons, because they all call different functions. This is VB6 way of doing things .. Isn't there any better way .. I sure hope the heck there is ... I will keep looking and will personally PM you if I could find one. Thanks for your help.
Cyber, This is what I wanted and finally achieved. All child forms have a method call ToolBarHandler, which handles the toolbar button's of the MDI. In other words, when you click the New button, toolbar handler will be called here for the active form with button.text as parameter. And in toolbarhandler method in the activeform, I will take this parameter and do whatever I want. This way I can implement as many buttons as I like in all child forms without having to explicitly checking and creating child form objects. Only 10 lines of code does it all and does it VB.NET way. But thanks for your attempts.
VB Code:
Imports System.Reflection
Dim activeForm As Form = Me.ActiveMdiChild
Dim formType As Type = Me.ActiveMdiChild.GetType
Dim formParams(0) As Object
Dim method As MethodInfo
Try
'Every window form/class must implement this method. even if its not going to use it.
Originally posted by CyberHawke Nice work, I had done some similar work, but not with an MDI application, so now I have a question
Why are you passing the button text as a parameter to your method?
That's entirely optional, if your function (ToolBarHandler in this case) needs to know (for some reason) which button was pressed, you can easily pass button text as parameter and then in Function Implementation of ToolBar you can do whatever you want to do with that parameter.
Furthurmore, if you add a new button tomorrow, your MDI implementation of Toolbar remains untouched. All you have to do is check in form implementation of ToolBarHandler, check the new button text and do whatever you like.
Ok, so you are just determining which button was pressed in your child form instead of your parent form. I don't actually see the point there, but, if it works for you . . . awesome
I would prefer to have my mdi form choose which button was pressed and then call the appropriate method in the active child window.
VB Code:
Dim fChild As Form = Me.ActiveMdiChild
Dim tChild As Type = Me.ActiveMdiChild.GetType
Dim Method As MethodInfo
Select Case e.Button.Tag
Case "Add" : Method = tChild.GetMethod("AddItem")
Case "Edit" : Method = tChild.GetMethod("EditItem")
Case "Save" : Method = tChild.GetMethod("SaveItem")
Case "Delete" : Method = tChild.GetMethod("DeleteItem")
End Select
Try
Method.Invoke(fChild, Nothing)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
but I do appreciate the refresher on the Method.Invoke handler, it has been a while
Whadayamean it doesn't work....
It works fine on my machine!
My MDI does not have to know which methods are implemented in which class. The methods should be absolutely abstract for MDI. MDI's job is to issue command's to all childs telling which button is pressed. It does not have to know what method to call ... you are enforcing MDI to call specific methods. So if for instance I decide not to implement method "Open" in an particular class, you will still try and execute it from the MDI which in turn will raise the error. Which is entirely unneccesary.
Well actually, I wrote even better way. No namespace required. Using pure Object Orientation you can do this too. Using Interfaces.
Create a Interface called IListener.
VB Code:
Option Strict On
Interface IListener
Sub ListenerMethod(ByVal action As String)
End Interface
Now in each form where you need to handle the toolbar actions you will implement this interface.
VB Code:
Public Class Test
Implements IListnener
Public Sub ToolBarListener(ByVal actionType As String) Implements IListener.ListenerMethod
Dim language_M As New Language_M
Select Case actionType.ToUpper
Case "NEW"
language_M.ShowDialog()
End Select
End Sub
From the MDI toolbar click event, all you need is this ...
VB Code:
Private Sub tbrActions_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles tbrActions.ButtonClick