How do you instantiate a Toolbar in code?

  1. 4 months ago

    I want to be able to enable/disable toolbar buttons from within the ContentsChanged event of myWindow (subclass of Window) but to do that, my understanding is that I must instantiate the toolbar in code rather than drag it into the layout (since myWindow doesn't have a layout editor).

    I can't seem to figure this out so I started with an empty project and Window1 to test it but I'm still stuck.

    Here are the steps (starting with an empty Xojo Desktop project):

    1. Add a Toolbar to the project... "Toolbar1"
    2. Add a ToolButton to Toolbar1... "ToolItem1"
    3. Add a property to Window1... myToolbar As Toolbar1
    4. Instantiate the toolbar in the Open event handler of Window1... myToolbar = New Toolbar1

    Run the program... no Toolbar.. why?

  2. Philip C

    Jun 9 Pre-Release Testers, Xojo Pro Cooroy, QLD, Australia

    Learning myself
    https://docs.xojo.com/UserGuide:Desktop_Toolbars

  3. Michel B

    Jun 9 Pre-Release Testers, Xojo Pro RubberViews.com

    You got an example at http://docs.xojo.com/Toolbar

  4. Jean-Yves P

    Jun 10 Pre-Release Testers, Xojo Pro Europe (France, Besançon)

    @Kristin G Run the program... no Toolbar.. why?

    because you instanciate the toolbar as a property of myWindow, and not as a toolbar of myWindow
    there should be a property toolbar, or a method SetWindowToolbar, but there isn't.
    you should add the toolbar in the window, with the IDE , and with all needed buttons, as non-visible
    then set each visible button you need with code.

  5. Paul S

    Jun 10 Pre-Release Testers, Xojo Pro Europe (Netherlands, Den Haag)

    @Philip C
    Your link isn't working.
    The link shown is correct, but when you hover/click the link you can see there is an extra letter (m) at the end of the url.
    This is the correct link without the extra m.
    https://docs.xojo.com/UserGuide:Desktop_Toolbars

  6. Emile S

    Jun 10 Europe (France, Strasbourg)
    Edited 4 months ago

    It would be good for new comers (or those who never use the feature [Desktop Toolbars for example]) to put a link to a sample project at the page bottom there: Desktop Toolbars .

  7. @Michel B You got an example at http://docs.xojo.com/Toolbar

    @Philip C Learning myself
    https://docs.xojo.com/UserGuide:Desktop_Toolbars

    @Emile S It would be good for new comers (or those who never use the feature [Desktop Toolbars for example]) to put a link to a sample project at the page bottom there: Desktop Toolbars .

    I've read over the documentation thoroughly before submitting my question here and neither of these doc pages talk about how to instantiate a Toolbar in code. They provide an example of setting up the ToolButtons in code but the example clearly states that the Toolbar was added to the Window in the IDE... not in code.

    @Jean-YvesPochez because you instanciate the toolbar as a property of myWindow, and not as a toolbar of myWindow
    there should be a property toolbar, or a method SetWindowToolbar, but there isn't.
    you should add the toolbar in the window, with the IDE , and with all needed buttons, as non-visible
    then set each visible button you need with code.

    The whole point is to NOT add the toolbar with the IDE.

    I followed the instructions on this page... AddHandler ... only I attempted to create a Toolbar instead of a Timer.

  8. Here is a little background to the issue that I'm having...

    Almost 50% of the windows in my project will be 'edit' windows... meaning that they will present an object as a form for editing. If the user changes the contents of any of the fields in the form, certain items in the toolbar need to be enabled... such as a 'Save' button (or a 'Reset' button that reverts all fields back to their original state).

    Using the ContentsChanged event of the window, I can easily enable/disable buttons in the toolbar. I simply reference the Toolbar object that was dragged into it in the IDE. No problem. As per the instructions in the docs, I can even set up all the buttons in the Open event of the toolbar.

    However, I'll need this functionality in 50% of my windows.

    Someone smarter than myself Anytime we must type the same code more than once, we should think about abstraction

    So, it would seem to be a good idea to abstract this functionality into a subclass of Window. I can create a subclass of Window and call it, "myWindow". I can handle the ContentsChanged event in myWindow but when I attempt to make reference to the toolbar.... there is no toolbar! Because it's a subclass, I have no layout into which I can drag a toolbar. I must instantiate the toolbar in code so that I can reference it in my events and methods.

    This brings me to my present dilemma... how do you instantiate a toolbar in code?

  9. Michel B

    Jun 10 Pre-Release Testers, Xojo Pro RubberViews.com
    Edited 4 months ago

    @Kristin G I've read over the documentation thoroughly before submitting my question here and neither of these doc pages talk about how to instantiate a Toolbar in code. They provide an example of setting up the ToolButtons in code but the example clearly states that the Toolbar was added to the Window in the IDE... not in code.

    What you did not understand is that a toolbar dragged to the window can have it's buttons enabled and disabled.

    You then went chasing an untamed ornithoid, ad Start Trek's Data would say.

    Follow *exactly* the example I pointed to at http://docs.xojo.com/Toolbar , and add a button to the window with this code inside:

    OpenButton.enabled = not OpenButton.enabled

    That should help you achieve what you want to do.

  10. -image-

    @Michel B What you did not understand is that a toolbar dragged to the window can have it's buttons enabled and disabled.

    Follow *exactly* the example I pointed to at http://docs.xojo.com/Toolbar , and add a button to the window with this code inside:

    OpenButton.enabled = not OpenButton.enabled

    That should help you achieve what you want to do.

    It does not. Perhaps a screenshot of my project would help...

    -image-

  11. Michel B

    Jun 10 Pre-Release Testers, Xojo Pro RubberViews.com
    Edited 4 months ago

    Kristin, you are still trying what did not work before.

    1. Drag a toolbar to the project, on the left hand side (Toolbar1)
    2. Drag that toolbar to the window (Toolbar11)

    You can then interact with that control as with any other one. See link to the sample project I made below.

    -image-

    toolbar.xojo_binary_project

  12. Dragging a Toolbar to the project and then dragging the resulting Toolbar1 to Window1 works perfectly... as expected... However, how do I do that if I subclass Window to myWindow? There is no layout into which I can drag a Toolbar.

  13. Jean-Yves P

    Jun 10 Pre-Release Testers, Xojo Pro Europe (France, Besançon)

    @Kristin G This brings me to my present dilemma... how do you instantiate a toolbar in code?

    again, you can't !
    I have the same problem as you, want an class editingWindow, with an optionnal toolbar.
    the way I did it is, check in myWindowClass is there is a toolbar with this :

    Public Function HasToolbar(extends w as Window) as Boolean
      Return (w.GetToolbar <> nil)
    End Function

    and this

    Public Function GetToolbar(extends w as Window) as Toolbar
      dim res as Toolbar = Nil
      dim c as Control
      
      for i as Integer = 0 to w.ControlCount-1
        c = w.Control(i)
        if c isa Toolbar then
          res = Toolbar(c)
          exit For
        end if
      next
      
      Return res
    End Function

    then I drag a toolbar in every window instance that needs a toolbar, no toolbar in window that don't need it
    in the abstract myWindow class I check if there is a toolbar then I deal with it enabling, masking what needed.

    you can't create a toolbar by code, as you would do with a timer, it's not permitted by xojo
    (althought it would be nice ...)
    -image-

  14. @Jean-YvesPochez because you instanciate the toolbar as a property of myWindow, and not as a toolbar of myWindow
    there should be a property toolbar, or a method SetWindowToolbar, but there isn't.
    you should add the toolbar in the window, with the IDE , and with all needed buttons, as non-visible
    then set each visible button you need with code.

    Ah... thank you! Sorry, I didn't understand your reply when I first read it. The answer is that it can't be done.

    Although I'm disappointed by this, I am relieved to find out that I'm not going crazy. ;-)

  15. Michel B

    Jun 10 Pre-Release Testers, Xojo Pro RubberViews.com
    Edited 4 months ago

    As I said, you were chasing a wild goose.

    But if I understood right your first post, you wanted to do that only to be able to enable/disable ToolButtons.

    As my little example project shows, it is possible without a whimsical code instantiation :D

  16. Edited 4 months ago

    @Michel B As I said, you were chasing a wild goose.

    But if I understood right your first post, you wanted to do that only to be able to enable/disable ToolButtons.

    As my little example project shows, it is possible without a whimsical code instantiation :D

    I presented a basic example in an attempt to not over-complicate the question. The idea was that what worked on Window1 should also work on myWindow. Of course, if you're only doing it for one window then it wouldn't make sense but now imagine that you have 250 tables in your database... for which you must create both a ListWindow and an EditWindow.

    Toolbars in Xojo are strange. You must first add the Toolbar to the project... but you can't call it "Toolbar"... so, what do you call it? "myToolbar"? "MainToolBar"? "EditToolbar" vs. "ListToolbar"?

    Then, you must drag that into your window... which creates a new thing that you must also name... what do you call it? "ThisWindowsToolbar"? Rename every toolbar to match the content of the window: "UserEditToolbar"?

    "Toolbar1" and "Toolbar11" just seems so amateur.

    I agree with @Jean-YvesPochez , a toolbar should be built into every Window and simply made visible when needed. It seems like such a really basic thing to do. (see what I did there?) ;-)

  17. Jean-Yves P

    Jun 10 Pre-Release Testers, Xojo Pro Europe (France, Besançon)

    @Kristin G but now imagine that you have 250 tables in your database... for which you must create both a ListWindow and an EditWindow.

    I did it the way in my previous post https://forum.xojo.com/conversation/post/440824
    and it works fine. I made an "edittoolbar" I add at each instance of an edit window, and an "listtoolbar" I instance for each list window. I have a global list window, and a global edit window with no toolbar (but I could add one if needed)
    works fine for some times now.

  18. Norman P

    Jun 10 Pre-Release Testers, Xojo Pro great-white-software.com/blog
    Edited 4 months ago

    @Kristin G I presented a basic example in an attempt to not over-complicate the question. The idea was that what worked on Window1 should also work on myWindow. Of course, if you're only doing it for one window then it wouldn't make sense but now imagine that you have 250 tables in your database... for which you must create both a ListWindow and an EditWindow.

    First off I would not create 250 windows to edit tables
    I'd have one windows which could edit any table and one that could lost any table - 2 windows in total
    And be able to open multiple instances of these windows so if I need to edit or list more than one table at a time I can

  19. Newer ›

or Sign Up to reply!