B4J Question how to display underscores in MenuItems

Didier99

Member
Licensed User
When adding strings to a menu: MyMenu.MenuItems.Add( "Some_String" ), the underscore is not displayed "SomeString". I believe the underscore is used to define a hotkey as a shortcut, but in my case, the string is a path, and underscores are significant. I note that I can get the original string by copying it to the Tag, but I would prefer that the string were displayed correctly.

How can I display the underscore (and I do not need the hotkey functionality)?
 

William Lancee

Well-Known Member
Licensed User
Longtime User
You can try to replace the underscore in the text with a fake one.
B4X:
    Dim fakeUL As String = " " & Chr(0x0332) & Chr(0x2009)
    Dim s As String = "A_B"
    Log(s.replace("_", fakeUL))
'A_B
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
DisableMnemonicParsing(MenuBar1)

Private Sub DisableMnemonicParsing(Menu As MenuBar)
    For Each m As Menu In Menu.Menus
        DisableMnemonicParsingHelper(m)
    Next
End Sub

Private Sub DisableMnemonicParsingHelper(Menu As Menu)
    For Each m As MenuItem In Menu.MenuItems
        If m Is Menu Then DisableMnemonicParsingHelper(m)
        m.As(JavaObject).RunMethod("setMnemonicParsing", Array(False))
    Next
End Sub

 
Upvote 0

Didier99

Member
Licensed User
Erel to the rescue
Thank you so much!
 
Upvote 0

Didier99

Member
Licensed User
Interesting, not sure how or why it works but that would be useful to just disable parsing for a few menu items while keeping the functionality for the rest.
Thank you
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
It works because unicode 0x0332 is a 'combining' underline. So the space character is underlined.
But the space is a little to short, so add a narrow space after - that is 0x2009

Alternatively you could have used full width low line 0xff3f, but it is a little long.

As I was testing, I realized that you could use 0x0332 for underlining letters on the fly.
I wish I had known that a long time ago.

B4X:
    Dim txt As String = "The Brown Fox Jumped Over the Lazy Dog"
    Log(underlineWords(txt, Array As String("Fox", "Dog")))
'The Brown F̲o̲x̲ Jumped Over the Lazy D̲o̲g̲

B4X:
Private Sub underlineWords(txt As String, underline() As String) As String
    For Each t As String In underline
        Dim sb As StringBuilder
        sb.initialize
        For i = 0 To t.Length - 1
            sb.Append(t.CharAt(i)).Append(Chr(0x0332))
        Next
        txt = txt.replace(t , sb.toString)
    Next
    Return txt
End Sub
 
Last edited:
Upvote 0

Didier99

Member
Licensed User
Wow, good job! Thank you!
The "combining" underline works like the accents on the old French mechanical typewriters: pressing the accent key would print the accent but not move the carriage, so you could add an accent over any other character by typing it first, and if you pressed the accent key and then the space bar, you had an apostrophe. It's multi-purpose! Such mechanical marvel made obsolete by the computers
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…