locked
How to add some short text to a menu item RRS feed

  • Question

  • I have one main menu item called "Options" containing items for various user options which are contained in my.settings. Some of the options are boolean values; others are strings. For the string options, I want to be able to display the string value to the right of the menu items. I'm doing it like this now: "mnuOptionsSample.caption = "&Sample  (" & my.setting.sample & ")". This works, but the problem is that all the string values in the menu are not well aligned with each other. Is there a way to fix this? I tried to use tabs instead of spaces to separate the menu items from their values, but that doesn't work either.

    Robert Homes

    Sunday, October 15, 2017 2:26 PM

All replies

  • Hi

    Not exactly sure what you are asking, but, I think that  .PadRight / .PadLeft may be of use to you.

    Example (just using Labels, but applies just the same)

    ' Form1 with Label1 and Label2
    Option Strict On
    Option Explicit On
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Label1.BorderStyle = BorderStyle.FixedSingle
            Label2.BorderStyle = BorderStyle.FixedSingle
    
            Label1.Text = "This string is PadRight".PadRight(40)
            Label2.Text = "This string is PadLeft".PadLeft(40)
        End Sub
    End Class
    


    Regards Les, Livingston, Scotland

    Sunday, October 15, 2017 2:43 PM
  • Can you show an image of what you get and what you want to see?

    Public Class Form5
        Dim menuList As New List(Of String)
    
        Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'MenuStrip1.Font = New Font("Lucida Console", 12, FontStyle.Regular)
            MenuStrip1.Font = New Font("Consolas", 12, FontStyle.Regular)
            menuList.Add("Item 1")
            menuList.Add("This Item two")
    
        End Sub
    
        Private Sub FileToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles FileToolStripMenuItem.Click
    
    
            Dim addList As New List(Of String)
            addList.Add("Cat")
            addList.Add("Rabbit")
    
    
            Item1ToolStripMenuItem.Text = menuList(0).PadRight(20) & "  " & addList(0).PadRight(10)
            ThisItemToolStripMenuItem.Text = menuList(1).PadRight(20) & "  " & addList(1).PadRight(10)
    
        End Sub
    End Class


    PS Maybe some code.
    Sunday, October 15, 2017 3:11 PM
  • That doesn't work for me. I think the problem is that if you pad the entire menu caption, then the string values don't align with each other on the left, the who caption (menu caption AND string value) is padded. I tried padding just the values, but that didn't work either. I think there's something fundamentally different between labels and menu captions.

    Robert Homes

    Sunday, October 15, 2017 3:55 PM
  • Doesn't work - at least with the fonts I'm using. Maybe with a fixed width font (like CourierNew) it would work. Here's what I tried:

    mnuOptionsFont.Caption = "&Font".PadRight(20) & ("(" & Fo.Name & ")")

    I don't see the need for padding the value (Fo.name in this case). Padding the menu item itself should be enough to force all the values to line up, but padding with spaces I don't think works with variable width fonts. Am I on the right track? If so, got any other ideas?


    Robert Homes

    Sunday, October 15, 2017 4:02 PM
  • Hi

    When replying to a particular post, you need to indicate which one - best way is to either indicate poster name, or, use quote.


    Regards Les, Livingston, Scotland

    Sunday, October 15, 2017 4:05 PM
  • but padding with spaces I don't think works with variable width fonts. Am I on the right track? If so, got any other ideas?

    I will answer your question if you answer mine (and Leshay's).  :}

    But yes that is right.

    Sunday, October 15, 2017 4:10 PM
  • Try this too:

       mnuOptionsSample.Text = "&Sample"

       mnuOptionsSample.ShowShortcutKeys = True

       mnuOptionsSample.ShortcutKeyDisplayString = "some text"

     

    The additional text will be right-aligned.

    If you have a different kind of menu (not usual MenuStrip), then give details.

    Sunday, October 15, 2017 6:27 PM
  • Robert,

    Just an idea here: If your samples are static then rather than showing them as strings, create an image and show the image as the item image in the menu:

    Obviously that's not what you want but maybe it's something to consider?


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Sunday, October 15, 2017 6:36 PM
  •  You could create your own Renderer for your MenuStrip or ToolStrip and just override the OnRenderItemText sub in it so you can control the drawing of the text in each item.  This may be a little more difficult but,  it will give you greater control over drawing anything in the menu and giving it your own look and feel.

     You can just use a string separator between the item's Text and your 'setting value' text.  The longer it is,  the more space that will be between the item's text and the setting value text.

    Public Class Form1
        Private separator As String = "____________"

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            MenuStrip1.Renderer = New MyMenuRenderer

            'separate the Items Text and the 'setting' with the (separator) string.
            UserAction1ToolStripMenuItem.Text = "Sample" & separator & My.Settings.Sample
            UserAction2ToolStripMenuItem.Text = "Running" & separator & My.Settings.Running.ToString
            UserAction3ToolStripMenuItem.Text = "Speed" & separator & My.Settings.Speed.ToString
        End Sub
    End Class


    Public Class MyMenuRenderer
        Inherits ToolStripProfessionalRenderer

        Protected Overrides Sub OnRenderItemText(e As ToolStripItemTextRenderEventArgs)
            Dim txt() As String = e.Text.Split(New Char() {"_"c}, StringSplitOptions.RemoveEmptyEntries)
            If txt.Length > 0 Then
                Dim txtclr As Color = If(e.Item.Selected, Color.Blue, Color.Black)
                Dim LeftRect As Rectangle = If(txt.Length > 1, New Rectangle(e.TextRectangle.Left, e.TextRectangle.Top, e.TextRectangle.Width - (e.TextRectangle.Width \ 4), e.TextRectangle.Height), e.TextRectangle)
                TextRenderer.DrawText(e.Graphics, txt(0).Trim, e.TextFont, LeftRect, txtclr, TextFormatFlags.Left Or TextFormatFlags.VerticalCenter Or TextFormatFlags.SingleLine)
                If txt.Length > 1 Then
                    Dim RightRect As New Rectangle(e.TextRectangle.Left + ((e.TextRectangle.Width \ 4) * 3), e.TextRectangle.Top, (e.TextRectangle.Width \ 2), e.TextRectangle.Height)
                    TextRenderer.DrawText(e.Graphics, txt(1).Trim, e.TextFont, RightRect, Color.Red, TextFormatFlags.Left Or TextFormatFlags.VerticalCenter Or TextFormatFlags.SingleLine)
                End If
            End If
        End Sub
    End Class
     

     Notice the ability to change different parts of the text to different colors...


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Monday, October 16, 2017 4:47 PM Fixed LeftRect width calculation
    Sunday, October 15, 2017 8:11 PM
  •  ...  Here is another way you could draw the text of the items in the Renderer which does about the same thing as my prior example but,  this uses a Left alignment for the item's Text and a Right alignment for the setting value text instead of using two different rectangles to draw them.

     You can just replace the MyMenuRenderer class in my prior example with this....

    Public Class MyMenuRenderer
        Inherits ToolStripProfessionalRenderer
    
        Protected Overrides Sub OnRenderItemText(e As ToolStripItemTextRenderEventArgs)
            Dim txt() As String = e.Text.Split(New Char() {"_"c}, StringSplitOptions.RemoveEmptyEntries)
            If txt.Length > 0 Then
                Dim txtclr As Color = If(e.Item.Selected, Color.Blue, Color.Black)
                TextRenderer.DrawText(e.Graphics, txt(0).Trim, e.TextFont, e.TextRectangle, txtclr, TextFormatFlags.Left Or TextFormatFlags.VerticalCenter Or TextFormatFlags.SingleLine)
                If txt.Length > 1 Then
                    TextRenderer.DrawText(e.Graphics, txt(1).Trim, e.TextFont, e.TextRectangle, Color.Red, TextFormatFlags.Right Or TextFormatFlags.VerticalCenter Or TextFormatFlags.SingleLine)
                End If
            End If
        End Sub
    End Class
    


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Sunday, October 15, 2017 8:44 PM
    Sunday, October 15, 2017 8:43 PM