Why won't the button hotkey show up with an underline in VB? - vb.net

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.

In a nutshell, because this setting in Ease of Access/Keyboard is off on your computer:
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

Related

vb.net textbox not displaying text or colors

I have many applications that reliably display data and colors in text boxes. I copied one working program to make a new application. There are about 10 text boxes that I use for data displays. Some are working - and some are not. Here are 2 samples - the Switch code works in many programs - but here it will put the word "ON" in the box but no color, and then when the switch is off - no change to the textbox. The code seems not to care between the .AppendText or the .text = variable control. I have tried changing the properties of the text box to public but no luck. I have tried adding the .show object, and verified the .visible attribute in the properties view. A similar problem is with the DistanceUnitsOut and the NextImageDistanceOut - I can step through the code and see the values, but the displayed data does not change! I have tried changing the definition of the display to public - no joy!
SwitchStatusBox.clear
SwitchStatusBox.AppendText(SwitchStatusIn)
If SwitchStatusIn = "OFF" Then
SwitchStatusBox.BackColor.Equals(Color.Red)
SwitchStatusBox.Show()
Switch_Is_On = False
Else
SwitchStatusBox.BackColor = Color.Green
SwitchStatusBox.Show()
Switch_Is_On = True
End If
If dDistance > 1.0 Then
'show distance in Miles
DistanceUnitsOut.Clear()
DistanceUnitsOut.Text = "MI"
NextImageDistanceOut.Clear()
NextImageDistanceOut.Text = CType(dDistance, String)
Else
NextImageDistanceOut.Clear()
NextImageDistanceOut.Text = CType(dDistanceFT, String)
DistanceUnitsOut.Clear()
DistanceUnitsOut.Text = "FT"
End If
Private Sub NextImageDistanceOut_TextChanged(sender As Object, e As EventArgs) Handles
NextImageDistanceOut.TextChanged
End Sub
Private Sub DistanceUnitsOut_TextChanged(sender As Object, e As EventArgs) Handles
DistanceUnitsOut.TextChanged
End Sub
To see how the .Equals function works...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim a = TextBox1.BackColor.Equals(Color.Red)
Debug.Print(a.ToString)
End Sub
In my test app this prints False in the Immediate window.
The answer is that some time ago I began using <variablename_OUT> followed by the TEXT = as in :
NextImageDistanceOut.Text = CType(dDistanceFT, String)
what worked was:
NextImageDistanceOut.Appendtext(CStr(dDistanceFT))
I still don't understand why the first did not work - Both require a previous
NextImageDistanceOut.Clear

Chexbox ChekState functionality in Windows Form

I need a checkbox which will ask the user:"Do you need to add an address?" and if user clicks on it then address label and textbox appears on the form ( I mean By default they were invisible and then I have changed this state), I have implemented this functionality by CheckboxSate event but what if the user doesn't click on the checkbox at all, in this case after submission external details I am not able to move forward ( i mean after clicking on the next button my form don't call another subform, but when I check/uncheck checkbox it works correctly), is there any way to edit/update form validation or any default checkbox property in order to get rid of this bag?, here is my CheckState Event's code:
Private Sub AddAddress_CheckedStatrChange_1(sender As Object, e As EventArgs) Handles AddAddress.CheckStateChanged
If AddAddress.CheckState = CheckState.Checked Then
AddAddressLabel.Visible = True
AddAddressTextBox.Visible = True
AddAddressTextBox.Enabled = True
ElseIf AddAddress.CheckState = CheckState.Unchecked Then
AddAddressLabel.Visible = False
AddAddressTextBox.Visible = False
AddAddressTextBox.Enabled = False
End If
END Sub
That's how you would do:
Sub AddFTP_CheckedChanged() Handles AddFTP.CheckedChanged
AddAddressLabel.Visible = Not AddAddressLabel.Visible
AddAddressTextBox.Visible = Not AddAddressTextBox.Visible
AddAddressTextBox.Enabled = Not AddAddressTextBox.Enabled 'It's useless since it's not visible
End Sub

Disable Button If Combox Input Deleted

In my project, I have a few textbox inputs, and some combo boxes with maybe 2 indexed items on a form. There's a button I'm disabling on load if no input is supplied to both textbox inputs, and it works great even if I delete out any text. However, I'm having issues with forcing the combo box to behave in the same manner. This work however:
Private Sub cboPickShirts_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboPickShirts.SelectedIndexChanged
InputCheck_3 = True
If cboPickShirts.SelectedIndex < 0 Then
InputCheck_3 = False
End If
If InputCheck_3 = False Then
btnInputResult.Enabled = False
ElseIf InputCheck_3 = True Then
btnInputResult.Enabled = True
End If
End Sub
I have InputCheck_3 set up as a global variable in a Public Module. On form load, I'm disabling my button and it doesn't enable until I select one of the indexed items. My struggle to get the button disable again if any combo box text is entered and deleted out, leaving it null or empty. Any thoughts on what I'm missing or what I can add to get results? I guess I need a variable or event to notice the change (entering & deletion of text).
The problem you are having is that your SelectedIndexChanged event is not being triggered when you remove the selected item from your ComboBox. I would use the TextChanged event of your TextBox's and ComboBox and give it a common handler and check it that way. Something like this
Private Sub TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, cboPickShirts.TextChanged
EnableCheck()
End Sub
Private Sub EnableCheck()
btnInputResult.Enabled = (String.IsNullOrEmpty(TextBox1.Text) And String.IsNullOrEmpty(TextBox2.Text) And ComboBox1.SelectedIndex = -1)
End Sub
You can also check that the comboBox is NullorEmpty the same way as the textbox's. As it stands right now the combobox will be enabled when the text no longer matches a selection.
One line code
btnInputResult.Enabled = If((cboPickShirts.SelectedIndex<0),False, True)

