Results 1 to 21 of 21

Thread: MDI with Toolbar

  1. #1

    Thread Starter
    Hyperactive Member spoiledkid's Avatar
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    437

    MDI with Toolbar

    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.

  2. #2
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    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.
    Attached Files Attached Files
    Whadayamean it doesn't work....
    It works fine on my machine!

  3. #3

    Thread Starter
    Hyperactive Member spoiledkid's Avatar
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    437
    I do not think you understood the question very well ...

  4. #4
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    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!

  5. #5

    Thread Starter
    Hyperactive Member spoiledkid's Avatar
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    437
    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' ...

  6. #6
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    You want to click the tool bar button and execute a procedure contained in the child form
    Whadayamean it doesn't work....
    It works fine on my machine!

  7. #7

    Thread Starter
    Hyperactive Member spoiledkid's Avatar
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    437
    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 ?

  8. #8
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    I have updated the example I sent you yesterday to include code that executes from the child form. I've attached it here.
    Attached Files Attached Files
    Whadayamean it doesn't work....
    It works fine on my machine!

  9. #9

    Thread Starter
    Hyperactive Member spoiledkid's Avatar
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    437
    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.

  10. #10
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    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!

  11. #11

    Thread Starter
    Hyperactive Member spoiledkid's Avatar
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    437
    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.

    tx for bearing with me. Really appreciated.

  12. #12
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Ok, sorry this took so long, I just got back from lunch

    here are your 15 seconds of code
    Attached Files Attached Files
    Whadayamean it doesn't work....
    It works fine on my machine!

  13. #13

    Thread Starter
    Hyperactive Member spoiledkid's Avatar
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    437
    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.

  14. #14

    Thread Starter
    Hyperactive Member spoiledkid's Avatar
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    437
    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:
    1. Imports System.Reflection
    2.  
    3.         Dim activeForm As Form = Me.ActiveMdiChild
    4.         Dim formType As Type = Me.ActiveMdiChild.GetType
    5.  
    6.         Dim formParams(0) As Object
    7.         Dim method As MethodInfo
    8.  
    9.         Try
    10.             'Every window form/class must implement this method. even if its not going to use it.
    11.             method = formType.GetMethod("ToolBarHandler")
    12.             formParams(0) = e.Button.Text
    13.             method.Invoke(activeForm, formParams)
    14.         Catch ex As Exception
    15.             MessageBox.Show(ex.Message)
    16.         End Try

  15. #15
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    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?
    Whadayamean it doesn't work....
    It works fine on my machine!

  16. #16

    Thread Starter
    Hyperactive Member spoiledkid's Avatar
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    437
    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.

  17. #17
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    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:
    1. Dim fChild As Form = Me.ActiveMdiChild
    2.         Dim tChild As Type = Me.ActiveMdiChild.GetType
    3.         Dim Method As MethodInfo
    4.  
    5.         Select Case e.Button.Tag
    6.             Case "Add" : Method = tChild.GetMethod("AddItem")
    7.             Case "Edit" : Method = tChild.GetMethod("EditItem")
    8.             Case "Save" : Method = tChild.GetMethod("SaveItem")
    9.             Case "Delete" : Method = tChild.GetMethod("DeleteItem")
    10.         End Select
    11.         Try
    12.             Method.Invoke(fChild, Nothing)
    13.         Catch ex As Exception
    14.             MessageBox.Show(ex.Message)
    15.         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!

  18. #18

    Thread Starter
    Hyperactive Member spoiledkid's Avatar
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    437
    I see all the points ...

    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.

  19. #19
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Very good, I give that code an A+
    Whadayamean it doesn't work....
    It works fine on my machine!

  20. #20

    Thread Starter
    Hyperactive Member spoiledkid's Avatar
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    437
    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:
    1. Option Strict On
    2.  
    3. Interface IListener
    4.     Sub ListenerMethod(ByVal action As String)
    5. End Interface

    Now in each form where you need to handle the toolbar actions you will implement this interface.

    VB Code:
    1. Public Class Test
    2.  
    3. Implements IListnener
    4.  
    5.  
    6.     Public Sub ToolBarListener(ByVal actionType As String) Implements IListener.ListenerMethod
    7.         Dim language_M As New Language_M
    8.  
    9.         Select Case actionType.ToUpper
    10.             Case "NEW"
    11.                 language_M.ShowDialog()
    12.         End Select
    13.     End Sub

    From the MDI toolbar click event, all you need is this ...

    VB Code:
    1. Private Sub tbrActions_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles tbrActions.ButtonClick
    2.         Dim IListenerInstance As IListener
    3.         IListenerInstance = CType(Me.ActiveMdiChild, IListener)
    4.         IListenerInstance.ListenerMethod(e.Button.Text)
    5.     End Sub

    Simple, effective and of course, Object Oriented.

  21. #21
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Hmmmmm....
    I had a box of gold stars around here somewhere, I guess I left them at home . . .
    Whadayamean it doesn't work....
    It works fine on my machine!

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