Challenges in menu creation

Aug 27, 2013 at 11:26 PM
Hello to my fellow ShowUI users....

I'm currently trying to build a little UI for a program I'm doing in PowerShell.

  • I'm having issues with my buttons in the main menu. The buttons are in the middle of the field, and there seems to be a space to the left of the button.
    # Start by importing the ShowUI module
Import-Module -Name ShowUI
. ..\..\Functions\AskForFolder;
. ..\..\Functions\AskForFile;

# Create window
New-Window -Name OKAY -ControlName OKAY -Title "OKAY OKAY" -WindowStartupLocation CenterScreen -Content {
# Create a grid to hold the main menu
    New-Grid -Rows Auto -Children {
# The main menu
        New-Menu -IsMainMenu -IsEnabled -Height 25 -Items {
    # "OKAY" menu creation
            New-MenuItem -Name OKAY -Header "OKAY" -Items {
                    ## Buttons in the main menu
                        # Read Files button
                New-Button -Name ReadFiles "Find folder" -On_Click {
                        # Read file button
                New-Button -Name ReadFile "Find file" -On_Click {
                        # Exit button
                New-Button -Name Exit "Exit" -On_Click {
                    $window | Close-Control;
            } # end off "OKAY" menu
    # Help menu
            New-MenuItem -Name Help -Header "Help" -Items {
                    ## Buttons in the Help menu
                        # Help button
                New-Button -Name Help "Help" -On_Click {
                    Invoke-Item -Path .\Help\Help.txt
                        # About button
                New-button -Name About "About" -On_Click {
                    New-TextBlock -Text "Programmed by: Bingchong.
                    " -Show
            }; # end off "Help" menu
        }; # end off main menu
    } # end off new-grid holding the main menu
} -Show; # end off window creation. Window called to be shown
  • I tried to take a screenshot of how it looks, but when I do that the menu looses focus and pops back in again - haven't found out how to avoid that yet ;-)
  • So please run the code and I think you'll see what I mean. The buttons inside the OKAY and HELP menu are not looking to good. I would like them to fill out the gray space, instead of being centered.
I have looked all over this site under source code, to see examples. The MSDN WPF site to be inspired. StackOverFlow to see if someone have had the same problem. Been pretty much everywhere.

Also, I tried playing around with the code, trying different attributes/parameters on the new-menu, new-menuitem and the new-button.

I hope someone in here can help. Thank you very much. Looking forward to hear your suggestions.

Aug 28, 2013 at 8:28 AM
Edited Aug 28, 2013 at 8:30 AM
Short version:
  1. The empty space on the left is where the icons would go (just look at any other menu, like the one you get for Alt+Space).
  2. You shouldn't be putting BUTTONS in your menuitems, you should be putting more menuitems.
Try this sample out:
# There are three kinds of menu items in WPF (and thus, in ShowUI):
Window -Width 300 -Height 150 {
   DockPanel {
      Menu -Dock Top -Height 20 {
         MenuItem -Header "_File" {
            ####### First (and most complicated) MenuItem type:
            ## Menu items hooked to built-in commands that require manual handling
            ## NOTE: These are "ApplicationCommands" defined here:
            MenuItem -Command "New"
            MenuItem -Command "Open"
            MenuItem -Command "Save"
            ####### Second most complicated MenuItem type:
            ## Menu items with no Command, and a click handler instead.
            ## There's no "Exit" command (Alt+F4 is handled by the OS)
            ## So if you you want an EXIT menu item, you have to make it up:
            MenuItem -Header "E_xit" -On_Click { Close-Control $window }
         MenuItem -Header "_Edit" {
            ####### Lastly, the most simple MenuItem type:
            ## MenuItems with built-in basic handling.
            ## These are more "ApplicationCommands" that are handled by the RichTextBox.
            ## This sort-of stuff is *why* WPF uses routable Commands: they can be "handled" by any control...
            MenuItem -Command "Undo"
            MenuItem -Command "Redo"
            MenuItem -Command "Select All"
            MenuItem -Command "Cut"
            MenuItem -Command "Copy"
            MenuItem -Command "Paste"
            MenuItem -Command "Delete"
         MenuItem -Header "F_ormat" {
            ## These are "EditingCommands" and are also handled magically by the RichTextBox.
            ## There are lots more of them, but I don't have the patience to add them all:
            MenuItem -Header "_Bold" -Command "ToggleBold"
            MenuItem -Header "_Italic" -Command "ToggleItalic"
      ## BUGBUG: My RichTextBox command is broken, it's missing it's parameters!
      ##         I just FIXED this for ShowUI 1.5 (coming very soon) [53dc134747f0]
      ## Workaround: Set the Name by hand, since that's all I cared about
      RichTextBox | % { $_.Name = "Content"; $_ }

   } -On_Load {
      ## In the load event, hook up command bindings for commands you need to handle:
      ## You must handle the CanExecute. Set CanExecute to True to enable the menu item
      $this.CommandBindings.Add( (
         CommandBinding -Command "New" `
                     -On_CanExecute {$_.CanExecute = $Content.Document.Blocks.Count -gt 0 }   `
                     -On_Executed { $Content.Document.Blocks.Clear() } 
      ) ) | Out-Null
} -Show
Aug 28, 2013 at 1:03 PM
Hello Jaykul,

Very nice. Thank you for taking the time. I'll try to build from that. Again thank you a million.

Nov 30, 2013 at 1:41 PM

Is there a way to add custom commands to a menuitem (e.g. calling a function) instead of using an ApplicationCommand?

Thank you!
Dec 3, 2013 at 9:44 AM
Ok I'm an idiot its simply the second type of MenuItem like this:
MenuItem -Header "dostuff" -On_Click{doStuff_FunctionI}
Dec 3, 2013 at 4:06 PM
I wouldn't say you're an idiot -- you figured it out while I was out for the holiday and sick not paying attention :-)

I really need to try and get some examples in the function help for some of these common scenarios. But first, I need to do a 1.5 release...
Dec 19, 2013 at 11:59 AM
Edited Dec 19, 2013 at 1:02 PM
Its hardly your fault, being sick and that in your holidays :(

Yes some additional documentation would be very nice.

But I really appreciate showui. Its very easy to use and pefect for little or maybe even migger projects that need endusers to be able to interact with your scripts. I have used a lot of script languages but I never felt as comfortable as with powershell: Now having the ability to use it to build guis that easily is just great!

Thx a lot for your work!