Backcolor change of radiobutton, with button appearance

I'm creating a laboratory application in VS2013, where the user enters a rack number, and gets back a visual grid of which samples he/she has to weigh for a certain analysis, for that specific rack with samples.
I'm getting the information from an Oracle DB, based on the requested/send SQL string.
I've created radio buttons as a means of selecting/filter the analysis type.
I've given them the appearance of regular buttons, in the object properties.
When a certain radio buttons is clicked, I want to give them a specific color, depending on the type of analysis.
The same color will be used to mark the samples that are to be weighed in the visual grid.
When I test the program, the radio buttons do what they need to do (meaning: getting the correct info from Oracle DB), but I can't seem to manage the backcolor change of the radio buttons.
The backcolor change of the regular buttons (on visual grid) is working correctly.
I trigger the function behind all this, by Function Rbanalysistype (sender as object, e as EventArgs) Handles Rbanalysistype1.Click, Rbanalysistype2.Click, ...
Public Function RbAnaTypeClick(sender As Object, e As EventArgs) Handles RbAnaTypeAcIn.CheckedChanged
Dim SenderName As String = ""
Dim TitrType As String = ""
SenderName = CType(sender, RadioButton).Name
Select Case SenderName
Case Is = "RbAnaTypeAcIn"
TitrType = "AcIn"
'put correct Radiobuttion in GbAnaType in color
If RbAnaTypeAcIn.Checked = True Then
RbAnaTypeAcIn.BackColor = Color.Orange
End If
End Select
End Function
You're handling the wrong event for a start. Here's the sort of thing you should be doing:
Private Sub RadioButtons_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged,
RadioButton2.CheckedChanged
Dim rb = DirectCast(sender, RadioButton)
If rb.Checked Then
rb.BackColor = Color.Red
Else
rb.BackColor = Color.Green
End If
End Sub

Auto Login Procedure (Hide Form) vb.NET Windows Forms

Okay, I am having a bit of trouble here. I am creating a log in window for an application, but I am trying to get the application to automatically log in (i.e. perform the functions that happen when the user logs in) when it starts, without showing the log in screen, if the settings already have a stored email and password. I have a notification System Tray Icon that shows when the app is running, and when the form is not visible, a balloon notification pops up so the user knows that it is still running, and click on the icon to open the log in screen.
Take a look at the following code. I know that this If Not event is being called and working correctly, because it performs everything inside the statement EXCEPT hiding the form. Why does it not change to invisible? I also tried Me.Hide, and same issue. The Balloon Notification pops up, the text boxes fill with the previously stored data...but the form stays visible...
Private Sub RadFrmLogin_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Checks settings to see if email and password have already been stored and enters them into text fields, proceeds to automatically update access list
If Not String.IsNullOrEmpty(My.Settings.Email) And Not String.IsNullOrEmpty(My.Settings.Password) Then
TxtEmail.Text = My.Settings.Email
TxtPassword.Text = My.Settings.Password
Me.Visible = False
'Displays Balloon Tip
ntfySystemTrayIcon.ShowBalloonTip(800)
End If
End Sub
As an added note, I added a test button to hide the form, and it works perfectly:
Private Sub BtnHide_Click(sender As Object, e As EventArgs) Handles BtnHide.Click
'Hides form(for testing notification tray icon and balloon tip
Me.Visible = False
ntfySystemTrayIcon.ShowBalloonTip(1000)
End Sub
(removed my stupid default debug instructions since they did not help at all)
Update
okay, so there were similar questions before, take a look here: C#/.NET - WinForms - Instantiate a Form without showing it
short explanation: usually something like form1.show is used, so it is always changed to visible = true after the form_load is finished.
Either use the instructed event form_shown and add the visible=false
or another user recommended to change start properties to minimized and activate to hide program in taskbar. This helps to prevent that annoying flickering. I guess after that you can change the options back.
Update 2 The following seems to work well:
Private _IsVisible As Boolean
Public Property IsVisible() As Boolean
Get
Return _IsVisible
End Get
Set(ByVal value As Boolean)
_IsVisible = value
If _IsVisible Then
Me.WindowState = FormWindowState.Normal
Me.ShowInTaskbar = True
Me.Visible = True
Me.Activate()
Else
Me.WindowState = FormWindowState.Minimized
Me.ShowInTaskbar = False
Me.Visible = False
End If
End Set
End Property
If you want to get rid of the small taskbar flickering, then change the forms property showInTaskbar. When it is changed during the form_load, then there seem to be a short movement at the taskbar.
And to make it perfect, in form.Shown add following code:
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
Me.Visible = IsVisible
End Sub
now it is enough to use
IsVisible = False
in form_Load, or if you want to show it
IsVisible = True
Just some ideas:
If all your tasks are completed in the _Load event try just calling End. Of course that would remove your tray icon as well.
Another possibility is to call Me.Visible in the _Shown event. This may cause a flash on the screen. If so perhaps you could position the form off the screen in _Load.