2

I just started a self-paced course in Visual Basic using Visual Studio. One of my assigned problems is to create a form with two buttons. When the form loads, Button1 is enabled and Button2 is disabled. When you click on Button1, Button1 is disabled and Button2 is enabled. When you then click on Button2, Button2 is disabled and Button1 is enabled.

I got that to work easily, so I decided to add an extra challenge to myself. The challenge is that I want to make the disabled button show the text "Disabled" and the enabled button show "Enabled" with the "E" underlined as the hotkey for the button. I set a string variable for the enabled button containing the string "&Enabled" to enable the "E" as the hotkey. The "E" works as the hotkey, but it does not display with an underline.

I have searched the web for a fix to this issue, but I've come up dry. I also tried resizing the buttons to see if the buttons were too small to display the underline. That did not work. I have scrutinized my code, but I really don't know the language well enough to understand why the "E" does not show up with an underline. I am submitting my code and asking for help. I want to understand why this doesn't work the way I expect it to work.

This is the VB.Net code that I wrote for the form using Visual Studio 2019.

    Public Class frmEnabledProblem
    Dim blnButton1Enabled As Boolean = True
    Dim blnButton2Enabled As Boolean = False
    Dim strButton1Enabled As String = "&Enabled"
    Dim strButton1Disabled As String = "Disabled"
    Dim strButton2Enabled As String = "&Enabled"
    Dim strButton2Disabled As String = "Disabled"
    Private Sub frmEnabledProblem_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.KeyPreview = True
        btnButton2.Text = strButton2Disabled
        btnButton2.Enabled = False
        btnButton1.Text = strButton1Enabled
        btnButton1.Enabled = True

    End Sub

    Private Sub btnButton1_Click(sender As Object, e As EventArgs) Handles btnButton1.Click
        blnButton1Enabled = False
        blnButton2Enabled = True
        btnButton1.Enabled = blnButton1Enabled
        btnButton2.Enabled = blnButton2Enabled
        btnButton1.Text = strButton1Disabled
        btnButton2.Text = strButton2Enabled
    End Sub

    Private Sub btnButton2_Click(sender As Object, e As EventArgs) Handles btnButton2.Click
        blnButton1Enabled = True
        blnButton2Enabled = False
        btnButton1.Enabled = blnButton1Enabled
        btnButton2.Enabled = blnButton2Enabled
        btnButton1.Text = strButton1Enabled
        btnButton2.Text = strButton2Disabled
    End Sub
End Class

Thanks for your help.

  • 2
    This is a Windows thing, press the ALT key on your keyboard and it should show up as you expect – JayV Dec 23 '20 at 21:43
  • @JayV, thanks! However, why won't it show up without pressing the ALT key? If I put the hotkey in the button text using the properties viewer, it will show up when the program runs. So why not when I'm changing the text on the fly? – Hreodbeorht Dec 23 '20 at 21:47
  • For the first part - Microsoft made the change and I don't know why, probably visual aesthetics. For the second part - that's not the behaviour I see. The underline is never visible to me without pressing the ALT key while the program is running no matter how it is set (this also includes using your code above) – JayV Dec 23 '20 at 21:55
  • @JayV, thanks for that explanation. When I don't try to change the text, and I'm running in debug mode, the underlines for the hotkeys show up on my computer. Anyway, your comments answered my question. Thank you, again, very much. – Hreodbeorht Dec 23 '20 at 22:20
  • Keyboard cues are enabled [in the Control class](https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/Control.cs,4166) when `DesignMode = true`, otherwise, `ShowKeyboardCues` returns `false`. You can use a Custom Control and add `protected override bool ShowKeyboardCues => true;` (which translates to `Protected Overrides ReadOnly Property ShowKeyboardCues As Boolean = True` in VB.Net). This overrides the Property to always return `True`, so the *underline* is shown also at run-time. In the comments on the code, it's also explained what's going on there. – Jimi Dec 23 '20 at 23:14
  • [This](https://stackoverflow.com/a/54711698/14171304) will make the mnemonics show up continuously without needing to press the `Alt` key. Easy to convert it to vb.net I believe. – dr.null Dec 24 '20 at 00:03
  • As an unrelated tip.. If you're into some off-piste learning, please consider focusing on things like naming variables well. Button1 is a terrible name for a button, because by the time you've got 50 buttons in your app youre going to be constantly looking up "which button is the OK button again? Hang on.. i'll look at the designer.. oh, it's button27". `okButton` would be a much better name. Also, you don't need to prefix your buttons with `btn`, when there is `Button` elsewhere in the name, and especially when the `btn` is followed by `Button`. "button button one" is a truly awful name – Caius Jard Dec 24 '20 at 16:10
  • Prefixing variable names with the type is highly unnecessary in the day and age of intelligent editors like Visual Studio, that can tell you the type of a variable just by pointing to it. `blnButton1Enabled = false` followed by `btnButton1.Enabled = blnButtonOneEnabled` is making the code overly verbose, hard to read and full of wasted effort. `okButton.Enabled = False` works just as well and is considerably more readable. Also, turn on Option Strict, Explicit, and Infer in the project properties, and fix all the resulting errors - it will considerably improve the code quality – Caius Jard Dec 24 '20 at 16:14
  • @CaiusJard, thank you. I learned coding over 40 years ago with FORTRAN and COBOL on punch cards. I let it drop to pursue biochemistry, and now I'm trying to learn new programming languages. I appreciate the tips about naming conventions. 40 years ago, variables could be rather sparsely named, necessitating lots of documentation. I'll work on improving my variable names. – Hreodbeorht Dec 25 '20 at 03:49
  • @Jimi, thanks for the information about the keyboard cues. As I said, I'm just starting this course, and I am early in it. They may cover this information in later lessons, but I've never been content with doing just the homework. I've always wanted to challenge myself to do more and do better. – Hreodbeorht Dec 25 '20 at 03:51

1 Answers1

2

In a nutshell, because this setting in Ease of Access/Keyboard is off on your computer:

enter image description here

Turn it on and restart your app


I wouldn't personally advocate going out of your way to force it on for your app even when it's off elsewhere in the system; just provide the &OK text on your button, and let the user decide if they want it to show like OK or O̲K depnding on their own settings/appetite for how they want their UI

Caius Jard
  • 72,509
  • 5
  • 49
  • 80