Switch enabled-property of lable with doubleClick-event - vb.net

I have a lable and I want it to switch its enabled-property when doubleclicking on it. The code I wrote only works in one direction: if the label is enabled I can set enabled=false by doubleclicking, but if the label is not enabled I can't set enabled=true by doubleclicking. Here is my code:
Private Sub Label1_DoubleClick_1(sender As Object, e As EventArgs) Handles Label1.DoubleClick
If Me.Label1.Enabled = True Then
Me.Label1.Enabled = False
Else
Me.Label1.Enabled = True
End If
End Sub
How can I solve this problem? Thank you in advance!

The nature of the Enabled property is to prevent user interaction with a control. You can emulate the behavior you're looking for in a couple of ways:
Instead of disabling the control, manually change its appearance
If (Label1.ForeColor = Color.Black) Then
'"disable" control
Label1.ForeColor = Color.Gray
Else
'"enable" control
Label1.ForeColor = Color.Black
End If
Overlay the label with a transparent panel. Receive the DoubleClick event on the panel, but enable/disable the label below.

Related

Why won't the button hotkey show up with an underline in VB?

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

How do I change the colour of a button when pressed and when pressed the second time to get back to the default colour?

So I made a script for a pool/snooker business and i have a page with the buttons which represents the tables. The buttons are as default green and when I press on them I want the to turn red and when pressed again to return to the default colour. If you can help me i will be grateful. Thank you very much!
If WinForms then, assuming you have set button background to Green by default then:
Private Sub MyButton_Click(sender As Object, e As EventArgs) Handles MyButton.Click
If MyButton.BackColor = Color.Green Then
MyButton.BackColor = Color.Red
Else
MyButton.BackColor = Color.Green
End If
End Sub
If not using WinForms I suspect a similar approach would work - i.e. check colour first then toggle colour based on result of that check.

Setting a Button in a sub?

So, I have a little problem. Im making a application (in visual basic), and I have a way so that I can set a color when you hover over it. Now, I want to do this for all buttons, but make it a little easier by making a sub that can do this for me. The problem is, how can my sub tell which button to initialize the custom hover color? Here is my code.
Private Sub initButton(ByVal color As Color)
Button1.TabStop = False
Button1.FlatStyle = FlatStyle.Flat
Button1.FlatAppearance.BorderSize = 0
Button1.FlatAppearance.BorderColor = color
Button1.FlatAppearance.CheckedBackColor = color
Button1.FlatAppearance.MouseDownBackColor = color
Button1.FlatAppearance.MouseOverBackColor = color
End Sub
Now, how can I set Button1 to the button I want to initialize? Is there anyway to put that as an argument? If you find a answer, please reply.
As Plutonix suggested, using the Hover Event of the buttons would be the first logical choice. You will need to add a Parameter to your your Sub to pass in the button being hovered over.
Private Sub Button1_MouseHover(sender As Object, e As EventArgs)
Handles Button1.MouseHover,Button2.MouseHover 'add more buttons....
initButton(CType(sender, Button),Color.Blue)
End Sub
Private Sub initButton(hoverButton As Button, ByVal color As Color)
hoverButton.TabStop = False
hoverButton.FlatStyle = FlatStyle.Flat
hoverButton.FlatAppearance.BorderSize = 0
hoverButton.FlatAppearance.BorderColor = color
hoverButton.FlatAppearance.CheckedBackColor = color
hoverButton.FlatAppearance.MouseDownBackColor = color
hoverButton.FlatAppearance.MouseOverBackColor = color
End Sub

Mimic Disabled Focus Behaviour in WinForms

In accordance to this Post I'm trying to mimic the behavior of
Enabled = False
without actually disable the Control. (In my case a multiline TextBox)
The next I'm trying to accomplish is to mimic the focus behavior by mouse of a disabled control. If I click on a disabled control it won't get the focus and the control that previously had focus won't loose the focus.
What I came up with so far: I can intercept the WM_SETFOCUS message in WndProc so my control won't recieve focus.
Private Const WM_SETFOCUS = &H7
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If Me.ReadOnly AndAlso (m.Msg = WM_SETFOCUS) Then Exit Sub
MyBase.WndProc(m)
End Sub
The problem with that is, that the previous contol lost the focus, which isn't intended. How do I prevent that even the click by mouse will do anything in the focus behaviour? Is there any way to do this?
Update: 06.08.12
As suggested by Justin I solved the problem by changing it to a label in an autoscroll panel. A minimal code example is as followed:
Imports System.Windows.Forms
Public Class ScrollableDisabledTextBox
Inherits TextBox
Private xLabel As Label
Private xPanel As Panel
Public Sub New()
InizializeComponent()
End Sub
Private Sub InizializeComponent()
xPanel = New Panel
xPanel.AutoScroll = True
xPanel.BorderStyle = BorderStyle.FixedSingle
xLabel = New Label
xLabel.Enabled = False
xLabel.AutoSize = True
xPanel.Controls.Add(xLabel)
Me.Me_SizeChanged()
End Sub
Private Sub Me_EnabledChanged() Handles Me.EnabledChanged
If Me.Enabled Then
Me.Show()
xPanel.Hide()
Else
xPanel.Show()
Me.SendToBack()
Me.Hide()
End If
End Sub
Private Sub Me_TextChanged() Handles Me.TextChanged
xLabel.Text = Me.Text
End Sub
Private Sub Me_SizeChanged() Handles Me.SizeChanged
xPanel.Size = Me.Size
xLabel.MaximumSize = New System.Drawing.Size(xPanel.Size.Width, 0)
End Sub
Private Sub Me_ParentChanged() Handles Me.ParentChanged
xPanel.Location = Me.Location
'If parent changed multiple times, remember to remove panel from old parent!
If Not Me.Parent.Controls.Contains(xPanel) Then
Me.Parent.Controls.Add(xPanel)
End If
End Sub
End Class
I do not believe what you want to do is possible. If you do not have focus, then the scrolling will not work.
However, I posit that you should rethink your original problem. Why not use an AutoSize = true, MaximumSize.Width = ParentWidth label (which could be disabled) inside of a panel that will autoscroll. This sounds like what you are really looking for.

Button BackColor Not Working As Expected

I have a form with a split panel. In the one split are a group of buttons which I want to programmatically change the color of the last pressed button. The following loop seems to run correctly and set the colors correctly but the form doesn't represent that. Once the loop is completed and I recheck the button colors, they revert to previous state.
For Each formControl As Control In Me.FormSplitContainer.Panel1.Controls
If formControl.GetType() Is GetType(Button) Then
If CType(sender, Button) Is CType(formControl, Button) Then
CType(sender, Button).BackColor = Color.White
Else
CType(sender, Button).BackColor = System.Drawing.SystemColors.ControlDark
End If
End If
Next
I can get the desired effect by doing the below code but seems less elegant and would obviously require updates as buttons would be added or removed.
DataFeedButton.BackColor = System.Drawing.SystemColors.ControlDark
IncentiveButton.BackColor = System.Drawing.SystemColors.ControlDark
CType(sender, Button).BackColor = Color.White
Anyone see what I am missing?
Assign sender to a button variable, then assign the color.
dim b as button
And then in the loop, assign it this way:
b = sender
b.backcolor = color.white
Where are you calling this code? Each time they click a button? To me, it looks like it sets the backcolor of all the form's buttons to white. I don't see where you're testing for the 'last button pressed' condition.