How can I toggle button with key VB - vb.net

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.

Related

vb.net detect keypress on a form

I am new to vb.net and am trying to detect a KeyPress on a Form
I have accomplished this in JavaFX by creating a listener that when the ESC Key is press the application close
I have not found any code examples that use a listener in vb.net
I have found code that Handles a KeyPress for a TextBox but the same code for a Form FAILS
For this function to close the application from any Form I am wondering if it needs to be declared in a Module? While that part of the question would be nice to know Call it a Bonus
My question is why is this code not detecting a keypress on frmOne ?
The code to detect a keypress in the txtBoxOne runs as expected
Public Class frmOne
Private Sub frmOne_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
'frmOne Property FixedToolWindow
'frmOne is the Start Up Form
If Asc(e.KeyChar) > 1 Then
MessageBox.Show("You Pressed " & e.KeyChar)
End If
'If Asc(e.KeyChar) > 1 Then txtBoxOne.Text = "You Pressed"
End Sub
Private Sub txtBoxOne_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtBoxOne.KeyPress
If Asc(e.KeyChar) = 13 Then
e.Handled = True
MsgBox("Error.")
Else
e.Handled = False
End If
End Sub
Private Sub btnToFormTwo_Click(sender As Object, e As EventArgs) Handles btnToFormTwo.Click
Dim i As Integer
i = txtBoxOne.Text.Length
If i = 0 Then
'txtBoxOne.Text = "Enter"
MessageBox.Show("Enter Data")
txtBoxOne.Select()
Return
End If
Dim OBJ As New frmTwo
OBJ.SPass = txtBoxOne.Text
OBJ.Show()
'MyTextBox_Enter()
txtBoxOne.Clear()
Me.Hide()
'Me.Close()'R Click project PassVar Set Start Up Form
'Best Solution is to have Splash Form as Start Up Form
End Sub
Public Sub MyTextBox_Enter()
txtBoxOne.Clear()
End Sub
'Private Sub frmOne_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Use code below if pre populated text in txtBoxOne
'Me.ActiveControl = txtBoxOne
'txtBoxOne.Select(txtBoxOne.Text.Length, 0)
'txtBoxOne.Select()
'End Sub
End Class
The same code will work for a form but a form will not raise keyboard events by default if a child control has focus. You need to set the form's KeyPreview property to True, in which case the form will raise those keyboard events before the active child control does.

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

Changing checkbox appearance to button

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

VB.Net program minimized not working

I have a code like below
Private Sub InterfaceProg_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
'program minimized
Try
If Me.WindowState = FormWindowState.Minimized Then
Me.Visible = True
NotifyIcon1.Visible = True
NotifyIcon1.ShowBalloonTip(1, "Browser Bandwidth Optimizer", "Program Minimized", ToolTipIcon.Info)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
The code is working fine but there is one problem.
When i press the minimize button on top right, the program go to system tray
When i press close button, the program also go to system also go to system tray.
i want to make the program go to system tray if user press close button only and minimized the program to taskbar if user press minimized. how to do it?
Use the FormClosing event instead of the Resize event:
Private CloseAllowed As Boolean
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
If Not CloseAllowed And e.CloseReason <> CloseReason.WindowsShutDown Then
Me.Hide()
e.Cancel = True
NotifyIcon1.Visible = True
'' etc..
End If
End Sub
You still need to give the user a way to exit the program. The context menu for the NotifyIcon is the usual approach. Add an Exit item:
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
CloseAllowed = True
Me.Close()
End Sub
When ever you press the minimize or close buttons a WM_SYSCOMMAND message is sent. The WPARAM specifies which button is pressed:
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = &H112 Then 'WM_SYSCOMMAND
If CInt(m.WParam) = &HF060 Then 'SC_CLOSE, the close button is pressed
Me.Visible = False
Me.ShowInTaskbar = False
Return 'cancel the message
End If
If CInt(m.WParam) = &HF020 Then 'SC_MINIMIZE, the minimize button is pressed
'do your staff
End If
End If
MyBase.WndProc(m)
End Sub
WM_SYSCOMMAND message
Then you need to call your Code of "InterfaceProg_Resize" in the form closing even.
So make a new sub "Private Sub ProgToTray()"
Also you don't need a try catch for it.
You also need to hide the Taskbar Icon "Me.ShowInTaskbar = False"
And minimize the form"Me.WindowState = FormWindowState.Minimized"
Private Sub ProgToTray()
Me.ShowInTaskbar = False
Me.WindowState = FormWindowState.Minimized
NotifyIcon1.Visible = True
NotifyIcon1.ShowBalloonTip(1, "Browser Bandwidth Optimizer", "Program Minimized", ToolTipIcon.Info)
End Sub
If the user clicks x on your form ,Then you need to Cancel the closing of the form in the FormClosing event."e.Cancel = True"
Then you call "ProgToTray()"
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
e.Cancel = True
ProgToTray()
End Sub
Then if you close the form with x it will go to the notify area.
Don't forget to make a click event to open the form again from the notify icon and an option so the user can exit the form.
To make an exit button you need to add a ContextMenuStrip1 to your form .
To open the ContextMenuStrip1 when you right click the NotifyIcon1 you need add the code "ContextMenuStrip1.Show(Cursor.Position)" to you NotifyIcon1 click event
You also need to check which button is clicked.
"If e.Button = Windows.Forms.MouseButtons.Right Then" for the right button.
and
"e.Button = Windows.Forms.MouseButtons.Left Then" for the left button.
So if the user clicks left the form will open again and when he clicks right the ContextMenuStrip1 will show.
If the user clicks left mouse button you set the formwindowstate back to normal.
Also show the taskbar icon again and hide the tray icon.
Private Sub NotifyIcon1_MouseClick(sender As Object, e As MouseEventArgs) Handles NotifyIcon1.MouseClick
If e.Button = Windows.Forms.MouseButtons.Right Then
ContextMenuStrip1.Show(Cursor.Position)
ElseIf e.Button = Windows.Forms.MouseButtons.Left Then
Me.WindowState = FormWindowState.Normal
Me.ShowInTaskbar = True
NotifyIcon1.Visible = False
End If
End Sub
Then you make an click event for the exit button and remove the handler for the form closing event "RemoveHandler MyBase.FormClosing, AddressOf Form1_FormClosing".
The canceling of closing the form will not take place.
Then call me.close()
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
RemoveHandler MyBase.FormClosing, AddressOf Form1_FormClosing
Me.Close()
End Sub

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