Changing checkbox appearance to button - vb.net

I am using a checkbox as a toggle switch (ON-OFF Button) by changing its appearance to Button using Checkbox.Appearance property.
When I run the VB.NET application, the Checkbox turns into a button only after the first click. Is it possible to change the appearance of the Checkbox to button as default? I also want the button to Popup when the user clicks "ON". I have tried changing the appearance using FlatStyle property, but it doesn't work.
I am using the following code:
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
CheckBox1.Appearance = System.Windows.Forms.Appearance.Button
If CheckBox1.Checked = True Then
CheckBox1.Text = "ON"
ElseIf CheckBox1.Checked = False Then
CheckBox1.Text = "OFF"
End If
End Sub

It is possible to select Appearance into the Load event of the form
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.CheckBox1.Appearance = Appearance.Button
End Sub

Related

How can I toggle button with key VB

I try to toggle button with a key I can't find the solution. Can Someone help me? Here is the code of button.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
toggle = toggle + 1
If toggle = 1 Then
Timer1.Start()
Button1.Text = "Status: ON"
Else
Timer1.Stop()
toggle = 0
Button1.Text = "Status: OFF"
End If
End Sub
You can use the KeyDown or KeyUp event of the Form:
'bind the KeyUp event
AddHandler Me.KeyUp, AddressOf SubToPressButton
'the Sub which is triggered by the KeyUp event
Sub SubToPressButton(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
'click button on key "A"
If e.KeyCode = Keys.A Then
Button1.PerformClick()
End If
End Sub
'your Button1 click event
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
toggle += 1
If toggle = 1 Then
Timer1.Start()
Button1.Text = "Status: ON"
Else
Timer1.Stop()
toggle = 0
Button1.Text = "Status: OFF"
End If
End Sub
You also have to enable the KeyPreview of the Form:
Me.KeyPreview = True
You can set the above line to the constructor (New) or Load event of the Form. You can also enable the KeyPreview directly on the properties of the Form.
You need to toggle the button outside the application?
In this case you need global keyboard hooks. You can find a solution on StackOverflow already:
listen for a key when the application is not focused
Catching "global hotkeys" (Windows)
But be careful, other applications can use global or local hotkeys too.
I would probably learn from the other answers - regarding KeyUp/Down events, but just to enhance your existing code, try this;
Private toggle As Boolean
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Not toggle Then
Timer1.Start()
Button1.Text = "Status: ON"
Else
Timer1.Stop()
Button1.Text = "Status: OFF"
End If
toggle = Not toggle
End Sub
What I've done is;
Change the toggle integer to a boolean - since it's being used as a flag
Use that flag to toggle the timer on or off for every button click
Invert the flag at the end of every button click
I hope that helps - but as noted above - there are better ways to achieve this!
Use an accelerator key, i.e. add an ampersand (&) to the Text property and you can use the subsequent character's key with the Alt key.
Also, instead of a Button, use a Checkbox with its Appearance set to Button.

The checkboxes as buttons has a color they all change to when clicked, how to alter this?

On a form the check boxes can appear as buttons, when these are selected, they change colour to a light blue, how to alter this to something else??
Is this what you require:
Start a new Form App
Add a checkbox to the form
Double-click the checkbox to generate the CheckedChanged event then use this code
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked Then
CheckBox1.BackColor = Color.LightBlue
Else
CheckBox1.BackColor = Nothing
End If
End Sub

Changed checked state on checkbox with button appearance in VB.NET

As stated in my summary, I am currently working on a Virtual OS in VB.Net. I am currently working on the session as I am done with the login stuff.
I am having trouble with a checkbox with button appearance. I want to set the CheckState to Checked if I click on the button with the Click() event like this:
Private Sub btnApps_Click(Byval sender As Object, Byval e As EventArgs) Handles btnApps.Click()
If btnApps.CheckState = CheckState.Checked Then
btnApps.CheckState = CheckState.Unchecked
Else
btnApps.CheckState = CheckState.Checked
End If
End Sub
I also tried the Checked property.
This code is not working at all, if I put the whole If-End If section in the CheckedChanged event I get a StackOverflowException. What am I doing wrong?
The CheckBox is a custom control b.t.w.
If you'd like to prevent your Checkbox from automatically changing state and change the appearance with your own Click event, you can turn AutoCheck to false.
https://msdn.microsoft.com/en-us/library/system.windows.forms.checkbox.autocheck(v=vs.110).aspx
Information found thanks to this question: How to cancel RadioButton or CheckBox checked change
Public Class Form1
Private WithEvents btnApps As New clsChk
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
btnApps.AutoCheck = False
Me.Controls.Add(btnApps)
End Sub
Private Sub btnApps_Click(sender As Object, e As EventArgs) Handles btnApps.Click
Debug.WriteLine(btnApps.CheckState)
If btnApps.CheckState = CheckState.Checked Then
btnApps.CheckState = CheckState.Unchecked
Else
btnApps.CheckState = CheckState.Checked
End If
End Sub
End Class

VB.net disable a TabPage of a TabControl

I currently have a form that uses TabControl which has 5 TabPages. I want to create a button that could disable a specific TabPage.
I have tried
TabPage1.Enabled = False
But it does not work. How do I do this?
you need to use the TabPages collection. Add a button to your form and try this
Private Sub Button1_Click( sender As Object, e As EventArgs) Handles Button1.Click
TabControl1.TabPages(0).Enabled =false
End Sub
It's a base zero array, so in your case it should be from 0-4.
Or you can access it from the text of the tab
Private Sub Button2_Click( sender As Object, e As EventArgs) Handles Button2.Click
Dim tabPage As TabPage
For Each tabPage In TabControl1.TabPages
If tabPage.Text ="TabPage2"
tabPage.Enabled =False
End If
Next
End Sub
Currently, the following two code blocks does the same thing: disables all the controls on that TabPage (Sets Control.Enabled = False). The tab itself is still visible and selectable from the TabControl, it is not hidden. The tab is selectable and all the elements appear disabled.
TabMyTab.Enabled = False
MyTabControl.TabPages(4).Enabled = False where the TabPages(4) is the 5th in the TabControl collection.
Your initial code should work if that is your intent.
If you want to disable the tab similar to i.e. button.Enabled = False which does not allow the control to be used, you will need to do something different as disabling a TabPage as in the code above disables all controls in that tab. If this is what you want, keep reading. A lot of programmers suggest using the TabControl to disallow the tab from being selected by selecting a different or the previously selected tab. This is the most effective way I know. I would implement this as follows:
Private PreviousTab As New TabPage
Private CurrentTab As New TabPage
Private Sub TabControlName_Deselected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControlName.Deselected
PreviousTab = e.TabPage
End Sub
Private Sub TabControlName_Selected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControlName.Selected
CurrentTab = e.TabPage
If (PreviousTab.Name <> CurrentTab.Name) And (CurrentTab.Name = UnselectableTab.Name) Then
MessageBox.Show("Tab disabled.", "Selection Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
TabControlName.SelectedTab = PreviousTab
End If
End Sub
Substitute your own values for "UnselectableTab" and "TabControlName" for your project.
You can combine the use of disabling the tab, that way the behavior is dynamic if you change which tabs are enabled or disabled in code.
Private Sub TabControl1_Deselected(sender As Object, e As TabControlEventArgs) Handles TabControl1.Deselected
PreviousTab = e.TabPage
End Sub
.
Private Sub TabControl1_Selected(sender As Object, e As TabControlEventArgs) Handles TabControl1.Selected
If Not e.TabPage.Enabled Then
TabControl1.SelectedTab = PreviousTab
End If
End Sub
You can disabled a tab by setting its Enabled property:
TabControl1.TabPages("tbPage1").Enabled = False

How to stop space bar from clicking button

I'm using VS 2008. In either a Windows or Smart Device project, pressing Space bar key when a button currently has the focus will cause the button to be clicked and it will raise the Click event. Is there any property or setting which will cause it to skip it so that the space bar does not cause a button click?
You can use the Form's KeyPreview property along with the KeyDown event to catch the space bar before it is passed to the button and if the button has focus, then you can mark the key press handled to prevent it from bubbling up:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MessageBox.Show("Button Clicked")
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.KeyPreview = True
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Space And Button1.Focused Then
e.Handled = True
End If
End Sub